@kaspernj/api-maker 1.0.316 → 1.0.317

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.316",
19
+ "version": "1.0.317",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -7,21 +7,8 @@ export default class ApiMakerBaseModelReflection {
7
7
  this.reflectionData = reflectionData
8
8
  }
9
9
 
10
- foreignKey() {
11
- return digg(this, "reflectionData", "foreignKey")
12
- }
13
-
14
- macro() {
15
- return digg(this, "reflectionData", "macro")
16
- }
17
-
18
- modelClass() {
19
- const modelClass = modelClassRequire(inflection.singularize(inflection.camelize(digg(this, "reflectionData", "resource_name"))))
20
-
21
- return modelClass
22
- }
23
-
24
- name() {
25
- return inflection.camelize(digg(this, "reflectionData", "name"), true)
26
- }
10
+ foreignKey = () => digg(this, "reflectionData", "foreignKey")
11
+ macro = () => digg(this, "reflectionData", "macro")
12
+ modelClass = () => modelClassRequire(inflection.singularize(inflection.camelize(digg(this, "reflectionData", "resource_name"))))
13
+ name = () => inflection.camelize(digg(this, "reflectionData", "name"), true)
27
14
  }
@@ -9,6 +9,10 @@ import useModel from "../use-model"
9
9
  import useQueryParams from "on-location-changed/src/use-query-params"
10
10
 
11
11
  const EditAttributeInput = ({attributeName, id, inputs, label, model, name}) => {
12
+ if (!(attributeName in model)) {
13
+ throw new Error(`${attributeName} isn't set on the resource ${model.modelClassData().name}`)
14
+ }
15
+
12
16
  const defaultValue = useCallback(() => model[attributeName]() || "")
13
17
  const [value, setValue] = useState(() => defaultValue())
14
18
 
@@ -41,6 +45,10 @@ const EditAttributeInput = ({attributeName, id, inputs, label, model, name}) =>
41
45
  }
42
46
 
43
47
  const EditAttributeContent = ({attribute, id, inputs, model, name}) => {
48
+ if (!(attribute.attribute in model)) {
49
+ throw new Error(`${attribute.attribute} isn't set on the resource ${model.modelClassData().name}`)
50
+ }
51
+
44
52
  const defaultValue = useCallback(() => model[attribute.attribute]() || "")
45
53
  const [value, setValue] = useState(() => defaultValue())
46
54
  const onChangeValue = useCallback((newValue) => {
@@ -6,6 +6,7 @@ import {memo, useMemo} from "react"
6
6
  import * as modelsModule from "@kaspernj/api-maker/src/models.mjs.erb"
7
7
  import {useCallback, useEffect, useState} from "react"
8
8
  import ShowPage from "./show-page"
9
+ import ShowReflectionActions from "./show-reflection-actions"
9
10
  import ShowReflectionPage from "./show-reflection-page"
10
11
  import useQueryParams from "on-location-changed/src/use-query-params"
11
12
 
@@ -90,8 +91,11 @@ const ApiMakerSuperAdmin = () => {
90
91
  }
91
92
  </>
92
93
  }
94
+ {pageToShow == "show_reflection" &&
95
+ <ShowReflectionActions model={model} modelClass={modelClass} reflectionName={queryParams.model_reflection} />
96
+ }
93
97
  </>,
94
- [model, pageToShow]
98
+ [model, modelClass, pageToShow]
95
99
  )
96
100
 
97
101
  return (
@@ -0,0 +1,26 @@
1
+ import {digg} from "diggerize"
2
+ import {memo} from "react"
3
+
4
+ const SuperAdminShowReflectionActions = ({model, modelClass, reflectionName}) => {
5
+ const reflection = modelClass.reflections().find((reflection) => reflection.name() == reflectionName)
6
+ const modelClassName = digg(reflection, "reflectionData", "className")
7
+ const modelData = {}
8
+ const dataParamName = inflection.singularize(reflection.reflectionData.collectionName)
9
+
10
+ modelData[reflection.foreignKey()] = model?.id()
11
+
12
+ const linkParams = {
13
+ model: modelClassName,
14
+ mode: "new"
15
+ }
16
+
17
+ linkParams[dataParamName] = modelData
18
+
19
+ return (
20
+ <Link className="create-new-model-link" to={Params.withParams(linkParams)}>
21
+ Create new
22
+ </Link>
23
+ )
24
+ }
25
+
26
+ export default memo(SuperAdminShowReflectionActions)
@@ -9,7 +9,8 @@ import withModel from "../with-model"
9
9
  const ApiMakerSuperAdminShowReflectionPage = ({modelClass, restProps}) => {
10
10
  const queryParams = useQueryParams()
11
11
  const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
12
- const model = digg(restProps, camelizedLower)
12
+ const useModelResult = useModel(modelClass, {loadByQueryParam: ({queryParams}) => digg(queryParams, "model_id")})
13
+ const model = digg(useModelResult, camelizedLower)
13
14
  const reflections = modelClass.reflections()
14
15
  const reflection = reflections.find((reflectionI) => reflectionI.name() == queryParams.model_reflection)
15
16
  const reflectionModelClass = reflection.modelClass()
@@ -37,17 +38,4 @@ ApiMakerSuperAdminShowReflectionPage.propTypes = {
37
38
  modelClass: PropTypes.func.isRequired
38
39
  }
39
40
 
40
- const modelClassResolver = {callback: ({queryParams}) => {
41
- const modelClassName = digg(queryParams, "model")
42
- const modelClass = digg(require("../models.mjs.erb"), modelClassName)
43
-
44
- return modelClass
45
- }}
46
-
47
- export default withModel(
48
- memo(ApiMakerSuperAdminShowReflectionPage),
49
- modelClassResolver,
50
- {
51
- loadByQueryParam: ({props}) => props.queryParams.model_id
52
- }
53
- )
41
+ export default memo(ApiMakerSuperAdminShowReflectionPage)