@kaspernj/api-maker 1.0.332 → 1.0.334
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/use-current-user.mjs +23 -26
package/package.json
CHANGED
package/src/use-current-user.mjs
CHANGED
|
@@ -2,25 +2,35 @@ import {useCallback, useEffect, useMemo} from "react"
|
|
|
2
2
|
import {camelize} from "inflection"
|
|
3
3
|
import Devise from "./devise.mjs"
|
|
4
4
|
import Services from "./services.mjs"
|
|
5
|
+
import useEventEmitter from "./use-event-emitter.mjs"
|
|
5
6
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
6
7
|
|
|
7
|
-
const currentUserData = {}
|
|
8
|
-
|
|
9
8
|
const useCurrentUser = (args) => {
|
|
10
|
-
const s = useShape()
|
|
9
|
+
const s = useShape(args || {})
|
|
11
10
|
const scope = args?.scope || "user"
|
|
12
|
-
const scopeName =
|
|
11
|
+
const scopeName = `current${camelize(scope)}`
|
|
13
12
|
|
|
14
13
|
s.meta.scope = scope
|
|
15
14
|
s.meta.scopeName = scopeName
|
|
16
15
|
|
|
16
|
+
const debugs = useCallback((debugCallback) => {
|
|
17
|
+
if (s.props.debug) {
|
|
18
|
+
let debugArgs = debugCallback()
|
|
19
|
+
|
|
20
|
+
if (!Array.isArray(debugArgs)) debugArgs = [debugArgs]
|
|
21
|
+
|
|
22
|
+
console.log("useCurrentUser", ...debugArgs)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
|
|
17
26
|
const loadCurrentUserFromRequest = useCallback(async () => {
|
|
18
27
|
const {scope, scopeName} = s.m
|
|
28
|
+
|
|
29
|
+
debugs(() => `Loading ${scope} with request`)
|
|
30
|
+
|
|
19
31
|
const result = await Services.current().sendRequest("Devise::Current", {scope})
|
|
20
32
|
const current = digg(result, "current")
|
|
21
33
|
|
|
22
|
-
currentUserData[scope] = current
|
|
23
|
-
|
|
24
34
|
if (!(scopeName in s.setStates)) throw new Error(`'${scopeName}' not found in setStates`)
|
|
25
35
|
|
|
26
36
|
s.setStates[scopeName](current)
|
|
@@ -29,12 +39,10 @@ const useCurrentUser = (args) => {
|
|
|
29
39
|
const defaultCurrentUser = useCallback(() => {
|
|
30
40
|
const {scope, scopeName} = s.m
|
|
31
41
|
|
|
32
|
-
if (scope
|
|
33
|
-
|
|
34
|
-
} else if (Devise.current().hasCurrentScope(scope)) {
|
|
35
|
-
currentUserData[scope] = Devise[scopeName]()
|
|
42
|
+
if (Devise.current().hasCurrentScope(scope)) {
|
|
43
|
+
debugs(() => `Setting ${scope} from current scope`)
|
|
36
44
|
|
|
37
|
-
return
|
|
45
|
+
return Devise[scopeName]()
|
|
38
46
|
}
|
|
39
47
|
}, [])
|
|
40
48
|
|
|
@@ -53,26 +61,15 @@ const useCurrentUser = (args) => {
|
|
|
53
61
|
}, [])
|
|
54
62
|
|
|
55
63
|
useEffect(() => {
|
|
56
|
-
if (!(s.m.scope
|
|
64
|
+
if (!Devise.current().hasCurrentScope(s.m.scope)) {
|
|
57
65
|
loadCurrentUserFromRequest()
|
|
58
66
|
}
|
|
59
67
|
}, [])
|
|
60
68
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
return () => {
|
|
65
|
-
Devise.events().removeListener("onDeviseSignIn", updateCurrentUser)
|
|
66
|
-
}
|
|
67
|
-
}, [])
|
|
69
|
+
useEventEmitter(Devise.events(), "onDeviseSignIn", updateCurrentUser)
|
|
70
|
+
useEventEmitter(Devise.events(), "onDeviseSignOut", updateCurrentUser)
|
|
68
71
|
|
|
69
|
-
|
|
70
|
-
Devise.events().addListener("onDeviseSignOut", updateCurrentUser)
|
|
71
|
-
|
|
72
|
-
return () => {
|
|
73
|
-
Devise.events().removeListener("onDeviseSignOut", updateCurrentUser)
|
|
74
|
-
}
|
|
75
|
-
}, [])
|
|
72
|
+
console.error(`DEBUG: Keys: ${Object.keys(s.s).map((key) => `${key}: ${s.s[key]?.id()}`).join(", ")}`)
|
|
76
73
|
|
|
77
74
|
return s.s[scopeName]
|
|
78
75
|
}
|