@kaspernj/api-maker 1.0.305 → 1.0.307

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.305",
19
+ "version": "1.0.307",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -19,7 +19,7 @@ const EditPage = ({modelClass}) => {
19
19
  const modelVarName = inflection.camelize(modelClass.modelClassData().name, true)
20
20
  const extraContent = configReader.modelConfig?.edit?.extraContentconst
21
21
  const attributes = configReader.modelConfig?.edit?.attributes
22
- const selectedModelAttributes = []
22
+ const selectedModelAttributes = ["id"]
23
23
  const selectedAttributes = {}
24
24
 
25
25
  selectedAttributes[modelClassName] = selectedModelAttributes
@@ -37,7 +37,8 @@ const EditPage = ({modelClass}) => {
37
37
  const useModelResult = useModel(modelClass, {
38
38
  cacheArgs: [currentUser?.id()],
39
39
  loadByQueryParam: (props) => props.queryParams.model_id,
40
- newIfNoId: true
40
+ newIfNoId: true,
41
+ select: selectedAttributes
41
42
  })
42
43
 
43
44
  const model = digg(useModelResult, modelVarName)
@@ -68,7 +69,7 @@ const EditPage = ({modelClass}) => {
68
69
  {attribute.translated && availableLocales.map((locale) =>
69
70
  <div key={locale}>
70
71
  <Input
71
- attribute={attribute.attribute}
72
+ attribute={`${attribute.attribute}${inflection.camelize(locale)}`}
72
73
  id={`${camelizedLower}_${inflection.underscore(attribute.attribute)}_${locale}`}
73
74
  label={`${modelClass.humanAttributeName(attribute.attribute)} (${locale})`}
74
75
  model={model}
@@ -1,10 +1,10 @@
1
- import {digg} from "diggerize"
2
1
  import EditPage from "./edit-page"
3
2
  import IndexPage from "./index-page"
4
3
  import Layout from "./layout"
5
4
  import Link from "../link"
6
5
  import {memo, useMemo} from "react"
7
6
  import * as modelsModule from "@kaspernj/api-maker/src/models.mjs.erb"
7
+ import {useCallback} from "react"
8
8
  import ShowPage from "./show-page"
9
9
  import ShowReflectionPage from "./show-reflection-page"
10
10
  import useQueryParams from "on-location-changed/src/use-query-params"
@@ -15,8 +15,13 @@ const ApiMakerSuperAdmin = () => {
15
15
 
16
16
  if (queryParams.model) modelClass = modelsModule[queryParams.model]
17
17
 
18
+ const modelId = queryParams.model_id
19
+ const modelName = modelClass?.modelClassData()?.name
20
+
18
21
  if (queryParams.model && queryParams.model_id && queryParams.model_reflection) {
19
22
  pageToShow = "show_reflection"
23
+ } else if (queryParams.model && queryParams.model_id && queryParams.mode == "edit") {
24
+ pageToShow = "edit"
20
25
  } else if (queryParams.model && queryParams.model_id) {
21
26
  pageToShow = "show"
22
27
  } else if (queryParams.model && queryParams.mode == "new") {
@@ -27,13 +32,39 @@ const ApiMakerSuperAdmin = () => {
27
32
  pageToShow = "welcome"
28
33
  }
29
34
 
35
+ const onDestroyClicked = useCallback(async (e) => {
36
+ e.preventDefault()
37
+
38
+ if (!confirm("Are you sure?")) {
39
+ return
40
+ }
41
+
42
+ try {
43
+ const model = await modelClass.find(modelId)
44
+
45
+ await model.destroy()
46
+ } catch (error) {
47
+ FlashMessage.errorResponse(error)
48
+ }
49
+ }, [modelName, modelId])
50
+
30
51
  const actions = useMemo(
31
52
  () => <>
32
53
  {modelClass && pageToShow == "index" &&
33
- <Link className="create-new-model-link" to={Params.withParams({model: modelClass.modelClassData().name, mode: "new"})}>
54
+ <Link className="create-new-model-link" to={Params.withParams({model: modelName, mode: "new"})}>
34
55
  Create new
35
56
  </Link>
36
57
  }
58
+ {modelClass && pageToShow == "show" &&
59
+ <>
60
+ <Link to={Params.withParams({model: modelName, model_id: modelId, mode: "edit"})}>
61
+ Edit
62
+ </Link>
63
+ <a href="#" onClick={onDestroyClicked}>
64
+ Delete
65
+ </a>
66
+ </>
67
+ }
37
68
  </>,
38
69
  [modelClass, pageToShow]
39
70
  )
@@ -42,27 +73,27 @@ const ApiMakerSuperAdmin = () => {
42
73
  <Layout actions={actions} active={queryParams.model} headerTitle={modelClass?.modelName()?.human({count: 2})}>
43
74
  {pageToShow == "index" &&
44
75
  <IndexPage
45
- key={`index-page-${digg(modelClass.modelClassData(), "name")}`}
76
+ key={`index-page-${modelName}`}
46
77
  modelClass={modelClass}
47
78
  />
48
79
  }
49
80
  {pageToShow == "show" &&
50
81
  <ShowPage
51
- key={`show-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
82
+ key={`show-page-${modelName}-${modelId}`}
52
83
  modelClass={modelClass}
53
- modelId={queryParams.modelId}
84
+ modelId={modelId}
54
85
  />
55
86
  }
56
87
  {pageToShow == "show_reflection" &&
57
88
  <ShowReflectionPage
58
- key={`show-reflection-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
89
+ key={`show-reflection-page-${modelName}-${modelId}`}
59
90
  modelClass={modelClass}
60
- modelId={queryParams.modelId}
91
+ modelId={modelId}
61
92
  />
62
93
  }
63
94
  {pageToShow == "edit" &&
64
95
  <EditPage
65
- key={`edit-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
96
+ key={`edit-page-${modelName}-${modelId}`}
66
97
  modelClass={modelClass}
67
98
  />
68
99
  }
@@ -16,6 +16,17 @@ const ApiMakerSuperAdminModelClassTable = ({modelClass, ...restProps}) => {
16
16
  return configReader.tableColumns()
17
17
  }, [modelClass])
18
18
 
19
+ const editModelPath = useCallback((args) => {
20
+ const argName = inflection.camelize(digg(modelClass.modelClassData(), "name"), true)
21
+ const model = digg(args, argName)
22
+
23
+ return Params.withParams({
24
+ model: modelClass.modelClassData().name,
25
+ model_id: model.primaryKey(),
26
+ mode: "edit"
27
+ })
28
+ })
29
+
19
30
  const viewModelPath = useCallback((args) => {
20
31
  const argName = inflection.camelize(digg(modelClass.modelClassData(), "name"), true)
21
32
  const model = digg(args, argName)
@@ -30,6 +41,7 @@ const ApiMakerSuperAdminModelClassTable = ({modelClass, ...restProps}) => {
30
41
  <Table
31
42
  columns={columns}
32
43
  currentUser={currentUser}
44
+ editModelPath={editModelPath}
33
45
  modelClass={modelClass}
34
46
  viewModelPath={viewModelPath}
35
47
  {...restProps}
@@ -3,6 +3,7 @@ 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"
6
7
  import PropTypes from "prop-types"
7
8
  import {memo} from "react"
8
9
  import ShowNav from "../show-nav"