@kaspernj/api-maker 1.0.383 → 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.
- package/package.json +1 -1
- package/src/table/use-breakpoint.mjs +2 -2
- package/src/use-can-can.mjs +2 -2
- package/src/use-collection.mjs +2 -2
- package/src/use-created-event.mjs +2 -2
- package/src/use-destroyed-event.mjs +2 -2
- package/src/use-event-emitter.mjs +2 -2
- package/src/use-event-listener.mjs +2 -2
- package/src/use-model.mjs +29 -23
- package/src/use-resize-observer.mjs +2 -2
- package/src/use-updated-event.mjs +2 -2
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useCallback,
|
|
1
|
+
import {useCallback, useLayoutEffect} from "react"
|
|
2
2
|
import apiMakerConfig from "@kaspernj/api-maker/src/config.mjs"
|
|
3
3
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
4
4
|
|
|
@@ -30,7 +30,7 @@ const useBreakpoint = () => {
|
|
|
30
30
|
breakpoint: () => calculateBreakPoint()
|
|
31
31
|
})
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
useLayoutEffect(() => {
|
|
34
34
|
window.addEventListener("resize", onCalled)
|
|
35
35
|
|
|
36
36
|
return () => {
|
package/src/use-can-can.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import CanCan from "./can-can.mjs"
|
|
2
|
-
import {useCallback, useMemo, useState} from "react"
|
|
2
|
+
import {useCallback, useLayoutEffect, useMemo, useState} from "react"
|
|
3
3
|
import useCurrentUser from "./use-current-user.mjs"
|
|
4
4
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
5
5
|
|
|
@@ -31,7 +31,7 @@ const useCanCan = (abilitiesCallback, dependencies) => {
|
|
|
31
31
|
loadAbilities()
|
|
32
32
|
}, dependencies)
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
useLayoutEffect(() => {
|
|
35
35
|
CanCan.current().events.addListener("onResetAbilities", onResetAbilities)
|
|
36
36
|
|
|
37
37
|
return () => {
|
package/src/use-collection.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import debounce from "debounce"
|
|
|
2
2
|
import {digg} from "diggerize"
|
|
3
3
|
import * as inflection from "inflection"
|
|
4
4
|
import ModelEvents from "./model-events.mjs"
|
|
5
|
-
import {useCallback, useMemo} from "react"
|
|
5
|
+
import {useCallback, useLayoutEffect, useMemo} from "react"
|
|
6
6
|
import useCreatedEvent from "./use-created-event.mjs"
|
|
7
7
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
8
8
|
import useQueryParams from "on-location-changed/src/use-query-params.js"
|
|
@@ -251,7 +251,7 @@ const useCollection = (props, cacheKeys = []) => {
|
|
|
251
251
|
|
|
252
252
|
useCreatedEvent(s.p.modelClass, onCreated)
|
|
253
253
|
|
|
254
|
-
|
|
254
|
+
useLayoutEffect(() => {
|
|
255
255
|
const connections = []
|
|
256
256
|
|
|
257
257
|
if (s.s.models) {
|
|
@@ -2,7 +2,7 @@ import debounceFunction from "debounce"
|
|
|
2
2
|
import ModelEvents from "./model-events.mjs"
|
|
3
3
|
import PropTypes from "prop-types"
|
|
4
4
|
import propTypesExact from "prop-types-exact"
|
|
5
|
-
import {useCallback,
|
|
5
|
+
import {useCallback, useLayoutEffect} from "react"
|
|
6
6
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
7
7
|
|
|
8
8
|
const ApiMakerUseCreatedEvent = (modelClass, onCreated, args = {}) => {
|
|
@@ -33,7 +33,7 @@ const ApiMakerUseCreatedEvent = (modelClass, onCreated, args = {}) => {
|
|
|
33
33
|
}
|
|
34
34
|
}, [])
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
useLayoutEffect(() => {
|
|
37
37
|
const connectCreated = ModelEvents.connectCreated(s.p.modelClass, (...args) => onCreatedCallback(...args))
|
|
38
38
|
|
|
39
39
|
return () => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useCallback, useMemo} from "react"
|
|
1
|
+
import {useCallback, useLayoutEffect, useMemo} from "react"
|
|
2
2
|
import debounceFunction from "debounce"
|
|
3
3
|
import ModelEvents from "./model-events.mjs"
|
|
4
4
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
@@ -34,7 +34,7 @@ const apiMakerUseDestroyedEvent = (model, onDestroyed, props) => {
|
|
|
34
34
|
}
|
|
35
35
|
}, [])
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
useLayoutEffect(() => {
|
|
38
38
|
let connectDestroyed, onConnectedListener
|
|
39
39
|
|
|
40
40
|
if (model) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {useCallback,
|
|
1
|
+
import {useCallback, useLayoutEffect} from "react"
|
|
2
2
|
|
|
3
3
|
const ApiMakerUseEventListener = (target, event, onCalled) => {
|
|
4
4
|
const onCalledCallback = useCallback((...args) => {
|
|
5
5
|
onCalled.apply(null, args)
|
|
6
6
|
}, [target, event, onCalled])
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
useLayoutEffect(() => {
|
|
9
9
|
if (target) {
|
|
10
10
|
target.addEventListener(event, onCalledCallback)
|
|
11
11
|
|
package/src/use-model.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useCallback, useMemo, useState} from "react"
|
|
1
|
+
import {useCallback, useLayoutEffect, useMemo, useState} from "react"
|
|
2
2
|
import Devise from "./devise.mjs"
|
|
3
3
|
import * as inflection from "inflection"
|
|
4
4
|
import ModelEvents from "./model-events.mjs"
|
|
@@ -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
|
-
|
|
52
|
+
let query
|
|
50
53
|
|
|
51
|
-
if (
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}, [])
|
|
@@ -95,7 +101,7 @@ const useModel = (modelClassArg, argsArg = {}) => {
|
|
|
95
101
|
cacheArgs
|
|
96
102
|
)
|
|
97
103
|
|
|
98
|
-
|
|
104
|
+
useLayoutEffect(() => {
|
|
99
105
|
let reloadModelCallback
|
|
100
106
|
|
|
101
107
|
if (args.events) {
|
|
@@ -109,17 +115,17 @@ const useModel = (modelClassArg, argsArg = {}) => {
|
|
|
109
115
|
}
|
|
110
116
|
}, [args.events])
|
|
111
117
|
|
|
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()
|
|
@@ -129,7 +135,7 @@ const useModel = (modelClassArg, argsArg = {}) => {
|
|
|
129
135
|
loadModel()
|
|
130
136
|
}, [])
|
|
131
137
|
|
|
132
|
-
|
|
138
|
+
useLayoutEffect(() => {
|
|
133
139
|
Devise.events().addListener("onDeviseSignIn", onSignedIn)
|
|
134
140
|
Devise.events().addListener("onDeviseSignOut", onSignedOut)
|
|
135
141
|
|
|
@@ -147,23 +153,23 @@ const useModel = (modelClassArg, argsArg = {}) => {
|
|
|
147
153
|
s.p.onDestroyed(forwardArgs)
|
|
148
154
|
}, [])
|
|
149
155
|
|
|
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
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useCallback, useMemo} from "react"
|
|
1
|
+
import {useCallback, useLayoutEffect, useMemo} from "react"
|
|
2
2
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
3
3
|
|
|
4
4
|
const useResizeObserver = (element, callback) => {
|
|
@@ -8,7 +8,7 @@ const useResizeObserver = (element, callback) => {
|
|
|
8
8
|
}, [])
|
|
9
9
|
const observer = useMemo(() => new ResizeObserver(onResize), [])
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
useLayoutEffect(() => {
|
|
12
12
|
if (element) {
|
|
13
13
|
observer.observe(element)
|
|
14
14
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useCallback, useMemo} from "react"
|
|
1
|
+
import {useCallback, useLayoutEffect, useMemo} from "react"
|
|
2
2
|
import debounceFunction from "debounce"
|
|
3
3
|
import ModelEvents from "./model-events.mjs"
|
|
4
4
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
@@ -32,7 +32,7 @@ const apiMakerUseUpdatedEvent = (model, onUpdated, {active = true, debounce, onC
|
|
|
32
32
|
}
|
|
33
33
|
}, [])
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
useLayoutEffect(() => {
|
|
36
36
|
let connectUpdated, onConnectedListener
|
|
37
37
|
|
|
38
38
|
if (model) {
|