@kaspernj/api-maker 1.0.226 → 1.0.228

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.226",
19
+ "version": "1.0.228",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -1,7 +1,9 @@
1
1
  import {dig, digg} from "diggerize"
2
2
  import errorMessages from "./error-messages.mjs"
3
3
 
4
- class BaseError extends Error {
4
+ export default class BaseError extends Error {
5
+ static apiMakerType = "BaseError"
6
+
5
7
  constructor (message, args = {}) {
6
8
  let messageToUse = message
7
9
 
@@ -34,7 +36,3 @@ class BaseError extends Error {
34
36
  }
35
37
  }
36
38
  }
37
-
38
- BaseError.apiMakerType = "BaseError"
39
-
40
- export default BaseError
@@ -0,0 +1,23 @@
1
+ import {digg} from "diggerize"
2
+
3
+ export default class ApiMakerBaseModelAttribute {
4
+ constructor(attributeData) {
5
+ this.attributeData = attributeData
6
+ }
7
+
8
+ isColumn() {
9
+ return Boolean(digg(this, "attributeData", "column"))
10
+ }
11
+
12
+ isSelectedByDefault() {
13
+ const isSelectedByDefault = digg(this, "attributeData", "selected_by_default")
14
+
15
+ if (isSelectedByDefault || isSelectedByDefault === null) return true
16
+
17
+ return false
18
+ }
19
+
20
+ name() {
21
+ return digg(this, "attributeData", "name")
22
+ }
23
+ }
@@ -0,0 +1,23 @@
1
+ import {digg} from "diggerize"
2
+ import inflection from "inflection"
3
+ import modelClassRequire from "../model-class-require.mjs"
4
+
5
+ export default class ApiMakerBaseModelReflection {
6
+ constructor(reflectionData) {
7
+ this.reflectionData = reflectionData
8
+ }
9
+
10
+ macro() {
11
+ return digg(this, "reflectionData", "macro")
12
+ }
13
+
14
+ modelClass() {
15
+ const modelClass = modelClassRequire(inflection.singularize(inflection.camelize(digg(this, "reflectionData", "name"))))
16
+
17
+ return modelClass
18
+ }
19
+
20
+ name() {
21
+ return inflection.camelize(digg(this, "reflectionData", "name"), true)
22
+ }
23
+ }
@@ -1,3 +1,4 @@
1
+ import Attribute from "./base-model/attribute.mjs"
1
2
  import AttributeNotLoadedError from "./attribute-not-loaded-error.mjs"
2
3
  import Collection from "./collection.mjs"
3
4
  import CommandsPool from "./commands-pool.mjs"
@@ -9,40 +10,21 @@ import inflection from "inflection"
9
10
  import ModelName from "./model-name.mjs"
10
11
  import NotLoadedError from "./not-loaded-error.mjs"
11
12
  import objectToFormData from "object-to-formdata"
13
+ import Reflection from "./base-model/reflection.mjs"
12
14
  import Services from "./services.mjs"
13
15
  import ValidationError from "./validation-error.mjs"
14
16
  import {ValidationErrors} from "./validation-errors.mjs"
15
17
 
16
- class ApiMakerAttribute {
17
- constructor(attributeData) {
18
- this.attributeData = attributeData
19
- }
20
-
21
- isColumn() {
22
- return Boolean(digg(this, "attributeData", "column"))
23
- }
24
-
25
- isSelectedByDefault() {
26
- const isSelectedByDefault = digg(this, "attributeData", "selected_by_default")
27
-
28
- if (isSelectedByDefault || isSelectedByDefault === null) return true
29
-
30
- return false
31
- }
32
-
33
- name() {
34
- return digg(this, "attributeData", "name")
35
- }
36
- }
18
+ export default class BaseModel {
19
+ static apiMakerType = "BaseModel"
37
20
 
38
- class BaseModel {
39
21
  static attributes() {
40
22
  const attributes = digg(this.modelClassData(), "attributes")
41
23
  const result = []
42
24
 
43
25
  for (const attributeKey in attributes) {
44
26
  const attributeData = attributes[attributeKey]
45
- const attribute = new ApiMakerAttribute(attributeData)
27
+ const attribute = new Attribute(attributeData)
46
28
 
47
29
  result.push(attribute)
48
30
  }
@@ -65,10 +47,10 @@ class BaseModel {
65
47
  }
66
48
  }
67
49
 
68
- static async find (id) {
69
- const primaryKeyName = this.modelClassData().primaryKey
50
+ static async find(id) {
70
51
  const query = {}
71
- query[`${primaryKeyName}_eq`] = id
52
+
53
+ query[`${this.primaryKey()}_eq`] = id
72
54
 
73
55
  const model = await this.ransack(query).first()
74
56
 
@@ -79,7 +61,7 @@ class BaseModel {
79
61
  }
80
62
  }
81
63
 
82
- static async findOrCreateBy (findOrCreateByArgs, args = {}) {
64
+ static async findOrCreateBy(findOrCreateByArgs, args = {}) {
83
65
  const result = await Services.current().sendRequest("Models::FindOrCreateBy", {
84
66
  additional_data: args.additionalData,
85
67
  find_or_create_by_args: findOrCreateByArgs,
@@ -90,7 +72,7 @@ class BaseModel {
90
72
  return model
91
73
  }
92
74
 
93
- static modelName () {
75
+ static modelName() {
94
76
  return new ModelName({modelClassData: this.modelClassData()})
95
77
  }
96
78
 
@@ -98,10 +80,23 @@ class BaseModel {
98
80
  return digg(this.modelClassData(), "primaryKey")
99
81
  }
100
82
 
101
- static ransack (query = {}) {
83
+ static ransack(query = {}) {
102
84
  return new Collection({modelClass: this}, {ransack: query})
103
85
  }
104
86
 
87
+ static reflections() {
88
+ const relationships = digg(this.modelClassData(), "relationships")
89
+ const reflections = []
90
+
91
+ for (const relationshipData of relationships) {
92
+ const reflection = new Reflection(relationshipData)
93
+
94
+ reflections.push(reflection)
95
+ }
96
+
97
+ return reflections
98
+ }
99
+
105
100
  constructor (args = {}) {
106
101
  this.changes = {}
107
102
  this.newRecord = args.isNewRecord
@@ -302,7 +297,7 @@ class BaseModel {
302
297
 
303
298
  // Load the missing abilities if any
304
299
  if (abilitiesToLoad.length > 0) {
305
- const primaryKeyName = this.modelClassData().primaryKey
300
+ const primaryKeyName = this.constructor.primaryKey()
306
301
  const ransackParams = {}
307
302
  ransackParams[`${primaryKeyName}_eq`] = this.primaryKey()
308
303
 
@@ -483,9 +478,8 @@ class BaseModel {
483
478
 
484
479
  async reload () {
485
480
  const params = this.collection && this.collection.params()
486
- const primaryKeyName = this.modelClassData().primaryKey
487
481
  const ransackParams = {}
488
- ransackParams[`${primaryKeyName}_eq`] = this.primaryKey()
482
+ ransackParams[`${this.constructor.primaryKey()}_eq`] = this.primaryKey()
489
483
 
490
484
  let query = this.constructor.ransack(ransackParams)
491
485
 
@@ -866,7 +860,7 @@ class BaseModel {
866
860
  }
867
861
 
868
862
  primaryKey () {
869
- return this.readAttributeUnderscore(digg(this.modelClassData(), "primaryKey"))
863
+ return this.readAttributeUnderscore(this.constructor.primaryKey())
870
864
  }
871
865
 
872
866
  static _token () {
@@ -877,7 +871,3 @@ class BaseModel {
877
871
  }
878
872
  }
879
873
  }
880
-
881
- BaseModel.apiMakerType = "BaseModel"
882
-
883
- export default BaseModel
@@ -24,10 +24,7 @@ export default class CollectionLoader extends React.PureComponent {
24
24
  abilities: PropTypes.object,
25
25
  appHistory: PropTypes.object,
26
26
  className: PropTypes.string,
27
- collection: PropTypes.oneOfType([
28
- instanceOfClassName("ApiMakerCollection"),
29
- PropTypes.instanceOf(Collection)
30
- ]),
27
+ collection: PropTypes.instanceOf(Collection),
31
28
  component: PropTypes.object,
32
29
  defaultParams: PropTypes.object,
33
30
  groupBy: PropTypes.array,
@@ -5,13 +5,14 @@ import PropTypes from "prop-types"
5
5
  import React from "react"
6
6
  import withQueryParams from "on-location-changed/src/with-query-params"
7
7
 
8
- export default (WrappedComponent, mdelClassArg, args = {}) => {
8
+ export default (WrappedComponent, modelClassArg, argsArg = {}) => {
9
9
  class ModelLoadWrapper extends React.PureComponent {
10
10
  static propTypes = {
11
11
  queryParams: PropTypes.object
12
12
  }
13
13
 
14
- modelClass = this.resolveModelClass(mdelClassArg)
14
+ modelClass = this.resolveModelClass(modelClassArg)
15
+ args = this.resolveArgs()
15
16
  camelizedLower = this.modelClass.modelName().camelizedLower()
16
17
  paramsVariableName = `${this.modelClass.modelName().paramKey()}_id`
17
18
 
@@ -21,6 +22,16 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
21
22
  notFound: undefined
22
23
  }
23
24
 
25
+ resolveArgs() {
26
+ if (typeof argsArg == "function") {
27
+ return argsArg({
28
+ modelClass: this.modelClass
29
+ })
30
+ }
31
+
32
+ return argsArg
33
+ }
34
+
24
35
  resolveModelClass(modelClassArg) {
25
36
  if (typeof modelClassArg == "object") {
26
37
  const {queryParams} = digs(this.props, "queryParams")
@@ -46,16 +57,16 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
46
57
  }
47
58
 
48
59
  loadModel = async () => {
49
- if (args.newIfNoId && !this.getModelId()) {
60
+ if (this.args.newIfNoId && !this.getModelId()) {
50
61
  return await this.loadNewModel()
51
- } else if (!args.optional || this.getModelId()) {
62
+ } else if (!this.args.optional || this.getModelId()) {
52
63
  return await this.loadExistingModel()
53
64
  }
54
65
  }
55
66
 
56
67
  getModelId() {
57
- if (args.loadByQueryParam)
58
- return args.loadByQueryParam({props: this.props})
68
+ if (this.args.loadByQueryParam)
69
+ return this.args.loadByQueryParam({props: this.props})
59
70
 
60
71
  return this.props.match.params[this.paramsVariableName] || this.props.match.params.id
61
72
  }
@@ -65,9 +76,9 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
65
76
  const ModelClass = digg(this, "modelClass")
66
77
  const query = await ModelClass.ransack({id_eq: modelId})
67
78
 
68
- if (args.abilities) query.abilities(args.abilities)
69
- if (args.preload) query.preload(args.preload)
70
- if (args.select) query.select(args.select)
79
+ if (this.args.abilities) query.abilities(this.args.abilities)
80
+ if (this.args.preload) query.preload(this.args.preload)
81
+ if (this.args.select) query.select(this.args.select)
71
82
 
72
83
  const model = await query.first()
73
84
 
@@ -85,11 +96,11 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
85
96
 
86
97
  let defaults = {}
87
98
 
88
- if (args.newIfNoId?.defaults) {
89
- defaults = await args.newIfNoId.defaults()
99
+ if (this.args.newIfNoId?.defaults) {
100
+ defaults = await this.args.newIfNoId.defaults()
90
101
  }
91
102
 
92
- const modelData = Object.assign(defaults, args.newAttributes, modelDataFromParams)
103
+ const modelData = Object.assign(defaults, this.args.newAttributes, modelDataFromParams)
93
104
  const model = new ModelClass({
94
105
  isNewRecord: true,
95
106
  data: {a: modelData}
@@ -109,10 +120,10 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
109
120
 
110
121
  return (
111
122
  <>
112
- {args.events &&
113
- <EventEmitterListener event="reloadModel" events={args.events} onCalled={reloadModel} />
123
+ {this.args.events &&
124
+ <EventEmitterListener event="reloadModel" events={this.args.events} onCalled={reloadModel} />
114
125
  }
115
- {model && args.eventUpdated &&
126
+ {model && this.args.eventUpdated &&
116
127
  <EventUpdated model={model} onUpdated={onUpdated} />
117
128
  }
118
129
  <WrappedComponent {...wrappedComponentProps} {...this.props} />
@@ -124,7 +135,5 @@ export default (WrappedComponent, mdelClassArg, args = {}) => {
124
135
  onUpdated = this.loadExistingModel
125
136
  }
126
137
 
127
- if (args.loadByQueryParam) return withQueryParams(ModelLoadWrapper)
128
-
129
- return ModelLoadWrapper
138
+ return withQueryParams(ModelLoadWrapper)
130
139
  }
@@ -10,7 +10,10 @@ const result = loader.load()
10
10
  <% model_class_names = [] %>
11
11
  <% ApiMaker::ModelClassesJavaScriptGeneratorService.new.resources.each do |resource| %>
12
12
  <% model_class_names << resource.short_name %>
13
- const <%= resource.short_name %> = digg(result, "<%= resource.short_name %>")
13
+
14
+ const <%= resource.short_name %>ModelClass = digg(result, "<%= resource.short_name %>")
15
+
16
+ class <%= resource.short_name %> extends <%= resource.short_name %>ModelClass {}
14
17
  <% end %>
15
18
 
16
19
  export {<%= model_class_names.join(", ") %>}
@@ -0,0 +1,24 @@
1
+ import {digs} from "diggerize"
2
+ import ModelClassTable from "./model-class-table"
3
+ import PropTypes from "prop-types"
4
+ import React from "react"
5
+
6
+ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
7
+ static propTypes = {
8
+ currentUser: PropTypes.object,
9
+ modelClass: PropTypes.func.isRequired,
10
+ queryParams: PropTypes.object.isRequired
11
+ }
12
+
13
+ render() {
14
+ const {currentUser, modelClass, queryParams} = digs(this.props, "currentUser", "modelClass", "queryParams")
15
+
16
+ return (
17
+ <ModelClassTable
18
+ currentUser={currentUser}
19
+ modelClass={modelClass}
20
+ queryParams={queryParams}
21
+ />
22
+ )
23
+ }
24
+ }
@@ -4,11 +4,13 @@ import Layout from "./layout"
4
4
  import * as modelsModule from "@kaspernj/api-maker/src/models.mjs.erb"
5
5
  import PropTypes from "prop-types"
6
6
  import ShowPage from "./show-page"
7
+ import ShowReflectionPage from "./show-reflection-page"
7
8
  import withQueryParams from "on-location-changed/src/with-query-params"
8
9
 
9
10
  class ApiMakerSuperAdmin extends React.PureComponent {
10
11
  static propTypes = {
11
- currentUser: PropTypes.object
12
+ currentUser: PropTypes.object,
13
+ queryParams: PropTypes.object.isRequired
12
14
  }
13
15
 
14
16
  render() {
@@ -31,9 +33,19 @@ class ApiMakerSuperAdmin extends React.PureComponent {
31
33
  }
32
34
  {pageToShow == "show" &&
33
35
  <ShowPage
34
- key={`show-page-${digg(modelClass.modelClassData(), "name")}`}
36
+ key={`show-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
35
37
  modelClass={modelClass}
36
38
  modelId={queryParams.modelId}
39
+ queryParams={queryParams}
40
+ />
41
+ }
42
+ {pageToShow == "show_reflection" &&
43
+ <ShowReflectionPage
44
+ currentUser={currentUser}
45
+ key={`show-reflection-page-${digg(modelClass.modelClassData(), "name")}-${queryParams.modelId}`}
46
+ modelClass={modelClass}
47
+ modelId={queryParams.modelId}
48
+ queryParams={queryParams}
37
49
  />
38
50
  }
39
51
  </Layout>
@@ -43,7 +55,9 @@ class ApiMakerSuperAdmin extends React.PureComponent {
43
55
  pageToShow() {
44
56
  const {queryParams} = digs(this.props, "queryParams")
45
57
 
46
- if (queryParams.model && queryParams.model_id) {
58
+ if (queryParams.model && queryParams.model_id && queryParams.model_reflection) {
59
+ return "show_reflection"
60
+ } else if (queryParams.model && queryParams.model_id) {
47
61
  return "show"
48
62
  } else if (queryParams.model) {
49
63
  return "index"
@@ -1,11 +1,11 @@
1
- import ConfigReader from "../config-reader"
1
+ import ConfigReader from "./config-reader"
2
2
  import {digg, digs} from "diggerize"
3
- import Params from "../../params"
3
+ import Params from "../params"
4
4
  import PropTypes from "prop-types"
5
5
  import React from "react"
6
- import Table from "../../table/table"
6
+ import Table from "../table/table"
7
7
 
8
- export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
8
+ export default class ApiMakerSuperAdminModelClassTable extends React.PureComponent {
9
9
  static propTypes = {
10
10
  currentUser: PropTypes.object,
11
11
  modelClass: PropTypes.func.isRequired,
@@ -13,7 +13,7 @@ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
13
13
  }
14
14
 
15
15
  render() {
16
- const {currentUser, modelClass} = digs(this.props, "currentUser", "modelClass")
16
+ const {currentUser, modelClass, queryParams, ...restProps} = this.props
17
17
 
18
18
  return (
19
19
  <Table
@@ -21,6 +21,7 @@ export default class ApiMakerSuperAdminIndexPage extends React.PureComponent {
21
21
  currentUser={currentUser}
22
22
  modelClass={modelClass}
23
23
  viewModelPath={digg(this, "viewModelPath")}
24
+ {...restProps}
24
25
  />
25
26
  )
26
27
  }
@@ -0,0 +1,35 @@
1
+ import {digg, digs} from "diggerize"
2
+ import Link from "../link"
3
+ import PropTypes from "prop-types"
4
+ import PropTypesExact from "prop-types-exact"
5
+ import React from "react"
6
+
7
+ export default class ApiMakerSuperAdminShowNav extends React.PureComponent {
8
+ static propTypes = PropTypesExact({
9
+ model: PropTypes.object.isRequired,
10
+ modelClass: PropTypes.func.isRequired,
11
+ queryParams: PropTypes.object.isRequired
12
+ })
13
+
14
+ render() {
15
+ const {model, modelClass, queryParams} = digs(this.props, "model", "modelClass", "queryParams")
16
+ const reflections = modelClass.reflections()
17
+
18
+ return (
19
+ <div>
20
+ <div>
21
+ <Link to={Params.withParams({model: modelClass.modelClassData().name, model_id: queryParams.model_id})}>
22
+ {I18n.t("js.api_maker.suprt_admin.show_reflection_page.general", {defaultValue: "General"})}
23
+ </Link>
24
+ </div>
25
+ {model && reflections.filter((reflection) => reflection.macro() == "has_many").map((reflection) =>
26
+ <div key={reflection.name()}>
27
+ <Link to={Params.withParams({model: digg(modelClass.modelClassData(), "name"), model_id: model.primaryKey(), model_reflection: reflection.name()})}>
28
+ {modelClass.humanAttributeName(reflection.name())}
29
+ </Link>
30
+ </div>
31
+ )}
32
+ </div>
33
+ )
34
+ }
35
+ }
@@ -0,0 +1,25 @@
1
+ import AttributeRow from "../../bootstrap/attribute-row"
2
+ import {digs} from "diggerize"
3
+ import inflection from "inflection"
4
+ import Link from "../../link"
5
+ import Params from "../../params"
6
+ import React from "react"
7
+
8
+ export default class ApiMakerSuperAdminShowPageBelongsToAttributeRow extends React.PureComponent {
9
+ render() {
10
+ const {model, modelClass, reflection} = digs(this.props, "model", "modelClass", "reflection")
11
+ const reflectionMethodName = inflection.camelize(reflection.name(), true)
12
+ const subModel = model[reflectionMethodName]()
13
+
14
+ return (
15
+ <AttributeRow label={modelClass.humanAttributeName(inflection.camelize(reflection.name(), true))}>
16
+ {subModel &&
17
+ <Link to={Params.withParams({model: subModel.modelClassData().name, model_id: subModel.primaryKey()})}>
18
+ {subModel && "name" in subModel && subModel.name()}
19
+ {subModel && !("name" in subModel) && subModel?.id()}
20
+ </Link>
21
+ }
22
+ </AttributeRow>
23
+ )
24
+ }
25
+ }
@@ -0,0 +1,86 @@
1
+ import AttributeRows from "../../bootstrap/attribute-rows"
2
+ import BelongsToAttributeRow from "./belongs-to-attribute-row"
3
+ import ConfigReader from "../config-reader"
4
+ import {digg, digs} from "diggerize"
5
+ import modelLoadWrapper from "../../model-load-wrapper"
6
+ import PropTypes from "prop-types"
7
+ import React from "react"
8
+ import ShowNav from "../show-nav"
9
+
10
+ class ApiMakerSuperAdminShowPage extends React.PureComponent {
11
+ static propTypes = {
12
+ modelClass: PropTypes.func.isRequired,
13
+ queryParams: PropTypes.object.isRequired
14
+ }
15
+
16
+ render() {
17
+ const {modelClass, queryParams} = digs(this.props, "modelClass", "queryParams")
18
+ const attributes = this.attributes()
19
+ const model = this.model()
20
+
21
+ return (
22
+ <div className="super-admin--show-page">
23
+ {model &&
24
+ <ShowNav model={model} modelClass={modelClass} queryParams={queryParams} />
25
+ }
26
+ {attributes && model &&
27
+ <AttributeRows attributes={attributes} model={model} />
28
+ }
29
+ {model && modelClass.reflections().filter((reflection) => reflection.macro() == "belongs_to").map((reflection) =>
30
+ <BelongsToAttributeRow key={reflection.name()} model={model} modelClass={modelClass} reflection={reflection} />
31
+ )}
32
+ </div>
33
+ )
34
+ }
35
+
36
+ attributes() {
37
+ const {modelClass} = digs(this.props, "modelClass")
38
+ const configReader = ConfigReader.forModel(modelClass)
39
+
40
+ return configReader.attributesToShow()
41
+ }
42
+
43
+ model() {
44
+ const {modelClass} = digs(this.props, "modelClass")
45
+ const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
46
+
47
+ return digg(this, "props", camelizedLower)
48
+ }
49
+ }
50
+
51
+ const modelClassResolver = {callback: ({queryParams}) => {
52
+ const modelClassName = digg(queryParams, "model")
53
+ const modelClass = digg(require("../../models.mjs.erb"), modelClassName)
54
+
55
+ return modelClass
56
+ }}
57
+
58
+ export default modelLoadWrapper(
59
+ ApiMakerSuperAdminShowPage,
60
+ modelClassResolver,
61
+ ({modelClass}) => {
62
+ const preload = []
63
+ const select = {}
64
+
65
+ for (const reflection of modelClass.reflections()) {
66
+ if (reflection.macro() != "belongs_to") continue
67
+
68
+ const reflectionModelClass = reflection.modelClass()
69
+ const reflectionModelClassName = reflectionModelClass.modelClassData().name
70
+ const reflectionModelClassAttributes = reflectionModelClass.attributes()
71
+ const nameAttribute = reflectionModelClassAttributes.find((attribute) => attribute.name() == "name")
72
+
73
+ preload.push(reflection.name())
74
+
75
+ if (!(reflectionModelClassName in select)) select[reflectionModelClassName] = []
76
+ if (!select[reflectionModelClassName].includes("id")) select[reflectionModelClassName].push("id")
77
+ if (nameAttribute && !select[reflectionModelClassName].includes("name")) select[reflectionModelClassName].push("name")
78
+ }
79
+
80
+ return {
81
+ loadByQueryParam: ({props}) => props.queryParams.model_id,
82
+ preload,
83
+ select
84
+ }
85
+ }
86
+ )
@@ -0,0 +1,64 @@
1
+ import {digg, digs} from "diggerize"
2
+ import modelLoadWrapper from "../model-load-wrapper"
3
+ import PropTypes from "prop-types"
4
+ import React from "react"
5
+ import ModelClassTable from "./model-class-table"
6
+ import ShowNav from "./show-nav"
7
+
8
+ class ApiMakerSuperAdminShowReflectionPage extends React.PureComponent {
9
+ static propTypes = {
10
+ currentUser: PropTypes.object,
11
+ modelClass: PropTypes.func.isRequired,
12
+ queryParams: PropTypes.object.isRequired
13
+ }
14
+
15
+ render() {
16
+ const {currentUser, modelClass, queryParams} = digs(this.props, "currentUser", "modelClass", "queryParams")
17
+ const model = this.model()
18
+ const reflections = modelClass.reflections()
19
+ const reflection = reflections.find((reflectionI) => reflectionI.name() == queryParams.model_reflection)
20
+ const reflectionModelClass = reflection.modelClass()
21
+ let collection
22
+
23
+ if (model) collection = model[reflection.name()]()
24
+
25
+ return (
26
+ <div className="super-admin--show-page">
27
+ {model &&
28
+ <ShowNav model={model} modelClass={modelClass} queryParams={queryParams} />
29
+ }
30
+ {collection &&
31
+ <ModelClassTable
32
+ collection={collection}
33
+ currentUser={currentUser}
34
+ key={reflectionModelClass.modelName().human()}
35
+ modelClass={reflectionModelClass}
36
+ queryParams={queryParams}
37
+ />
38
+ }
39
+ </div>
40
+ )
41
+ }
42
+
43
+ model() {
44
+ const {modelClass} = digs(this.props, "modelClass")
45
+ const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
46
+
47
+ return digg(this, "props", camelizedLower)
48
+ }
49
+ }
50
+
51
+ const modelClassResolver = {callback: ({queryParams}) => {
52
+ const modelClassName = digg(queryParams, "model")
53
+ const modelClass = digg(require("../models.mjs.erb"), modelClassName)
54
+
55
+ return modelClass
56
+ }}
57
+
58
+ export default modelLoadWrapper(
59
+ ApiMakerSuperAdminShowReflectionPage,
60
+ modelClassResolver,
61
+ {
62
+ loadByQueryParam: ({props}) => props.queryParams.model_id
63
+ }
64
+ )
@@ -39,10 +39,7 @@ class ApiMakerTable extends React.PureComponent {
39
39
  appHistory: PropTypes.object,
40
40
  card: PropTypes.bool.isRequired,
41
41
  className: PropTypes.string,
42
- collection: PropTypes.oneOfType([
43
- instanceOfClassName("ApiMakerCollection"),
44
- PropTypes.instanceOf(Collection)
45
- ]),
42
+ collection: PropTypes.instanceOf(Collection),
46
43
  columns: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
47
44
  columnsContent: PropTypes.func,
48
45
  controls: PropTypes.func,
@@ -134,7 +131,7 @@ class ApiMakerTable extends React.PureComponent {
134
131
 
135
132
  render () {
136
133
  const {modelClass, noRecordsAvailableContent, noRecordsFoundContent} = digs(this.props, "modelClass", "noRecordsAvailableContent", "noRecordsFoundContent")
137
- const {collection, defaultParams, select, selectColumns} = this.props
134
+ const {collection, defaultParams, selectColumns} = this.props
138
135
  const {
139
136
  overallCount,
140
137
  preload,
@@ -1,49 +0,0 @@
1
- import AttributeRows from "../bootstrap/attribute-rows"
2
- import ConfigReader from "./config-reader"
3
- import {digg, digs} from "diggerize"
4
- import modelLoadWrapper from "../model-load-wrapper"
5
- import React from "react"
6
-
7
- class ApiMakerSuperAdminShowPage extends React.PureComponent {
8
- render() {
9
- const attributes = this.attributes()
10
- const model = this.model()
11
-
12
- return (
13
- <div className="super-admin--show-page">
14
- {attributes && model &&
15
- <AttributeRows attributes={attributes} model={model} />
16
- }
17
- </div>
18
- )
19
- }
20
-
21
- attributes() {
22
- const {modelClass} = digs(this.props, "modelClass")
23
- const configReader = ConfigReader.forModel(modelClass)
24
-
25
- return configReader.attributesToShow()
26
- }
27
-
28
- model() {
29
- const {modelClass} = digs(this.props, "modelClass")
30
- const camelizedLower = digg(modelClass.modelClassData(), "camelizedLower")
31
-
32
- return digg(this, "props", camelizedLower)
33
- }
34
- }
35
-
36
- const modelClassResolver = {callback: ({queryParams}) => {
37
- const modelClassName = digg(queryParams, "model")
38
- const modelClass = digg(require("../models.mjs.erb"), modelClassName)
39
-
40
- return modelClass
41
- }}
42
-
43
- export default modelLoadWrapper(
44
- ApiMakerSuperAdminShowPage,
45
- modelClassResolver,
46
- {
47
- loadByQueryParam: ({props}) => props.queryParams.model_id
48
- }
49
- )