@kaspernj/api-maker 1.0.332 → 1.0.333
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 +17 -2
package/package.json
CHANGED
package/src/use-current-user.mjs
CHANGED
|
@@ -7,15 +7,28 @@ import useShape from "set-state-compare/src/use-shape.js"
|
|
|
7
7
|
const currentUserData = {}
|
|
8
8
|
|
|
9
9
|
const useCurrentUser = (args) => {
|
|
10
|
-
const s = useShape()
|
|
10
|
+
const s = useShape(args || {})
|
|
11
11
|
const scope = args?.scope || "user"
|
|
12
12
|
const scopeName = useMemo(() => `current${camelize(scope)}`, [scope])
|
|
13
13
|
|
|
14
14
|
s.meta.scope = scope
|
|
15
15
|
s.meta.scopeName = scopeName
|
|
16
16
|
|
|
17
|
+
const debugs = useCallback((debugCallback) => {
|
|
18
|
+
if (s.props.debug) {
|
|
19
|
+
let debugArgs = debugCallback()
|
|
20
|
+
|
|
21
|
+
if (!Array.isArray(debugArgs)) debugArgs = [debugArgs]
|
|
22
|
+
|
|
23
|
+
console.log("useCurrentUser", ...debugArgs)
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
17
27
|
const loadCurrentUserFromRequest = useCallback(async () => {
|
|
18
28
|
const {scope, scopeName} = s.m
|
|
29
|
+
|
|
30
|
+
debugs(() => `Loading ${scope} with request`)
|
|
31
|
+
|
|
19
32
|
const result = await Services.current().sendRequest("Devise::Current", {scope})
|
|
20
33
|
const current = digg(result, "current")
|
|
21
34
|
|
|
@@ -32,6 +45,8 @@ const useCurrentUser = (args) => {
|
|
|
32
45
|
if (scope in currentUserData) {
|
|
33
46
|
return currentUserData[scope]
|
|
34
47
|
} else if (Devise.current().hasCurrentScope(scope)) {
|
|
48
|
+
debugs(() => `Setting ${scope} from current scope`)
|
|
49
|
+
|
|
35
50
|
currentUserData[scope] = Devise[scopeName]()
|
|
36
51
|
|
|
37
52
|
return currentUserData[scope]
|
|
@@ -53,7 +68,7 @@ const useCurrentUser = (args) => {
|
|
|
53
68
|
}, [])
|
|
54
69
|
|
|
55
70
|
useEffect(() => {
|
|
56
|
-
if (!(s.m.scope in currentUserData)) {
|
|
71
|
+
if (!(s.m.scope in currentUserData) && !Devise.current().hasCurrentScope(s.m.scope)) {
|
|
57
72
|
loadCurrentUserFromRequest()
|
|
58
73
|
}
|
|
59
74
|
}, [])
|