@kaspernj/api-maker 1.0.314 → 1.0.316

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.314",
19
+ "version": "1.0.316",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
package/src/use-model.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import {useCallback, useEffect, useState} from "react"
2
+ import Devise from "./devise.mjs"
2
3
  import * as inflection from "inflection"
3
4
  import ModelEvents from "./model-events.mjs"
4
5
  import useQueryParams from "on-location-changed/src/use-query-params.js"
@@ -120,6 +121,24 @@ const useModel = (modelClassArg, argsArg = {}) => {
120
121
  }
121
122
  }, [args.eventUpdated, model?.id()])
122
123
 
124
+ const onSignedIn = useCallback(() => {
125
+ loadModel()
126
+ }, [])
127
+
128
+ const onSignedOut = useCallback(() => {
129
+ loadModel()
130
+ }, [])
131
+
132
+ useEffect(() => {
133
+ Devise.events().addListener("onDeviseSignIn", onSignedIn)
134
+ Devise.events().addListener("onDeviseSignOut", onSignedOut)
135
+
136
+ return () => {
137
+ Devise.events().removeListener("onDeviseSignIn", onSignedIn)
138
+ Devise.events().removeListener("onDeviseSignOut", onSignedOut)
139
+ }
140
+ })
141
+
123
142
  const onDestroyed = useCallback(({model}) => {
124
143
  const forwardArgs = {model}
125
144
 
@@ -1,144 +1,15 @@
1
- import {digg, digs} from "diggerize"
2
- import EventUpdated from "./event-updated"
3
- import * as inflection from "inflection"
4
- import Params from "./params.mjs"
5
- import PropTypes from "prop-types"
6
- import React from "react"
7
- import withQueryParams from "on-location-changed/src/with-query-params"
1
+ import {memo} from "react"
2
+ import useModel from "./use-model.mjs"
8
3
 
9
- export default (WrappedComponent, modelClassArg, argsArg = {}) => {
10
- class ApiMakerWithModel extends React.PureComponent {
11
- static propTypes = {
12
- queryParams: PropTypes.object
13
- }
4
+ export default (WrappedComponent, modelClass, givenArgs) => {
5
+ const ApiMakerWithModel = (props) => {
6
+ const args = Object.assign({match: props.match}, givenArgs)
7
+ const useModelResult = useModel(modelClass, args)
14
8
 
15
- modelClass = this.resolveModelClass(modelClassArg)
16
- args = this.resolveArgs()
17
- modelVariableName = inflection.camelize(this.modelClass.modelClassData().name, true)
18
- paramsVariableName = `${this.modelClass.modelName().paramKey()}_id`
19
-
20
- state = {
21
- model: undefined,
22
- modelId: this.getModelId(),
23
- notFound: undefined
24
- }
25
-
26
- resolveArgs() {
27
- if (typeof argsArg == "function") {
28
- return argsArg({
29
- modelClass: this.modelClass
30
- })
31
- }
32
-
33
- return argsArg
34
- }
35
-
36
- resolveModelClass(modelClassArg) {
37
- if (typeof modelClassArg == "object") {
38
- const {queryParams} = digs(this.props, "queryParams")
39
-
40
- return modelClassArg.callback({queryParams})
41
- }
42
-
43
- return modelClassArg
44
- }
45
-
46
- componentDidMount() {
47
- this.loadModel()
48
- }
49
-
50
- componentDidUpdate() {
51
- const newModelId = this.getModelId()
52
-
53
- // The model ID was changed in the URL and a different model should be loaded
54
- if (newModelId != this.state.modelId) {
55
- this.setState({model: undefined, modelId: newModelId})
56
- this.loadExistingModel()
57
- }
58
- }
59
-
60
- loadModel = async () => {
61
- if (this.args.newIfNoId && !this.getModelId()) {
62
- return await this.loadNewModel()
63
- } else if (!this.args.optional || this.getModelId()) {
64
- return await this.loadExistingModel()
65
- }
66
- }
67
-
68
- getModelId() {
69
- if (this.args.loadByQueryParam)
70
- return this.args.loadByQueryParam({props: this.props})
71
-
72
- return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
73
- }
74
-
75
- loadExistingModel = async () => {
76
- const modelId = this.getModelId()
77
- const ModelClass = digg(this, "modelClass")
78
- const query = await ModelClass.ransack({id_eq: modelId})
79
-
80
- if (!modelId) throw new Error(`No model ID was given: ${modelId} by '${this.paramsVariableName}' in query params: ${Object.keys(this.props.match.params).join(", ")}`)
81
- if (this.args.abilities) query.abilities(this.args.abilities)
82
- if (this.args.preload) query.preload(this.args.preload)
83
- if (this.args.select) query.select(this.args.select)
84
-
85
- const model = await query.first()
86
-
87
- this.setState({
88
- model,
89
- notFound: !model
90
- })
91
- }
92
-
93
- async loadNewModel() {
94
- const params = Params.parse()
95
- const ModelClass = digg(this, "modelClass")
96
- const paramKey = ModelClass.modelName().paramKey()
97
- const modelDataFromParams = params[paramKey] || {}
98
-
99
- let defaults = {}
100
-
101
- if (this.args.newIfNoId?.defaults) {
102
- defaults = await this.args.newIfNoId.defaults()
103
- }
104
-
105
- const modelData = Object.assign(defaults, this.args.newAttributes, modelDataFromParams)
106
- const model = new ModelClass({
107
- isNewRecord: true,
108
- data: {a: modelData}
109
- })
110
-
111
- this.setState({model})
112
- }
113
-
114
- render() {
115
- const {onUpdated, reloadModel} = digs(this, "onUpdated", "reloadModel")
116
- const {model, modelId, notFound} = digs(this.state, "model", "modelId", "notFound")
117
- const wrappedComponentProps = {}
118
-
119
- wrappedComponentProps[this.modelVariableName] = model
120
- wrappedComponentProps[`${this.modelVariableName}Id`] = modelId
121
- wrappedComponentProps[`${this.modelVariableName}NotFound`] = notFound
122
-
123
- return (
124
- <>
125
- {this.args.events &&
126
- <EventEmitterListener event="reloadModel" events={this.args.events} onCalled={reloadModel} />
127
- }
128
- {model && this.args.eventUpdated &&
129
- <EventUpdated model={model} onUpdated={onUpdated} />
130
- }
131
- {model && this.args.onDestroyed &&
132
- <EventDestroyed model={model} onDestroyed={this.args.onDestroyed} />
133
- }
134
- <WrappedComponent {...wrappedComponentProps} {...this.props} />
135
- </>
136
- )
137
- }
138
-
139
- reloadModel = () => this.loadModel()
140
- onUpdated = this.loadExistingModel
9
+ return (
10
+ <WrappedComponent {...useModelResult} {...props} />
11
+ )
141
12
  }
142
13
 
143
- return withQueryParams(ApiMakerWithModel)
14
+ return memo(ApiMakerWithModel)
144
15
  }