@kaspernj/api-maker 1.0.228 → 1.0.230

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.228",
19
+ "version": "1.0.230",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -73,7 +73,7 @@
73
73
  "eslint-find-rules": "^4.0.0",
74
74
  "eslint-plugin-jest": "^27.0.1",
75
75
  "eslint-plugin-react": "^7.23.2",
76
- "i18n-on-steroids": "^1.0.2",
76
+ "i18n-on-steroids": "^1.0.5",
77
77
  "jest": "^29.0.1",
78
78
  "jsdom": "^20.0.0"
79
79
  }
@@ -7,6 +7,10 @@ export default class ApiMakerBaseModelReflection {
7
7
  this.reflectionData = reflectionData
8
8
  }
9
9
 
10
+ foreignKey() {
11
+ return digg(this, "reflectionData", "foreignKey")
12
+ }
13
+
10
14
  macro() {
11
15
  return digg(this, "reflectionData", "macro")
12
16
  }
@@ -18,6 +18,7 @@ export default class ApiMakerBootstrapCard extends React.PureComponent {
18
18
  defaultExpanded: PropTypes.bool.isRequired,
19
19
  expandable: PropTypes.bool.isRequired,
20
20
  expandableHide: PropTypes.bool.isRequired,
21
+ footer: PropTypes.node,
21
22
  header: PropTypes.node,
22
23
  striped: PropTypes.bool,
23
24
  responsiveTable: PropTypes.bool.isRequired,
@@ -39,6 +40,7 @@ export default class ApiMakerBootstrapCard extends React.PureComponent {
39
40
  defaultExpanded,
40
41
  expandable,
41
42
  expandableHide,
43
+ footer,
42
44
  header,
43
45
  responsiveTable,
44
46
  striped,
@@ -48,7 +50,7 @@ export default class ApiMakerBootstrapCard extends React.PureComponent {
48
50
  const {expanded} = digs(this.state, "expanded")
49
51
 
50
52
  return (
51
- <div className={this.classNames()} ref="card" {...restProps}>
53
+ <div className={this.classNames()} data-has-footer={Boolean(footer)} ref="card" {...restProps}>
52
54
  {(controls || expandable || header) &&
53
55
  <div className={`card-header d-flex ${!expanded && "border-bottom-0"}`}>
54
56
  <div style={{alignSelf: "center", marginRight: "auto"}}>
@@ -81,6 +83,11 @@ export default class ApiMakerBootstrapCard extends React.PureComponent {
81
83
  {!table && children}
82
84
  </div>
83
85
  }
86
+ {footer &&
87
+ <div className="card-footer">
88
+ {footer}
89
+ </div>
90
+ }
84
91
  </div>
85
92
  )
86
93
  }
@@ -6,7 +6,9 @@ import {incorporate} from "incorporator"
6
6
  import modelClassRequire from "./model-class-require.mjs"
7
7
  import Result from "./result.mjs"
8
8
 
9
- class ApiMakerCollection {
9
+ export default class ApiMakerCollection {
10
+ static apiMakerType = "Collection"
11
+
10
12
  constructor (args, queryArgs = {}) {
11
13
  this.queryArgs = queryArgs
12
14
  this.args = args
@@ -249,7 +251,3 @@ class ApiMakerCollection {
249
251
  )
250
252
  }
251
253
  }
252
-
253
- ApiMakerCollection.apiMakerType = "Collection"
254
-
255
- export default ApiMakerCollection
@@ -9,7 +9,11 @@ export default class ApiMakerSuperAdminConfigReader {
9
9
  try {
10
10
  modelConfig = require(`super-admin/model-configs/${modelNameCamelized}`).default
11
11
  } catch (error) {
12
- console.log(`No model-config for ${modelClass.modelClassData().name}`)
12
+ if (error.message.includes("Cannot find module")) {
13
+ console.log(`No model-config for ${modelClass.modelClassData().name}`)
14
+ } else {
15
+ throw error
16
+ }
13
17
  }
14
18
 
15
19
  return new ApiMakerSuperAdminConfigReader(modelClass, modelConfig)
@@ -22,7 +22,7 @@ class ApiMakerSuperAdmin extends React.PureComponent {
22
22
  if (queryParams.model) modelClass = modelsModule[queryParams.model]
23
23
 
24
24
  return (
25
- <Layout headerTitle={modelClass?.modelName()?.human({count: 2})}>
25
+ <Layout active={queryParams.model} headerTitle={modelClass?.modelName()?.human({count: 2})}>
26
26
  {pageToShow == "index" &&
27
27
  <IndexPage
28
28
  currentUser={currentUser}
@@ -2,6 +2,7 @@ import AttributeRows from "../../bootstrap/attribute-rows"
2
2
  import BelongsToAttributeRow from "./belongs-to-attribute-row"
3
3
  import ConfigReader from "../config-reader"
4
4
  import {digg, digs} from "diggerize"
5
+ import inflection from "inflection"
5
6
  import modelLoadWrapper from "../../model-load-wrapper"
6
7
  import PropTypes from "prop-types"
7
8
  import React from "react"
@@ -13,10 +14,16 @@ class ApiMakerSuperAdminShowPage extends React.PureComponent {
13
14
  queryParams: PropTypes.object.isRequired
14
15
  }
15
16
 
17
+ configReader = ConfigReader.forModel(digg(this, "props", "modelClass"))
18
+
16
19
  render() {
17
20
  const {modelClass, queryParams} = digs(this.props, "modelClass", "queryParams")
18
21
  const attributes = this.attributes()
19
22
  const model = this.model()
23
+ const extraContent = this.configReader.modelConfig?.show?.extraContent
24
+ const modelArgs = {}
25
+
26
+ modelArgs[inflection.camelize(modelClass.modelClassData().name, true)] = model
20
27
 
21
28
  return (
22
29
  <div className="super-admin--show-page">
@@ -29,16 +36,12 @@ class ApiMakerSuperAdminShowPage extends React.PureComponent {
29
36
  {model && modelClass.reflections().filter((reflection) => reflection.macro() == "belongs_to").map((reflection) =>
30
37
  <BelongsToAttributeRow key={reflection.name()} model={model} modelClass={modelClass} reflection={reflection} />
31
38
  )}
39
+ {model && extraContent && extraContent(modelArgs)}
32
40
  </div>
33
41
  )
34
42
  }
35
43
 
36
- attributes() {
37
- const {modelClass} = digs(this.props, "modelClass")
38
- const configReader = ConfigReader.forModel(modelClass)
39
-
40
- return configReader.attributesToShow()
41
- }
44
+ attributes = () => this.configReader.attributesToShow()
42
45
 
43
46
  model() {
44
47
  const {modelClass} = digs(this.props, "modelClass")
@@ -60,7 +63,19 @@ export default modelLoadWrapper(
60
63
  modelClassResolver,
61
64
  ({modelClass}) => {
62
65
  const preload = []
63
- const select = {}
66
+ const configReader = ConfigReader.forModel(modelClass)
67
+ const select = configReader.modelConfig?.show?.extraSelect || {}
68
+ const modelClassName = modelClass.modelClassData().name
69
+ const modelClassSelect = select[modelClassName] || []
70
+ const primaryKeyName = modelClass.primaryKey()
71
+
72
+ if (!(modelClassName in select)) select[modelClassName] = modelClassSelect
73
+ if (!modelClassSelect.includes(primaryKeyName)) modelClassSelect.push(primaryKeyName)
74
+
75
+ // Select all attributes selected by default because they will be shown by default
76
+ for (const attribute of modelClass.attributes()) {
77
+ if (attribute.isSelectedByDefault() && !modelClassSelect.includes(attribute.name())) modelClassSelect.push(attribute.name())
78
+ }
64
79
 
65
80
  for (const reflection of modelClass.reflections()) {
66
81
  if (reflection.macro() != "belongs_to") continue
@@ -70,11 +85,14 @@ export default modelLoadWrapper(
70
85
  const reflectionModelClassAttributes = reflectionModelClass.attributes()
71
86
  const nameAttribute = reflectionModelClassAttributes.find((attribute) => attribute.name() == "name")
72
87
 
73
- preload.push(reflection.name())
88
+ preload.push(inflection.underscore(reflection.name()))
74
89
 
75
90
  if (!(reflectionModelClassName in select)) select[reflectionModelClassName] = []
76
91
  if (!select[reflectionModelClassName].includes("id")) select[reflectionModelClassName].push("id")
77
92
  if (nameAttribute && !select[reflectionModelClassName].includes("name")) select[reflectionModelClassName].push("name")
93
+
94
+ // The foreign key is needed to look up any belongs-to-relationships
95
+ if (!modelClassSelect.includes(reflection.foreignKey())) modelClassSelect.push(reflection.foreignKey())
78
96
  }
79
97
 
80
98
  return {
@@ -7,7 +7,6 @@ import columnVisible from "./column-visible.mjs"
7
7
  import {debounce} from "debounce"
8
8
  import {digg, digs} from "diggerize"
9
9
  import inflection from "inflection"
10
- import instanceOfClassName from "../instance-of-class-name"
11
10
  import modelClassRequire from "../model-class-require.mjs"
12
11
  import ModelRow from "./model-row"
13
12
  import Paginate from "../bootstrap/paginate"
@@ -300,7 +299,7 @@ class ApiMakerTable extends React.PureComponent {
300
299
  this.filterForm()
301
300
  }
302
301
  {card &&
303
- <Card className={classNames("mb-4", className)} controls={controlsContent} header={headerContent} table={!this.isSmallScreen()} {...restProps}>
302
+ <Card className={classNames("mb-4", className)} controls={controlsContent} header={headerContent} footer={this.tableFooter()} table={!this.isSmallScreen()} {...restProps}>
304
303
  {this.tableContent()}
305
304
  </Card>
306
305
  }
@@ -384,7 +383,25 @@ class ApiMakerTable extends React.PureComponent {
384
383
  )
385
384
  }
386
385
 
387
- className () {
386
+ tableFooter() {
387
+ const {result} = digs(this.shape, "result")
388
+ const currentPage = result.currentPage()
389
+ const totalCount = result.totalCount()
390
+ const perPage = result.perPage()
391
+ const to = Math.min(currentPage * perPage, totalCount)
392
+ const defaultValue = "Showing %{from} to %{to} out of %{total_count} total"
393
+ let from = ((currentPage - 1) * perPage) + 1
394
+
395
+ if (to === 0) from = 0
396
+
397
+ return (
398
+ <div style={{marginTop: "10px"}}>
399
+ {I18n.t("js.api_maker.table.showing_from_to_out_of_total", {defaultValue, from, to, total_count: totalCount})}
400
+ </div>
401
+ )
402
+ }
403
+
404
+ className() {
388
405
  const classNames = ["component-api-maker-live-table"]
389
406
 
390
407
  if (this.props.className)