@kaspernj/api-maker 1.0.384 → 1.0.385

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/use-model.mjs +24 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaspernj/api-maker",
3
- "version": "1.0.384",
3
+ "version": "1.0.385",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
package/src/use-model.mjs CHANGED
@@ -17,6 +17,11 @@ const useModel = (modelClassArg, argsArg = {}) => {
17
17
 
18
18
  const s = useShape(args)
19
19
 
20
+ s.useStates({
21
+ model: undefined,
22
+ notFound: undefined
23
+ })
24
+
20
25
  if (typeof modelClassArg == "object") {
21
26
  modelClass = modelClassArg.callback({queryParams})
22
27
  } else {
@@ -28,7 +33,7 @@ const useModel = (modelClassArg, argsArg = {}) => {
28
33
 
29
34
  if (args.loadByQueryParam) {
30
35
  modelId = args.loadByQueryParam({queryParams})
31
- } else {
36
+ } else if (!args.query) {
32
37
  if (!args.match) throw new Error("Both 'loadByQueryParam' and 'match' wasn't given")
33
38
 
34
39
  modelId = args.match.params[paramsVariableName] || args.match.params.id
@@ -36,19 +41,21 @@ const useModel = (modelClassArg, argsArg = {}) => {
36
41
 
37
42
  const modelVariableName = inflection.camelize(modelClass.modelClassData().name, true)
38
43
  const cacheArgs = [modelId]
39
- const [model, setModel] = useState(undefined)
40
- const [notFound, setNotFound] = useState(undefined)
41
44
 
42
45
  if (args.cacheArgs) {
43
46
  cacheArgs.push(...args.cacheArgs)
44
47
  }
45
48
 
46
- s.updateMeta({modelId, modelVariableName, queryParams})
49
+ s.updateMeta({args, modelId, modelVariableName, queryParams})
47
50
 
48
51
  const loadExistingModel = useCallback(async () => {
49
- const query = await modelClass.ransack({id_eq: s.m.modelId})
52
+ let query
50
53
 
51
- if (!modelId) {
54
+ if (s.m.modelId) {
55
+ query = modelClass.ransack({id_eq: s.m.modelId})
56
+ } else if (s.m.args.query) {
57
+ query = s.m.args.query.clone()
58
+ } else {
52
59
  throw new Error(`No model ID was given: ${s.m.modelId} by '${paramsVariableName}' in query params: ${Object.keys(s.props.match.params).join(", ")}`)
53
60
  }
54
61
 
@@ -58,8 +65,7 @@ const useModel = (modelClassArg, argsArg = {}) => {
58
65
 
59
66
  const model = await query.first()
60
67
 
61
- setModel(model)
62
- setNotFound(!model)
68
+ s.set({model, notFound: !model})
63
69
  }, [])
64
70
 
65
71
  const loadNewModel = useCallback(async () => {
@@ -79,13 +85,13 @@ const useModel = (modelClassArg, argsArg = {}) => {
79
85
  data: {a: modelData}
80
86
  })
81
87
 
82
- setModel(model)
88
+ s.set({model})
83
89
  }, [])
84
90
 
85
91
  const loadModel = useCallback(async () => {
86
92
  if (s.props.newIfNoId && !s.m.modelId) {
87
93
  return await loadNewModel()
88
- } else if (!s.props.optional || s.m.modelId) {
94
+ } else if (!s.props.optional || s.m.modelId | s.m.args.query) {
89
95
  return await loadExistingModel()
90
96
  }
91
97
  }, [])
@@ -112,14 +118,14 @@ const useModel = (modelClassArg, argsArg = {}) => {
112
118
  useLayoutEffect(() => {
113
119
  let connectUpdated
114
120
 
115
- if (model && args.eventUpdated) {
116
- connectUpdated = ModelEvents.connectUpdated(model, loadModel)
121
+ if (s.s.model && args.eventUpdated) {
122
+ connectUpdated = ModelEvents.connectUpdated(s.s.model, loadModel)
117
123
  }
118
124
 
119
125
  return () => {
120
126
  connectUpdated?.unsubscribe()
121
127
  }
122
- }, [args.eventUpdated, model?.id()])
128
+ }, [args.eventUpdated, s.s.model?.id()])
123
129
 
124
130
  const onSignedIn = useCallback(() => {
125
131
  loadModel()
@@ -150,20 +156,20 @@ const useModel = (modelClassArg, argsArg = {}) => {
150
156
  useLayoutEffect(() => {
151
157
  let connectDestroyed
152
158
 
153
- if (model && args.onDestroyed) {
154
- connectDestroyed = ModelEvents.connectDestroyed(model, onDestroyed)
159
+ if (s.s.model && args.onDestroyed) {
160
+ connectDestroyed = ModelEvents.connectDestroyed(s.s.model, onDestroyed)
155
161
  }
156
162
 
157
163
  return () => {
158
164
  connectDestroyed?.unsubscribe()
159
165
  }
160
- }, [args.onDestroyed, model?.id()])
166
+ }, [args.onDestroyed, s.s.model?.id()])
161
167
 
162
168
  const result = {}
163
169
 
164
- result[modelVariableName] = model
170
+ result[modelVariableName] = s.s.model
165
171
  result[`${modelVariableName}Id`] = modelId
166
- result[`${modelVariableName}NotFound`] = notFound
172
+ result[`${modelVariableName}NotFound`] = s.s.notFound
167
173
 
168
174
  return result
169
175
  }