@kaspernj/api-maker 1.0.210 → 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.210",
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,101 +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.getModelId(),
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
- componentDidUpdate() {
20
- const newModelId = this.getModelId()
22
+ componentDidMount() {
23
+ this.loadModel()
24
+ }
21
25
 
22
- // The model ID was changed in the URL and a different model should be loaded
23
- if (newModelId != this.state.modelId) {
24
- this.setState({model: undefined, modelId: newModelId})
25
- this.loadExistingModel()
26
+ componentDidUpdate() {
27
+ const newModelId = this.getModelId()
28
+
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
+ }
26
34
  }
27
- }
28
35
 
29
- loadModel = async () => {
30
- if (args.newIfNoId && !this.getModelId()) {
31
- return await this.loadNewModel()
32
- } else if (!args.optional || this.getModelId()) {
33
- return await this.loadExistingModel()
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
+ }
34
42
  }
35
- }
36
43
 
37
- getModelId() {
38
- return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
39
- }
44
+ getModelId() {
45
+ if (args.loadByQueryParam)
46
+ return args.loadByQueryParam({props: this.props})
40
47
 
41
- loadExistingModel = async () => {
42
- const modelId = this.getModelId()
43
- const query = await ModelClass.ransack({id_eq: modelId})
48
+ return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
49
+ }
44
50
 
45
- if (args.abilities) query.abilities(args.abilities)
46
- if (args.preload) query.preload(args.preload)
47
- if (args.select) query.select(args.select)
51
+ loadExistingModel = async () => {
52
+ const modelId = this.getModelId()
53
+ const query = await ModelClass.ransack({id_eq: modelId})
48
54
 
49
- const model = await query.first()
55
+ if (args.abilities) query.abilities(args.abilities)
56
+ if (args.preload) query.preload(args.preload)
57
+ if (args.select) query.select(args.select)
50
58
 
51
- this.setState({
52
- model,
53
- notFound: !model
54
- })
55
- }
59
+ const model = await query.first()
60
+
61
+ this.setState({
62
+ model,
63
+ notFound: !model
64
+ })
65
+ }
56
66
 
57
- async loadNewModel() {
58
- const params = Params.parse()
59
- const paramKey = ModelClass.modelName().paramKey()
60
- const modelDataFromParams = params[paramKey] || {}
67
+ async loadNewModel() {
68
+ const params = Params.parse()
69
+ const paramKey = ModelClass.modelName().paramKey()
70
+ const modelDataFromParams = params[paramKey] || {}
61
71
 
62
- let defaults = {}
72
+ let defaults = {}
63
73
 
64
- if (args.newIfNoId?.defaults) {
65
- defaults = await args.newIfNoId.defaults()
74
+ if (args.newIfNoId?.defaults) {
75
+ defaults = await args.newIfNoId.defaults()
76
+ }
77
+
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})
66
85
  }
67
86
 
68
- const modelData = Object.assign(defaults, args.newAttributes, modelDataFromParams)
69
- const model = new ModelClass({
70
- isNewRecord: true,
71
- data: {a: modelData}
72
- })
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
+ }
73
108
 
74
- this.setState({model})
109
+ reloadModel = () => this.loadModel()
110
+ onUpdated = this.loadExistingModel
75
111
  }
76
112
 
77
- render() {
78
- const {onUpdated, reloadModel} = digs(this, "onUpdated", "reloadModel")
79
- const {model, modelId, notFound} = digs(this.state, "model", "modelId", "notFound")
80
- const wrappedComponentProps = {}
81
-
82
- wrappedComponentProps[this.camelizedLower] = model
83
- wrappedComponentProps[`${this.camelizedLower}Id`] = modelId
84
- wrappedComponentProps[`${this.camelizedLower}NotFound`] = notFound
85
-
86
- return (
87
- <>
88
- {args.events &&
89
- <EventEmitterListener event="reloadModel" events={args.events} onCalled={reloadModel} />
90
- }
91
- {model && args.eventUpdated &&
92
- <EventUpdated model={model} onUpdated={onUpdated} />
93
- }
94
- <WrappedComponent {...wrappedComponentProps} {...this.props} />
95
- </>
96
- )
97
- }
113
+ if (args.loadByQueryParam) return withQueryParams(ModelLoadWrapper)
98
114
 
99
- reloadModel = () => this.loadModel()
100
- onUpdated = this.loadExistingModel
115
+ return ModelLoadWrapper
101
116
  }