@kaspernj/api-maker 1.0.316 → 1.0.318

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.318",
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
  }
@@ -8,7 +8,11 @@ import useCurrentUser from "../use-current-user"
8
8
  import useModel from "../use-model"
9
9
  import useQueryParams from "on-location-changed/src/use-query-params"
10
10
 
11
- const EditAttributeInput = ({attributeName, id, inputs, label, model, name}) => {
11
+ const EditAttributeInput = memo(({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
 
@@ -38,9 +42,13 @@ const EditAttributeInput = ({attributeName, id, inputs, label, model, name}) =>
38
42
  </View>
39
43
  </View>
40
44
  )
41
- }
45
+ })
46
+
47
+ const EditAttributeContent = memo(({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
+ }
42
51
 
43
- const EditAttributeContent = ({attribute, id, inputs, model, name}) => {
44
52
  const defaultValue = useCallback(() => model[attribute.attribute]() || "")
45
53
  const [value, setValue] = useState(() => defaultValue())
46
54
  const onChangeValue = useCallback((newValue) => {
@@ -62,9 +70,9 @@ const EditAttributeContent = ({attribute, id, inputs, model, name}) => {
62
70
  })
63
71
 
64
72
  return attribute.content(contentArgs())
65
- }
73
+ })
66
74
 
67
- const EditAttribute = ({attribute, inputs, model, modelClass}) => {
75
+ const EditAttribute = memo(({attribute, inputs, model, modelClass}) => {
68
76
  const availableLocales = Locales.availableLocales()
69
77
  const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
70
78
 
@@ -73,7 +81,7 @@ const EditAttribute = ({attribute, inputs, model, modelClass}) => {
73
81
  {attribute.content &&
74
82
  <EditAttributeContent
75
83
  attribute={attribute}
76
- id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}`}
84
+ id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
77
85
  inputs={inputs}
78
86
  model={model}
79
87
  name={inflection.underscore(attribute.attribute)}
@@ -82,7 +90,7 @@ const EditAttribute = ({attribute, inputs, model, modelClass}) => {
82
90
  {!attribute.content && attribute.translated && availableLocales.map((locale) =>
83
91
  <EditAttributeInput
84
92
  attributeName={`${attribute.attribute}${inflection.camelize(locale)}`}
85
- id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}_${locale}`}
93
+ id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}_${locale}`}
86
94
  inputs={inputs}
87
95
  label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
88
96
  model={model}
@@ -93,7 +101,7 @@ const EditAttribute = ({attribute, inputs, model, modelClass}) => {
93
101
  {!attribute.content && !attribute.translated &&
94
102
  <EditAttributeInput
95
103
  attributeName={attribute.attribute}
96
- id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}`}
104
+ id={`${inflection.underscore(camelizedLower)}_${inflection.underscore(attribute.attribute)}`}
97
105
  inputs={inputs}
98
106
  label={modelClass.humanAttributeName(attribute.attribute)}
99
107
  model={model}
@@ -102,7 +110,7 @@ const EditAttribute = ({attribute, inputs, model, modelClass}) => {
102
110
  }
103
111
  </>
104
112
  )
105
- }
113
+ })
106
114
 
107
115
  const EditPage = ({modelClass}) => {
108
116
  const availableLocales = Locales.availableLocales()
@@ -120,6 +128,10 @@ const EditPage = ({modelClass}) => {
120
128
 
121
129
  selectedAttributes[modelClassName] = selectedModelAttributes
122
130
 
131
+ if (!attributes) {
132
+ throw new Error(`No 'attributes' given from edit config for ${modelClass.modelClassData().name}`)
133
+ }
134
+
123
135
  for (const attribute of attributes) {
124
136
  if (attribute.translated) {
125
137
  for (const locale of availableLocales) {
@@ -6,7 +6,9 @@ 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"
11
+ import useCanCan from "../use-can-can"
10
12
  import useQueryParams from "on-location-changed/src/use-query-params"
11
13
 
12
14
  const ApiMakerSuperAdmin = () => {
@@ -18,6 +20,7 @@ const ApiMakerSuperAdmin = () => {
18
20
  const modelId = queryParams.model_id
19
21
  const modelName = modelClass?.modelClassData()?.name
20
22
  const [model, setModel] = useState()
23
+ const canCan = useCanCan(() => [[modelClass, ["new"]]])
21
24
 
22
25
  const loadModel = useCallback(async () => {
23
26
  if (modelId && modelClass) {
@@ -72,9 +75,13 @@ const ApiMakerSuperAdmin = () => {
72
75
  const actions = useMemo(
73
76
  () => <>
74
77
  {modelClass && pageToShow == "index" &&
75
- <Link className="create-new-model-link" to={Params.withParams({model: modelName, mode: "new"})}>
76
- Create new
77
- </Link>
78
+ <>
79
+ {canCan?.can("new", modelClass) &&
80
+ <Link className="create-new-model-link" to={Params.withParams({model: modelName, mode: "new"})}>
81
+ Create new
82
+ </Link>
83
+ }
84
+ </>
78
85
  }
79
86
  {model && pageToShow == "show" &&
80
87
  <>
@@ -90,8 +97,11 @@ const ApiMakerSuperAdmin = () => {
90
97
  }
91
98
  </>
92
99
  }
100
+ {pageToShow == "show_reflection" &&
101
+ <ShowReflectionActions model={model} modelClass={modelClass} reflectionName={queryParams.model_reflection} />
102
+ }
93
103
  </>,
94
- [model, pageToShow]
104
+ [canCan, model, modelClass, pageToShow]
95
105
  )
96
106
 
97
107
  return (
@@ -0,0 +1,31 @@
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
+ const canCan = useCanCan(() => [[reflection.modelClass(), ["new"]]])
10
+
11
+ modelData[reflection.foreignKey()] = model?.id()
12
+
13
+ const linkParams = {
14
+ model: modelClassName,
15
+ mode: "new"
16
+ }
17
+
18
+ linkParams[dataParamName] = modelData
19
+
20
+ return (
21
+ <>
22
+ {canCan?.can("new", reflection.modelClass()) &&
23
+ <Link className="create-new-model-link" to={Params.withParams(linkParams)}>
24
+ Create new
25
+ </Link>
26
+ }
27
+ </>
28
+ )
29
+ }
30
+
31
+ 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)