@kaspernj/api-maker 1.0.374 → 1.0.375

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kaspernj/api-maker",
3
- "version": "1.0.374",
3
+ "version": "1.0.375",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -1,3 +1,4 @@
1
+ import Column from "./column.mjs"
1
2
  import {digg} from "diggerize"
2
3
 
3
4
  export default class ApiMakerBaseModelAttribute {
@@ -5,10 +6,20 @@ export default class ApiMakerBaseModelAttribute {
5
6
  this.attributeData = attributeData
6
7
  }
7
8
 
8
- isColumn() {
9
- return Boolean(digg(this, "attributeData", "column"))
9
+ getColumn() {
10
+ if (!this.column) {
11
+ const columnData = digg(this, "attributeData", "column")
12
+
13
+ if (columnData) {
14
+ this.column = new Column(columnData)
15
+ }
16
+ }
17
+
18
+ return this.column
10
19
  }
11
20
 
21
+ isColumn = () => Boolean(digg(this, "attributeData", "column"))
22
+
12
23
  isSelectedByDefault() {
13
24
  const isSelectedByDefault = digg(this, "attributeData", "selected_by_default")
14
25
 
@@ -17,7 +28,5 @@ export default class ApiMakerBaseModelAttribute {
17
28
  return false
18
29
  }
19
30
 
20
- name() {
21
- return digg(this, "attributeData", "name")
22
- }
31
+ name = () => digg(this, "attributeData", "name")
23
32
  }
@@ -0,0 +1,13 @@
1
+ import {digg} from "diggerize"
2
+
3
+ export default class ApiMakerBaseModelColumn {
4
+ constructor(columnData) {
5
+ if (!columnData) {
6
+ throw new Error("No column data was given")
7
+ }
8
+
9
+ this.columnData = columnData
10
+ }
11
+
12
+ getType = () => digg(this, "columnData", "type")
13
+ }
@@ -1,11 +1,13 @@
1
1
  import classNames from "classnames"
2
2
  import {digg} from "diggerize"
3
+ import * as inflection from "inflection"
3
4
  import MoneyFormatter from "../../money-formatter"
4
5
  import PropTypes from "prop-types"
5
- import React from "react"
6
+ import {memo, useMemo} from "react"
7
+ import {shapeComponent, ShapeComponent} from "set-state-compare/src/shape-component.js"
6
8
  import strftime from "strftime"
7
9
 
8
- export default class ApiMakerBootstrapAttributeRow extends React.PureComponent {
10
+ export default memo(shapeComponent(class ApiMakerBootstrapAttributeRow extends ShapeComponent {
9
11
  static defaultProps = {
10
12
  checkIfAttributeLoaded: false
11
13
  }
@@ -20,6 +22,13 @@ export default class ApiMakerBootstrapAttributeRow extends React.PureComponent {
20
22
  value: PropTypes.node
21
23
  }
22
24
 
25
+ setup() {
26
+ this.attribute = useMemo(
27
+ () => this.props.model?.constructor?.attributes()?.find((attribute) => attribute.name() == inflection.underscore(this.props.attribute)),
28
+ [this.props.attribute, this.props.model]
29
+ )
30
+ }
31
+
23
32
  render () {
24
33
  const {attribute, checkIfAttributeLoaded, children, className, identifier, label, model, value, ...restProps} = this.props
25
34
 
@@ -68,7 +77,11 @@ export default class ApiMakerBootstrapAttributeRow extends React.PureComponent {
68
77
  }
69
78
 
70
79
  valueContent(value) {
71
- if (value instanceof Date) {
80
+ const columnType = this.attribute?.getColumn()?.getType()
81
+
82
+ if (columnType == "date") {
83
+ return I18n.l("date.formats.default", value)
84
+ } else if (value instanceof Date) {
72
85
  return strftime("%Y-%m-%d %H:%M", value)
73
86
  } else if (typeof value === "boolean") {
74
87
  if (value) return I18n.t("js.shared.yes", {defaultValue: "Yes"})
@@ -80,4 +93,4 @@ export default class ApiMakerBootstrapAttributeRow extends React.PureComponent {
80
93
  return value
81
94
  }
82
95
  }
83
- }
96
+ }))
@@ -105,8 +105,8 @@ export default memo(shapeComponent(class ApiMakerBootStrapLiveTableModelRow exte
105
105
  }
106
106
 
107
107
  columnsContentFromAttributeAndPath (column, model) {
108
- const {attribute} = digs(column, "attribute")
109
- const currentModelClass = this.props.modelClass
108
+ const {attribute: attributeName} = digs(column, "attribute")
109
+ const attributeNameUnderscore = inflection.underscore(attributeName)
110
110
  const path = column.path || []
111
111
  let value
112
112
  let currentModel = model
@@ -118,8 +118,18 @@ export default memo(shapeComponent(class ApiMakerBootStrapLiveTableModelRow exte
118
118
  }
119
119
  }
120
120
 
121
- if (!(attribute in currentModel)) throw new Error(`${currentModelClass.modelName().name} doesn't respond to ${attribute}`)
122
- if (currentModel.isAttributeLoaded(attribute)) value = currentModel[attribute]()
121
+ if (!(attributeName in currentModel)) {
122
+ throw new Error(`${currentModel.constructor.modelName().human()} doesn't respond to ${attributeName}`)
123
+ }
124
+
125
+ if (currentModel.isAttributeLoaded(attributeName)) value = currentModel[attributeName]()
126
+
127
+ const attribute = currentModel.constructor.attributes().find((attribute) => attribute.name() == attributeNameUnderscore)
128
+ const modelColumn = attribute?.getColumn()
129
+
130
+ if (modelColumn?.getType() == "date") {
131
+ return this.presentDateTime({apiMakerType: "date", value})
132
+ }
123
133
 
124
134
  return this.presentColumnValue(value)
125
135
  }
@@ -155,9 +165,9 @@ export default memo(shapeComponent(class ApiMakerBootStrapLiveTableModelRow exte
155
165
  }
156
166
  }
157
167
 
158
- presentColumnValue (value) {
168
+ presentColumnValue(value) {
159
169
  if (value instanceof Date) {
160
- return this.presentDateTime(value)
170
+ return this.presentDateTime({value})
161
171
  } else if (MoneyFormatter.isMoney(value)) {
162
172
  return MoneyFormatter.format(value)
163
173
  } else if (typeof value == "boolean") {
@@ -174,15 +184,13 @@ export default memo(shapeComponent(class ApiMakerBootStrapLiveTableModelRow exte
174
184
  return value
175
185
  }
176
186
 
177
- presentDateTime(value) {
178
- const apiMakerType = value.apiMakerType || "time"
179
-
180
- if (apiMakerType == "time") {
187
+ presentDateTime({apiMakerType, value}) {
188
+ if (!apiMakerType || apiMakerType == "time") {
181
189
  const dateTimeFormatName = this.props.liveTable.props.defaultDateTimeFormatName || "time.formats.default"
182
190
 
183
191
  return I18n.l(dateTimeFormatName, value)
184
192
  } else if (apiMakerType == "date") {
185
- const dateFormatName = this.props.liveTable.props.defaultDateTimeFormatName || "date.formats.default"
193
+ const dateFormatName = this.props.liveTable.props.defaultDateFormatName || "date.formats.default"
186
194
 
187
195
  return I18n.l(dateFormatName, value)
188
196
  } else {