@kaspernj/api-maker 1.0.308 → 1.0.309

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.308",
19
+ "version": "1.0.309",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -8,12 +8,52 @@ 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 EditAttribute = ({attribute, model, modelArgs, modelClass}) => {
12
+ const availableLocales = Locales.availableLocales()
13
+ const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
14
+ const contentArgs = () => {
15
+ const contentArgsResult = {
16
+ inputProps: {
17
+ attribute: attribute.attribute,
18
+ model
19
+ }
20
+ }
21
+
22
+ return contentArgsResult
23
+ }
24
+
25
+ return (
26
+ <>
27
+ {attribute.content && attribute.content(contentArgs())}
28
+ {!attribute.content && attribute.translated && availableLocales.map((locale) =>
29
+ <React.Fragment key={locale}>
30
+ <Input
31
+ attribute={`${attribute.attribute}${inflection.camelize(locale)}`}
32
+ id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}_${locale}`}
33
+ label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
34
+ model={model}
35
+ name={`${camelizedLower}[${inflection.underscore(attribute.attribute)}_${locale}]`}
36
+ />
37
+ </React.Fragment>
38
+ )}
39
+ {!attribute.content && !attribute.translated &&
40
+ <Input
41
+ attribute={attribute.attribute}
42
+ id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}`}
43
+ label={modelClass.humanAttributeName(attribute.attribute)}
44
+ model={model}
45
+ name={`${camelizedLower}[${inflection.underscore(attribute.attribute)}]`}
46
+ />
47
+ }
48
+ </>
49
+ )
50
+ }
51
+
11
52
  const EditPage = ({modelClass}) => {
12
53
  const availableLocales = Locales.availableLocales()
13
54
  const currentUser = useCurrentUser()
14
55
  const queryParams = useQueryParams()
15
56
  const configReader = ConfigReader.forModel(modelClass)
16
- const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
17
57
  const modelClassName = modelClass.modelClassData().name
18
58
  const modelIdVarName = `${inflection.camelize(modelClass.modelClassData().name, true)}Id`
19
59
  const modelVarName = inflection.camelize(modelClass.modelClassData().name, true)
@@ -65,28 +105,7 @@ const EditPage = ({modelClass}) => {
65
105
  <div className="super-admin--edit-page">
66
106
  <form onSubmit={onSubmit}>
67
107
  {model && attributes?.map((attribute) =>
68
- <div key={attribute.attribute}>
69
- {attribute.translated && availableLocales.map((locale) =>
70
- <div key={locale}>
71
- <Input
72
- attribute={`${attribute.attribute}${inflection.camelize(locale)}`}
73
- id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}_${locale}`}
74
- label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
75
- model={model}
76
- name={`${camelizedLower}[${inflection.underscore(attribute.attribute)}_${locale}]`}
77
- />
78
- </div>
79
- )}
80
- {!attribute.translated &&
81
- <Input
82
- attribute={attribute.attribute}
83
- id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}`}
84
- label={modelClass.humanAttributeName(attribute.attribute)}
85
- model={model}
86
- name={`${camelizedLower}[${inflection.underscore(attribute.attribute)}]`}
87
- />
88
- }
89
- </div>
108
+ <EditAttribute attribute={attribute} key={attribute.attribute} model={model} modelArgs={modelArgs} modelClass={modelClass} />
90
109
  )}
91
110
  {extraContent && extraContent(modelArgs)}
92
111
  <button style={{marginTop: "10px"}} type="submit">
@@ -1,32 +1,88 @@
1
- import AttributeRows from "../../bootstrap/attribute-rows"
1
+ import AttributeRow from "../../bootstrap/attribute-row"
2
2
  import BelongsToAttributeRow from "./belongs-to-attribute-row"
3
3
  import ConfigReader from "../config-reader"
4
4
  import {digg} from "diggerize"
5
5
  import * as inflection from "inflection"
6
- import Link from "../../link"
7
6
  import PropTypes from "prop-types"
8
7
  import {memo} from "react"
9
8
  import ShowNav from "../show-nav"
10
- import withModel from "../../with-model"
9
+ import useModel from "../../use-model"
11
10
 
12
- const ApiMakerSuperAdminShowPage = ({modelClass, ...restProps}) => {
11
+ const AttributePresenter = ({attribute, model, modelArgs}) => {
12
+ const attributeRowProps = {}
13
+
14
+ if (typeof attribute == "object") {
15
+ attributeRowProps.attribute = attribute.attribute
16
+ if (attribute.content) attributeRowProps.children = attribute.content(modelArgs)
17
+ } else {
18
+ attributeRowProps.attribute = attribute
19
+ }
20
+
21
+ console.log({attributeRowProps})
22
+
23
+ return (
24
+ <AttributeRow model={model} {...attributeRowProps} />
25
+ )
26
+ }
27
+
28
+ const ApiMakerSuperAdminShowPage = ({modelClass}) => {
13
29
  const configReader = ConfigReader.forModel(modelClass)
30
+ const showConfig = configReader.modelConfig?.show
14
31
  const attributes = configReader.attributesToShow()
32
+ const extraContent = showConfig?.extraContent
33
+ const modelClassName = modelClass.modelClassData().name
34
+ const primaryKeyName = modelClass.primaryKey()
35
+ const preload = []
36
+ const select = showConfig?.extraSelect || {}
37
+ const modelClassSelect = select[modelClassName] || []
38
+
39
+ if (!(modelClassName in select)) select[modelClassName] = modelClassSelect
40
+ if (!modelClassSelect.includes(primaryKeyName)) modelClassSelect.push(primaryKeyName)
41
+
42
+ // Select all attributes selected by default because they will be shown by default
43
+ for (const attribute of modelClass.attributes()) {
44
+ if (attribute.isSelectedByDefault() && !modelClassSelect.includes(attribute.name())) modelClassSelect.push(attribute.name())
45
+ }
46
+
47
+ for (const reflection of modelClass.reflections()) {
48
+ if (reflection.macro() != "belongs_to") continue
49
+
50
+ const reflectionModelClass = reflection.modelClass()
51
+ const reflectionModelClassName = reflectionModelClass.modelClassData().name
52
+ const reflectionModelClassAttributes = reflectionModelClass.attributes()
53
+ const nameAttribute = reflectionModelClassAttributes.find((attribute) => attribute.name() == "name")
54
+
55
+ preload.push(inflection.underscore(reflection.name()))
56
+
57
+ if (!(reflectionModelClassName in select)) select[reflectionModelClassName] = []
58
+ if (!select[reflectionModelClassName].includes("id")) select[reflectionModelClassName].push("id")
59
+ if (nameAttribute && !select[reflectionModelClassName].includes("name")) select[reflectionModelClassName].push("name")
60
+
61
+ // The foreign key is needed to look up any belongs-to-relationships
62
+ if (!modelClassSelect.includes(reflection.foreignKey())) modelClassSelect.push(reflection.foreignKey())
63
+ }
64
+
65
+ const useModelResult = useModel(modelClass, {
66
+ loadByQueryParam: ({queryParams}) => queryParams.model_id,
67
+ preload,
68
+ select
69
+ })
15
70
  const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
16
- const model = digg(restProps, camelizedLower)
17
- const extraContent = configReader.modelConfig?.show?.extraContent
71
+ const model = digg(useModelResult, camelizedLower)
18
72
  const modelArgs = {}
19
73
 
20
74
  modelArgs[inflection.camelize(modelClass.modelClassData().name, true)] = model
21
75
 
76
+ console.log({attributes})
77
+
22
78
  return (
23
79
  <div className="super-admin--show-page">
24
80
  {model &&
25
81
  <ShowNav model={model} modelClass={modelClass} />
26
82
  }
27
- {attributes && model &&
28
- <AttributeRows attributes={attributes} model={model} />
29
- }
83
+ {attributes && model && attributes.map((attribute) =>
84
+ <AttributePresenter attribute={attribute} key={attribute.attribute || attribute} modelArgs={modelArgs} model={model} />
85
+ )}
30
86
  {model && modelClass.reflections().filter((reflection) => reflection.macro() == "belongs_to").map((reflection) =>
31
87
  <BelongsToAttributeRow key={reflection.name()} model={model} modelClass={modelClass} reflection={reflection} />
32
88
  )}
@@ -39,54 +95,4 @@ ApiMakerSuperAdminShowPage.propTypes = {
39
95
  modelClass: PropTypes.func.isRequired
40
96
  }
41
97
 
42
- const modelClassResolver = {callback: ({queryParams}) => {
43
- const modelClassName = digg(queryParams, "model")
44
- const modelClass = digg(require("../../models.mjs.erb"), modelClassName)
45
-
46
- return modelClass
47
- }}
48
-
49
- export default withModel(
50
- memo(ApiMakerSuperAdminShowPage),
51
- modelClassResolver,
52
- ({modelClass}) => {
53
- const preload = []
54
- const configReader = ConfigReader.forModel(modelClass)
55
- const select = configReader.modelConfig?.show?.extraSelect || {}
56
- const modelClassName = modelClass.modelClassData().name
57
- const modelClassSelect = select[modelClassName] || []
58
- const primaryKeyName = modelClass.primaryKey()
59
-
60
- if (!(modelClassName in select)) select[modelClassName] = modelClassSelect
61
- if (!modelClassSelect.includes(primaryKeyName)) modelClassSelect.push(primaryKeyName)
62
-
63
- // Select all attributes selected by default because they will be shown by default
64
- for (const attribute of modelClass.attributes()) {
65
- if (attribute.isSelectedByDefault() && !modelClassSelect.includes(attribute.name())) modelClassSelect.push(attribute.name())
66
- }
67
-
68
- for (const reflection of modelClass.reflections()) {
69
- if (reflection.macro() != "belongs_to") continue
70
-
71
- const reflectionModelClass = reflection.modelClass()
72
- const reflectionModelClassName = reflectionModelClass.modelClassData().name
73
- const reflectionModelClassAttributes = reflectionModelClass.attributes()
74
- const nameAttribute = reflectionModelClassAttributes.find((attribute) => attribute.name() == "name")
75
-
76
- preload.push(inflection.underscore(reflection.name()))
77
-
78
- if (!(reflectionModelClassName in select)) select[reflectionModelClassName] = []
79
- if (!select[reflectionModelClassName].includes("id")) select[reflectionModelClassName].push("id")
80
- if (nameAttribute && !select[reflectionModelClassName].includes("name")) select[reflectionModelClassName].push("name")
81
-
82
- // The foreign key is needed to look up any belongs-to-relationships
83
- if (!modelClassSelect.includes(reflection.foreignKey())) modelClassSelect.push(reflection.foreignKey())
84
- }
85
-
86
- return {
87
- loadByQueryParam: ({props}) => props.queryParams.model_id,
88
- preload,
89
- select
90
- }
91
- }
92
- )
98
+ export default memo(ApiMakerSuperAdminShowPage)