@kaspernj/api-maker 1.0.302 → 1.0.304

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.304",
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,135 @@
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]
33
+ const [model, setModel] = useState(undefined)
34
+ const [notFound, setNotFound] = useState(undefined)
35
+
36
+ if (args.cacheArgs) {
37
+ cacheArgs.push(...args.cacheArgs)
38
+ }
39
+
40
+ const loadExistingModel = async () => {
41
+ const query = await modelClass.ransack({id_eq: modelId})
42
+
43
+ if (!modelId) throw new Error(`No model ID was given: ${modelId} by '${paramsVariableName}' in query params: ${Object.keys(match.params).join(", ")}`)
44
+ if (args.abilities) query.abilities(args.abilities)
45
+ if (args.preload) query.preload(args.preload)
46
+ if (args.select) query.select(args.select)
47
+
48
+ const model = await query.first()
49
+
50
+ setModel(model)
51
+ setNotFound(!model)
52
+ }
53
+
54
+ const loadNewModel = async () => {
55
+ const params = Params.parse()
56
+ const ModelClass = digg(this, "modelClass")
57
+ const paramKey = ModelClass.modelName().paramKey()
58
+ const modelDataFromParams = params[paramKey] || {}
59
+
60
+ let defaults = {}
61
+
62
+ if (this.args.newIfNoId?.defaults) {
63
+ defaults = await this.args.newIfNoId.defaults()
64
+ }
65
+
66
+ const modelData = Object.assign(defaults, this.args.newAttributes, modelDataFromParams)
67
+ const model = new ModelClass({
68
+ isNewRecord: true,
69
+ data: {a: modelData}
70
+ })
71
+
72
+ setModel(model)
73
+ }
74
+
75
+ const loadModel = async () => {
76
+ if (args.newIfNoId && !modelId) {
77
+ return await loadNewModel()
78
+ } else if (!args.optional || modelId) {
79
+ return await loadExistingModel()
80
+ }
81
+ }
82
+
83
+ useEffect(
84
+ () => { loadModel() },
85
+ cacheArgs
86
+ )
87
+
88
+ useEffect(() => {
89
+ let reloadModelCallback
90
+
91
+ if (args.events) {
92
+ reloadModelCallback = args.events.addListener("reloadModel", loadModel)
93
+ }
94
+
95
+ return () => {
96
+ if (reloadModelCallback) {
97
+ args.events.removeListener("reloadModel", loadModel)
98
+ }
99
+ }
100
+ }, [args.events])
101
+
102
+ useEffect(() => {
103
+ let connectUpdated
104
+
105
+ if (model && args.eventUpdated) {
106
+ connectUpdated = ModelEvents.connectUpdated(model, loadModel)
107
+ }
108
+
109
+ return () => {
110
+ connectUpdated?.unsubscribe()
111
+ }
112
+ }, [args.eventUpdated, model?.id()])
113
+
114
+ useEffect(() => {
115
+ let connectDestroyed
116
+
117
+ if (model && args.onDestroyed) {
118
+ connectDestroyed = ModelEvents.connectDestroyed(model, loadModel)
119
+ }
120
+
121
+ return () => {
122
+ connectDestroyed?.unsubscribe()
123
+ }
124
+ }, [args.onDestroyed, model?.id()])
125
+
126
+ const result = {}
127
+
128
+ result[modelVariableName] = model
129
+ result[`${modelVariableName}Id`] = modelId
130
+ result[`${modelVariableName}NotFound`] = notFound
131
+
132
+ return result
133
+ }
134
+
135
+ 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"