@kaspernj/api-maker 1.0.208 → 1.0.2011

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.208",
19
+ "version": "1.0.2011",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -637,7 +637,7 @@ class BaseModel {
637
637
  try {
638
638
  return await CommandsPool.addCommand(args, commandArgs)
639
639
  } catch (error) {
640
- if (formOrDataObject.nodeName == "FORM") {
640
+ if (formOrDataObject?.nodeName == "FORM") {
641
641
  BaseModel.parseValidationErrors({error, options: {form: formOrDataObject}})
642
642
  }
643
643
 
@@ -1,91 +1,116 @@
1
1
  import EventUpdated from "./event-updated"
2
2
  import Params from "./params.mjs"
3
+ import PropTypes from "prop-types"
3
4
  import React from "react"
5
+ import withQueryParams from "on-location-changed/src/with-query-params"
4
6
 
5
- export default (WrappedComponent, ModelClass, args = {}) => class modelLoadWrapper extends React.PureComponent {
6
- camelizedLower = ModelClass.modelName().camelizedLower()
7
- paramsVariableName = `${ModelClass.modelName().paramKey()}_id`
7
+ export default (WrappedComponent, ModelClass, args = {}) => {
8
+ class ModelLoadWrapper extends React.PureComponent {
9
+ static propTypes = {
10
+ queryParams: PropTypes.object
11
+ }
8
12
 
9
- state = {
10
- model: undefined,
11
- modelId: this.props.match.params[this.paramsVariableName] || this.props.match.params.id,
12
- notFound: undefined
13
- }
13
+ camelizedLower = ModelClass.modelName().camelizedLower()
14
+ paramsVariableName = `${ModelClass.modelName().paramKey()}_id`
14
15
 
15
- componentDidMount() {
16
- this.loadModel()
17
- }
16
+ state = {
17
+ model: undefined,
18
+ modelId: this.getModelId(),
19
+ notFound: undefined
20
+ }
18
21
 
19
- loadModel = async () => {
20
- if (args.newIfNoId && !this.getModelId()) {
21
- return await this.loadNewModel()
22
- } else if (!args.optional || this.getModelId()) {
23
- return await this.loadExistingModel()
22
+ componentDidMount() {
23
+ this.loadModel()
24
24
  }
25
- }
26
25
 
27
- getModelId() {
28
- return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
29
- }
26
+ componentDidUpdate() {
27
+ const newModelId = this.getModelId()
30
28
 
31
- loadExistingModel = async () => {
32
- const {modelId} = digs(this.state, "modelId")
33
- const query = await ModelClass.ransack({id_eq: modelId})
29
+ // The model ID was changed in the URL and a different model should be loaded
30
+ if (newModelId != this.state.modelId) {
31
+ this.setState({model: undefined, modelId: newModelId})
32
+ this.loadExistingModel()
33
+ }
34
+ }
34
35
 
35
- if (args.abilities) query.abilities(args.abilities)
36
- if (args.preload) query.preload(args.preload)
37
- if (args.select) query.select(args.select)
36
+ loadModel = async () => {
37
+ if (args.newIfNoId && !this.getModelId()) {
38
+ return await this.loadNewModel()
39
+ } else if (!args.optional || this.getModelId()) {
40
+ return await this.loadExistingModel()
41
+ }
42
+ }
38
43
 
39
- const model = await query.first()
44
+ getModelId() {
45
+ if (args.loadByQueryParam)
46
+ return args.loadByQueryParam({props: this.props})
40
47
 
41
- this.setState({
42
- model,
43
- notFound: !model
44
- })
45
- }
48
+ return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
49
+ }
46
50
 
47
- async loadNewModel() {
48
- const params = Params.parse()
49
- const paramKey = ModelClass.modelName().paramKey()
50
- const modelDataFromParams = params[paramKey] || {}
51
+ loadExistingModel = async () => {
52
+ const modelId = this.getModelId()
53
+ const query = await ModelClass.ransack({id_eq: modelId})
51
54
 
52
- let defaults = {}
55
+ if (args.abilities) query.abilities(args.abilities)
56
+ if (args.preload) query.preload(args.preload)
57
+ if (args.select) query.select(args.select)
53
58
 
54
- if (args.newIfNoId?.defaults) {
55
- defaults = await args.newIfNoId.defaults()
59
+ const model = await query.first()
60
+
61
+ this.setState({
62
+ model,
63
+ notFound: !model
64
+ })
56
65
  }
57
66
 
58
- const modelData = Object.assign(defaults, args.newAttributes, modelDataFromParams)
59
- const model = new ModelClass({
60
- isNewRecord: true,
61
- data: {a: modelData}
62
- })
67
+ async loadNewModel() {
68
+ const params = Params.parse()
69
+ const paramKey = ModelClass.modelName().paramKey()
70
+ const modelDataFromParams = params[paramKey] || {}
63
71
 
64
- this.setState({model})
65
- }
72
+ let defaults = {}
73
+
74
+ if (args.newIfNoId?.defaults) {
75
+ defaults = await args.newIfNoId.defaults()
76
+ }
66
77
 
67
- render() {
68
- const {onUpdated, reloadModel} = digs(this, "onUpdated", "reloadModel")
69
- const {model, modelId, notFound} = digs(this.state, "model", "modelId", "notFound")
70
- const wrappedComponentProps = {}
71
-
72
- wrappedComponentProps[this.camelizedLower] = model
73
- wrappedComponentProps[`${this.camelizedLower}Id`] = modelId
74
- wrappedComponentProps[`${this.camelizedLower}NotFound`] = notFound
75
-
76
- return (
77
- <>
78
- {args.events &&
79
- <EventEmitterListener event="reloadModel" events={args.events} onCalled={reloadModel} />
80
- }
81
- {model && args.eventUpdated &&
82
- <EventUpdated model={model} onUpdated={onUpdated} />
83
- }
84
- <WrappedComponent {...wrappedComponentProps} {...this.props} />
85
- </>
86
- )
78
+ const modelData = Object.assign(defaults, args.newAttributes, modelDataFromParams)
79
+ const model = new ModelClass({
80
+ isNewRecord: true,
81
+ data: {a: modelData}
82
+ })
83
+
84
+ this.setState({model})
85
+ }
86
+
87
+ render() {
88
+ const {onUpdated, reloadModel} = digs(this, "onUpdated", "reloadModel")
89
+ const {model, modelId, notFound} = digs(this.state, "model", "modelId", "notFound")
90
+ const wrappedComponentProps = {}
91
+
92
+ wrappedComponentProps[this.camelizedLower] = model
93
+ wrappedComponentProps[`${this.camelizedLower}Id`] = modelId
94
+ wrappedComponentProps[`${this.camelizedLower}NotFound`] = notFound
95
+
96
+ return (
97
+ <>
98
+ {args.events &&
99
+ <EventEmitterListener event="reloadModel" events={args.events} onCalled={reloadModel} />
100
+ }
101
+ {model && args.eventUpdated &&
102
+ <EventUpdated model={model} onUpdated={onUpdated} />
103
+ }
104
+ <WrappedComponent {...wrappedComponentProps} {...this.props} />
105
+ </>
106
+ )
107
+ }
108
+
109
+ reloadModel = () => this.loadModel()
110
+ onUpdated = this.loadExistingModel
87
111
  }
88
112
 
89
- reloadModel = () => this.loadModel()
90
- onUpdated = this.loadExistingModel
113
+ if (args.loadByQueryParam) return withQueryParams(ModelLoadWrapper)
114
+
115
+ return ModelLoadWrapper
91
116
  }