@kaspernj/api-maker 1.0.313 → 1.0.314

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.313",
19
+ "version": "1.0.314",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -3,7 +3,7 @@ import {useCallback, useEffect, useState} from "react"
3
3
  import useShape from "set-state-compare/src/use-shape.js"
4
4
 
5
5
  const useCanCan = (abilitiesCallback, dependencies) => {
6
- const shape = useShape({abilitiesCallback})
6
+ const s = useShape({abilitiesCallback})
7
7
  const [canCan, setCanCan] = useState()
8
8
 
9
9
  useEffect(() => {
@@ -12,7 +12,7 @@ const useCanCan = (abilitiesCallback, dependencies) => {
12
12
 
13
13
  const loadAbilities = useCallback(async () => {
14
14
  const canCan = CanCan.current()
15
- const abilities = shape.props.abilitiesCallback()
15
+ const abilities = s.p.abilitiesCallback()
16
16
 
17
17
  await canCan.loadAbilities(abilities)
18
18
 
package/src/use-model.mjs CHANGED
@@ -1,7 +1,8 @@
1
+ import {useCallback, useEffect, useState} from "react"
1
2
  import * as inflection from "inflection"
2
3
  import ModelEvents from "./model-events.mjs"
3
4
  import useQueryParams from "on-location-changed/src/use-query-params.js"
4
- import {useEffect, useState} from "react"
5
+ import useShape from "set-state-compare/src/use-shape.js"
5
6
 
6
7
  const useModel = (modelClassArg, argsArg = {}) => {
7
8
  const queryParams = useQueryParams()
@@ -13,6 +14,8 @@ const useModel = (modelClassArg, argsArg = {}) => {
13
14
  args = argsArg
14
15
  }
15
16
 
17
+ const s = useShape(args)
18
+
16
19
  if (typeof modelClassArg == "object") {
17
20
  modelClass = modelClassArg.callback({queryParams})
18
21
  } else {
@@ -20,18 +23,16 @@ const useModel = (modelClassArg, argsArg = {}) => {
20
23
  }
21
24
 
22
25
  const paramsVariableName = `${modelClass.modelName().paramKey()}_id`
26
+ let modelId
23
27
 
24
- const getModelId = () => {
25
- if (args.loadByQueryParam) {
26
- return args.loadByQueryParam({queryParams})
27
- }
28
-
28
+ if (args.loadByQueryParam) {
29
+ modelId = args.loadByQueryParam({queryParams})
30
+ } else {
29
31
  if (!args.match) throw new Error("Both 'loadByQueryParam' and 'match' wasn't given")
30
32
 
31
- return args.match.params[paramsVariableName] || args.match.params.id
33
+ modelId = args.match.params[paramsVariableName] || args.match.params.id
32
34
  }
33
35
 
34
- const modelId = getModelId()
35
36
  const modelVariableName = inflection.camelize(modelClass.modelClassData().name, true)
36
37
  const cacheArgs = [modelId]
37
38
  const [model, setModel] = useState(undefined)
@@ -41,48 +42,52 @@ const useModel = (modelClassArg, argsArg = {}) => {
41
42
  cacheArgs.push(...args.cacheArgs)
42
43
  }
43
44
 
44
- const loadExistingModel = async () => {
45
- const query = await modelClass.ransack({id_eq: modelId})
45
+ s.updateMeta({modelId, modelVariableName, queryParams})
46
+
47
+ const loadExistingModel = useCallback(async () => {
48
+ const query = await modelClass.ransack({id_eq: s.m.modelId})
49
+
50
+ if (!modelId) {
51
+ throw new Error(`No model ID was given: ${s.m.modelId} by '${paramsVariableName}' in query params: ${Object.keys(s.props.match.params).join(", ")}`)
52
+ }
46
53
 
47
- if (!modelId) throw new Error(`No model ID was given: ${modelId} by '${paramsVariableName}' in query params: ${Object.keys(match.params).join(", ")}`)
48
- if (args.abilities) query.abilities(args.abilities)
49
- if (args.preload) query.preload(args.preload)
50
- if (args.select) query.select(args.select)
54
+ if (s.props.abilities) query.abilities(s.p.abilities)
55
+ if (s.props.preload) query.preload(s.p.preload)
56
+ if (s.props.select) query.select(s.p.select)
51
57
 
52
58
  const model = await query.first()
53
59
 
54
60
  setModel(model)
55
61
  setNotFound(!model)
56
- }
62
+ }, [])
57
63
 
58
- const loadNewModel = async () => {
59
- const params = Params.parse()
64
+ const loadNewModel = useCallback(async () => {
60
65
  const ModelClass = modelClass
61
66
  const paramKey = ModelClass.modelName().paramKey()
62
- const modelDataFromParams = params[paramKey] || {}
67
+ const modelDataFromParams = s.m.queryParams[paramKey] || {}
63
68
 
64
69
  let defaults = {}
65
70
 
66
- if (args.newIfNoId?.defaults) {
67
- defaults = await args.newIfNoId.defaults()
71
+ if (s.props.newIfNoId?.defaults) {
72
+ defaults = await s.props.newIfNoId.defaults()
68
73
  }
69
74
 
70
- const modelData = Object.assign(defaults, args.newAttributes, modelDataFromParams)
75
+ const modelData = Object.assign(defaults, s.props.newAttributes, modelDataFromParams)
71
76
  const model = new ModelClass({
72
77
  isNewRecord: true,
73
78
  data: {a: modelData}
74
79
  })
75
80
 
76
81
  setModel(model)
77
- }
82
+ }, [])
78
83
 
79
- const loadModel = async () => {
80
- if (args.newIfNoId && !modelId) {
84
+ const loadModel = useCallback(async () => {
85
+ if (s.props.newIfNoId && !s.m.modelId) {
81
86
  return await loadNewModel()
82
- } else if (!args.optional || modelId) {
87
+ } else if (!s.props.optional || s.m.modelId) {
83
88
  return await loadExistingModel()
84
89
  }
85
- }
90
+ }, [])
86
91
 
87
92
  useEffect(
88
93
  () => { loadModel() },
@@ -115,11 +120,19 @@ const useModel = (modelClassArg, argsArg = {}) => {
115
120
  }
116
121
  }, [args.eventUpdated, model?.id()])
117
122
 
123
+ const onDestroyed = useCallback(({model}) => {
124
+ const forwardArgs = {model}
125
+
126
+ forwardArgs[s.m.modelVariableName] = model
127
+
128
+ s.p.onDestroyed(forwardArgs)
129
+ }, [])
130
+
118
131
  useEffect(() => {
119
132
  let connectDestroyed
120
133
 
121
134
  if (model && args.onDestroyed) {
122
- connectDestroyed = ModelEvents.connectDestroyed(model, loadModel)
135
+ connectDestroyed = ModelEvents.connectDestroyed(model, onDestroyed)
123
136
  }
124
137
 
125
138
  return () => {