@kaspernj/api-maker 1.0.314 → 1.0.315
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 +1 -1
- package/src/with-model.jsx +10 -139
package/package.json
CHANGED
package/src/with-model.jsx
CHANGED
|
@@ -1,144 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
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,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
|
14
|
+
return memo(ApiMakerWithModel)
|
|
144
15
|
}
|