@kaspernj/api-maker 1.0.400 → 1.0.401

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaspernj/api-maker",
3
- "version": "1.0.400",
3
+ "version": "1.0.401",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -20,13 +20,14 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
20
20
  }
21
21
 
22
22
  static propTypes = propTypesExact({
23
+ children: PropTypes.node,
23
24
  component: PropTypes.string,
24
25
  componentPath: PropTypes.string,
25
26
  exact: PropTypes.bool.isRequired,
26
27
  fallback: PropTypes.bool.isRequired,
27
28
  includeInPath: PropTypes.bool.isRequired,
28
29
  onMatch: PropTypes.func,
29
- path: PropTypes.string
30
+ path: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(RegExp)])
30
31
  })
31
32
 
32
33
  match = null
@@ -39,17 +40,14 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
39
40
  const givenRoute = useContext(RouteContext)
40
41
  const {pathShown} = switchGroup.s
41
42
 
43
+ this.debug = false
44
+ this.log(() => ({givenRoute}))
45
+
42
46
  this.requireComponent = useContext(RequireComponentContext)
43
47
  this.currentParams = useContext(ParamsContext)
44
48
  this.currentPath = useContext(CurrentPathContext)
45
49
  this.switchGroup = switchGroup
46
-
47
- this.routeParts = useMemo(() => {
48
- let routeParts = givenRoute?.split("/")
49
-
50
- return routeParts
51
- }, [path, givenRoute])
52
-
50
+ this.routeParts = useMemo(() => givenRoute?.split("/"), [path, givenRoute])
53
51
  this.pathParts = useMemo(() => path?.split("/"), [path])
54
52
 
55
53
  this.newRouteParts = useMemo(
@@ -79,7 +77,9 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
79
77
  this.props.onMatch()
80
78
  }
81
79
 
82
- this.loadComponent()
80
+ if ((!this.props.children && this.props.path) || this.props.component || this.props.componentPath) {
81
+ this.loadComponent()
82
+ }
83
83
  }
84
84
  }, [path, pathShown, this.s.matches])
85
85
  }
@@ -111,11 +111,14 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
111
111
  const params = {}
112
112
  const componentPathParts = [...this.currentPath]
113
113
 
114
+ this.log(() => [this.props.path, "Start generating component paths", JSON.stringify(componentPathParts)])
115
+
114
116
  for (const pathPartIndex in this.pathParts) {
115
117
  const pathPart = this.pathParts[pathPartIndex]
116
118
  const translatedPathPart = I18n.t(`routes.${pathPart}`, {defaultValue: pathPart})
117
119
 
118
120
  if (!(pathPartIndex in this.routeParts)) {
121
+ this.log(() => `No match for: ${pathPartIndex}`)
119
122
  matches = false
120
123
  break
121
124
  }
@@ -135,6 +138,7 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
135
138
  }
136
139
 
137
140
  if (exact && newRouteParts.length > 0) {
141
+ this.log(() => ["Exact and more route parts", {newRouteParts, pathParts: this.pathParts, routeParts: this.routeParts}])
138
142
  matches = false
139
143
  } else if (matches && path) {
140
144
  matches = true
@@ -148,6 +152,8 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
148
152
  matches = true
149
153
  }
150
154
 
155
+ this.log(() => [this.props.path, "End generating component paths", JSON.stringify(componentPathParts), {matches}])
156
+
151
157
  if (matches) {
152
158
  if (component && includeInPath) {
153
159
  componentPathParts.push(component)
@@ -169,6 +175,8 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
169
175
  const actualComponentPath = this.props.componentPath || this.tt.componentPathParts.join("/")
170
176
  let Component
171
177
 
178
+ this.log(() => ["loadComponent", {componentPath: this.props.componentPath, componentPathParts: this.componentPathParts, actualComponentPath}])
179
+
172
180
  try {
173
181
  const componentImport = await this.tt.requireComponent({routeDefinition: {component: actualComponentPath}})
174
182
 
@@ -182,9 +190,19 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
182
190
  this.setState({Component, componentNotFound: !Component})
183
191
  }
184
192
 
193
+ log(callbackArgs) {
194
+ if (this.debug) {
195
+ let args = callbackArgs()
196
+
197
+ if (!Array.isArray(args)) args = [args]
198
+
199
+ console.log(...args)
200
+ }
201
+ }
202
+
185
203
  render() {
186
204
  const {componentPathParts, match, newParams, newRouteParts} = this.tt
187
- const {component, path} = this.props
205
+ const {children, component, path} = this.props
188
206
  const {Component, componentNotFound, matches} = this.s
189
207
 
190
208
  if (!matches || !this.hasSwitchMatch()) {
@@ -192,7 +210,7 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
192
210
  return null
193
211
  }
194
212
 
195
- if (!Component && !componentNotFound) {
213
+ if (!Component && !children && !componentNotFound) {
196
214
  // Route is matching but hasn't been loaded yet.
197
215
  return (
198
216
  <div>
@@ -201,7 +219,7 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
201
219
  )
202
220
  }
203
221
 
204
- if (!Component && componentNotFound) {
222
+ if (!Component && !children && componentNotFound) {
205
223
  // Don't render anything if the component couldn't be found.
206
224
  return null
207
225
  }
@@ -211,7 +229,8 @@ const Route = memo(shapeComponent(class Route extends BaseComponent {
211
229
  <RouteContext.Provider value={newRouteParts.join("/")}>
212
230
  <ParamsContext.Provider value={newParams}>
213
231
  <Switch name={`route-group-${path}`} single={false}>
214
- <Component match={match} />
232
+ {Component && <Component match={match} />}
233
+ {children}
215
234
  </Switch>
216
235
  </ParamsContext.Provider>
217
236
  </RouteContext.Provider>
package/src/use-model.mjs CHANGED
@@ -42,12 +42,6 @@ const useModel = (modelClassArg, argsArg = {}) => {
42
42
  const modelVariableName = inflection.camelize(modelClass.modelClassData().name, true)
43
43
  const cacheArgs = [modelId]
44
44
 
45
- if (args.cacheArgs) {
46
- cacheArgs.push(...args.cacheArgs)
47
- }
48
-
49
- s.updateMeta({args, modelId, modelVariableName, queryParams})
50
-
51
45
  const loadExistingModel = useCallback(async () => {
52
46
  let query
53
47
 
@@ -89,13 +83,37 @@ const useModel = (modelClassArg, argsArg = {}) => {
89
83
  }, [])
90
84
 
91
85
  const loadModel = useCallback(async () => {
92
- if (s.props.newIfNoId && !s.m.modelId) {
86
+ if ("active" in s.props && !s.props.active) {
87
+ // Not active - don't do anything
88
+ } else if (s.props.newIfNoId && !s.m.modelId) {
93
89
  return await loadNewModel()
94
90
  } else if (!s.props.optional || s.m.modelId | s.m.args.query) {
95
91
  return await loadExistingModel()
96
92
  }
97
93
  }, [])
98
94
 
95
+ const onDestroyed = useCallback(({model}) => {
96
+ const forwardArgs = {model}
97
+
98
+ forwardArgs[s.m.modelVariableName] = model
99
+
100
+ s.p.onDestroyed(forwardArgs)
101
+ }, [])
102
+
103
+ const onSignedIn = useCallback(() => {
104
+ loadModel()
105
+ }, [])
106
+
107
+ const onSignedOut = useCallback(() => {
108
+ loadModel()
109
+ }, [])
110
+
111
+ if (args.cacheArgs) {
112
+ cacheArgs.push(...args.cacheArgs)
113
+ }
114
+
115
+ s.updateMeta({args, modelId, modelVariableName, queryParams})
116
+
99
117
  useMemo(
100
118
  () => { loadModel() },
101
119
  cacheArgs
@@ -127,14 +145,6 @@ const useModel = (modelClassArg, argsArg = {}) => {
127
145
  }
128
146
  }, [args.eventUpdated, s.s.model?.id()])
129
147
 
130
- const onSignedIn = useCallback(() => {
131
- loadModel()
132
- }, [])
133
-
134
- const onSignedOut = useCallback(() => {
135
- loadModel()
136
- }, [])
137
-
138
148
  useLayoutEffect(() => {
139
149
  Devise.events().addListener("onDeviseSignIn", onSignedIn)
140
150
  Devise.events().addListener("onDeviseSignOut", onSignedOut)
@@ -145,14 +155,6 @@ const useModel = (modelClassArg, argsArg = {}) => {
145
155
  }
146
156
  })
147
157
 
148
- const onDestroyed = useCallback(({model}) => {
149
- const forwardArgs = {model}
150
-
151
- forwardArgs[s.m.modelVariableName] = model
152
-
153
- s.p.onDestroyed(forwardArgs)
154
- }, [])
155
-
156
158
  useLayoutEffect(() => {
157
159
  let connectDestroyed
158
160