@kaspernj/api-maker 1.0.302 → 1.0.303

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.302",
19
+ "version": "1.0.303",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -49,7 +49,7 @@
49
49
  "js-money": ">= 0.6.3",
50
50
  "numberable": ">= 1.0.0",
51
51
  "object-to-formdata": ">= 4.1.0",
52
- "on-location-changed": ">= 1.0.7",
52
+ "on-location-changed": ">= 1.0.8",
53
53
  "qs": ">= 6.9.3",
54
54
  "replaceall": ">= 0.1.6",
55
55
  "spark-md5": "^3.0.2",
@@ -0,0 +1,29 @@
1
+ import {useCallback, useEffect} from "react"
2
+ import Devise from "./devise.mjs"
3
+
4
+ const useCurrentUser = () => {
5
+ const [currentUser, setCurrentUser] = useState(Devise.currentUser())
6
+ const updateCurrentUser = useCallback(() => {
7
+ setCurrentUser(Devise.currentUser())
8
+ })
9
+
10
+ useEffect(() => {
11
+ Devise.events().addListener("onDeviseSignIn", updateCurrentUser)
12
+
13
+ return () => {
14
+ Devise.events().removeListener("onDeviseSignIn", updateCurrentUser)
15
+ }
16
+ })
17
+
18
+ useEffect(() => {
19
+ Devise.events().addListener("onDeviseSignOut", updateCurrentUser)
20
+
21
+ return () => {
22
+ Devise.events().removeListener("onDeviseSignOut", updateCurrentUser)
23
+ }
24
+ })
25
+
26
+ return currentUser
27
+ }
28
+
29
+ export default useCurrentUser
@@ -0,0 +1,131 @@
1
+ import ModelEvents from "./model-events.mjs"
2
+ import useQueryParams from "on-location-changed/src/use-query-params.js"
3
+
4
+ const useModel = (match, modelClassArg, argsArg) => {
5
+ const queryParams = useQueryParams()
6
+ let args, modelClass
7
+
8
+ if (typeof argsArg == "function") {
9
+ args = argsArg({modelClass})
10
+ } else {
11
+ args = argsArg
12
+ }
13
+
14
+ if (typeof modelClassArg == "object") {
15
+ modelClass = modelClassArg.callback({queryParams})
16
+ } else {
17
+ modelClass = modelClassArg
18
+ }
19
+
20
+ const paramsVariableName = `${modelClass.modelName().paramKey()}_id`
21
+
22
+ const getModelId = () => {
23
+ if (args.loadByQueryParam) {
24
+ return args.loadByQueryParam({queryParams})
25
+ }
26
+
27
+ return match.params[paramsVariableName] || match.params.id
28
+ }
29
+
30
+ const modelId = getModelId()
31
+ const modelVariableName = inflection.camelize(modelClass.modelClassData().name, true)
32
+ const cacheArgs = [modelId, ...args.cacheArgs]
33
+ const [model, setModel] = useState(undefined)
34
+ const [notFound, setNotFound] = useState(undefined)
35
+
36
+ const loadExistingModel = async () => {
37
+ const query = await modelClass.ransack({id_eq: modelId})
38
+
39
+ if (!modelId) throw new Error(`No model ID was given: ${modelId} by '${paramsVariableName}' in query params: ${Object.keys(match.params).join(", ")}`)
40
+ if (args.abilities) query.abilities(args.abilities)
41
+ if (args.preload) query.preload(args.preload)
42
+ if (args.select) query.select(args.select)
43
+
44
+ const model = await query.first()
45
+
46
+ setModel(model)
47
+ setNotFound(!model)
48
+ }
49
+
50
+ const loadNewModel = async () => {
51
+ const params = Params.parse()
52
+ const ModelClass = digg(this, "modelClass")
53
+ const paramKey = ModelClass.modelName().paramKey()
54
+ const modelDataFromParams = params[paramKey] || {}
55
+
56
+ let defaults = {}
57
+
58
+ if (this.args.newIfNoId?.defaults) {
59
+ defaults = await this.args.newIfNoId.defaults()
60
+ }
61
+
62
+ const modelData = Object.assign(defaults, this.args.newAttributes, modelDataFromParams)
63
+ const model = new ModelClass({
64
+ isNewRecord: true,
65
+ data: {a: modelData}
66
+ })
67
+
68
+ setModel(model)
69
+ }
70
+
71
+ const loadModel = async () => {
72
+ if (args.newIfNoId && !modelId) {
73
+ return await loadNewModel()
74
+ } else if (!args.optional || modelId) {
75
+ return await loadExistingModel()
76
+ }
77
+ }
78
+
79
+ useEffect(
80
+ () => { loadModel() },
81
+ cacheArgs
82
+ )
83
+
84
+ useEffect(() => {
85
+ let reloadModelCallback
86
+
87
+ if (args.events) {
88
+ reloadModelCallback = args.events.addListener("reloadModel", loadModel)
89
+ }
90
+
91
+ return () => {
92
+ if (reloadModelCallback) {
93
+ args.events.removeListener("reloadModel", loadModel)
94
+ }
95
+ }
96
+ }, [args.events])
97
+
98
+ useEffect(() => {
99
+ let connectUpdated
100
+
101
+ if (model && args.eventUpdated) {
102
+ connectUpdated = ModelEvents.connectUpdated(model, loadModel)
103
+ }
104
+
105
+ return () => {
106
+ connectUpdated?.unsubscribe()
107
+ }
108
+ }, [args.eventUpdated, model?.id()])
109
+
110
+ useEffect(() => {
111
+ let connectDestroyed
112
+
113
+ if (model && args.onDestroyed) {
114
+ connectDestroyed = ModelEvents.connectDestroyed(model, loadModel)
115
+ }
116
+
117
+ return () => {
118
+ connectDestroyed?.unsubscribe()
119
+ }
120
+ }, [args.onDestroyed, model?.id()])
121
+
122
+ const result = {}
123
+
124
+ result[modelVariableName] = model
125
+ result[`${modelVariableName}Id`] = modelId
126
+ result[`${modelVariableName}NotFound`] = notFound
127
+
128
+ return result
129
+ }
130
+
131
+ export default useModel
@@ -1,5 +1,6 @@
1
1
  import {digg, digs} from "diggerize"
2
2
  import EventUpdated from "./event-updated"
3
+ import * as inflection from "inflection"
3
4
  import Params from "./params.mjs"
4
5
  import PropTypes from "prop-types"
5
6
  import React from "react"