@kaspernj/api-maker 1.0.266 → 1.0.268

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.266",
19
+ "version": "1.0.268",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -716,10 +716,16 @@ export default class BaseModel {
716
716
  try {
717
717
  return await CommandsPool.addCommand(args, commandArgs)
718
718
  } catch (error) {
719
- if (formOrDataObject?.nodeName == "FORM") {
720
- BaseModel.parseValidationErrors({error, options: {form: formOrDataObject}})
719
+ let form
720
+
721
+ if (commandArgs.form) {
722
+ form = commandArgs.form
723
+ } else if (formOrDataObject?.nodeName == "FORM") {
724
+ form = formOrDataObject
721
725
  }
722
726
 
727
+ if (form) BaseModel.parseValidationErrors({error, options: {form}})
728
+
723
729
  throw error
724
730
  }
725
731
  }
@@ -60,7 +60,6 @@ class ReflectionElement extends React.PureComponent {
60
60
  className="reflection-element"
61
61
  data-model-class={currentModelClass.modelClassData().name}
62
62
  data-reflection-name={reflection.name()}
63
- key={reflection.name()}
64
63
  onClick={digg(this, "onReflectionClicked")}
65
64
  >
66
65
  {currentModelClass.humanAttributeName(reflection.name())}
@@ -75,6 +74,42 @@ class ReflectionElement extends React.PureComponent {
75
74
  }
76
75
  }
77
76
 
77
+ class ScopeElement extends React.PureComponent {
78
+ static defaultProps = {
79
+ active: false
80
+ }
81
+
82
+ static propTypes = {
83
+ active: PropTypes.bool.isRequired,
84
+ scope: PropTypes.object.isRequired
85
+ }
86
+
87
+ render() {
88
+ const {active, scope} = this.props
89
+ const style = {}
90
+
91
+ if (active) style.fontWeight = "bold"
92
+
93
+ return (
94
+ <div
95
+ className="scope-element"
96
+ key={scope.name()}
97
+ onClick={digg(this, "onScopeClicked")}
98
+ style={style}
99
+ >
100
+ {scope.name()}
101
+ </div>
102
+ )
103
+ }
104
+
105
+ onScopeClicked = (e) => {
106
+ e.preventDefault()
107
+
108
+ this.props.onScopeClicked({scope: this.props.scope})
109
+ }
110
+ }
111
+
112
+
78
113
  export default class ApiMakerTableFiltersFilterForm extends React.PureComponent {
79
114
  static propTypes = PropTypesExact({
80
115
  filter: PropTypes.object,
@@ -88,6 +123,7 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
88
123
  path: this.props.filter.p || [],
89
124
  predicate: undefined,
90
125
  predicates: undefined,
126
+ scope: this.props.filter.sc,
91
127
  value: this.props.filter.v
92
128
  })
93
129
  valueInputRef = React.createRef()
@@ -114,7 +150,14 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
114
150
  render() {
115
151
  const {valueInputRef} = digs(this, "valueInputRef")
116
152
  const currentModelClass = this.currentModelClass()
117
- const {attribute, predicate, predicates, value} = digs(this.shape, "attribute", "predicate", "predicates", "value")
153
+ const {attribute, predicate, predicates, scope, value} = digs(this.shape, "attribute", "predicate", "scope", "predicates", "value")
154
+ let submitEnabled = false
155
+
156
+ if (attribute && predicate) {
157
+ submitEnabled = true
158
+ } else if (scope) {
159
+ submitEnabled = true
160
+ }
118
161
 
119
162
  return (
120
163
  <div className="api-maker--table--filters--filter-form">
@@ -153,13 +196,16 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
153
196
  />
154
197
  )}
155
198
  {currentModelClass.ransackableScopes().map((scope) =>
156
- <div key={scope.name()}>
157
- {scope.name()}
158
- </div>
199
+ <ScopeElement
200
+ active={scope.name() == this.shape.scope?.name()}
201
+ key={scope.name()}
202
+ scope={scope}
203
+ onScopeClicked={digg(this, "onScopeClicked")}
204
+ />
159
205
  )}
160
206
  </div>
161
207
  <div>
162
- {predicates &&
208
+ {predicates && !this.shape.scope &&
163
209
  <Select
164
210
  className="predicate-select"
165
211
  defaultValue={predicate?.name}
@@ -170,13 +216,13 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
170
216
  }
171
217
  </div>
172
218
  <div>
173
- {attribute && predicate &&
219
+ {((attribute && predicate) || scope) &&
174
220
  <Input className="value-input" defaultValue={value} inputRef={valueInputRef} />
175
221
  }
176
222
  </div>
177
223
  </div>
178
224
  <div>
179
- <button className="apply-filter-button" disabled={!attribute || !predicate}>
225
+ <button className="apply-filter-button" disabled={!submitEnabled}>
180
226
  {I18n.t("js.api_maker.table.filters.relationship_select.apply", {defaultValue: "Apply"})}
181
227
  </button>
182
228
  </div>
@@ -238,7 +284,11 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
238
284
  }
239
285
 
240
286
  onAttributeClicked = ({attribute}) => {
241
- this.shape.set({attribute})
287
+ this.shape.set({
288
+ attribute,
289
+ predicate: undefined,
290
+ scope: undefined
291
+ })
242
292
  }
243
293
 
244
294
  onPredicateChanged = (e) => {
@@ -253,27 +303,45 @@ export default class ApiMakerTableFiltersFilterForm extends React.PureComponent
253
303
 
254
304
  this.shape.set({
255
305
  attribute: undefined,
256
- path: newPath
306
+ path: newPath,
307
+ predicate: undefined
257
308
  })
258
309
 
259
310
  this.props.onPathChanged
260
311
  }
261
312
 
313
+ onScopeClicked = ({scope}) => {
314
+ this.shape.set({
315
+ attribute: undefined,
316
+ scope
317
+ })
318
+ }
319
+
262
320
  onSubmit = (e) => {
263
321
  e.preventDefault()
264
322
 
265
323
  const {filter, querySearchName} = digs(this.props, "filter", "querySearchName")
266
- const {attribute, path, predicate} = digs(this.shape, "attribute", "path", "predicate")
324
+ const {attribute, path, predicate, scope} = digs(this.shape, "attribute", "path", "predicate", "scope")
267
325
  const {filterIndex} = digs(filter, "filterIndex")
268
326
  const searchParams = Params.parse()[querySearchName] || {}
269
327
  const value = digg(this, "valueInputRef", "current", "value")
270
-
271
- searchParams[filterIndex] = JSON.stringify({
272
- a: attribute.name(),
328
+ const newSearchParams = {
273
329
  p: path,
274
- pre: digg(predicate, "name"),
275
330
  v: value
276
- })
331
+ }
332
+
333
+ if (attribute) {
334
+ newSearchParams.a = attribute.name()
335
+ newSearchParams.pre = digg(predicate, "name")
336
+ } else if (scope) {
337
+ newSearchParams.sc = inflection.underscore(scope.name())
338
+ } else {
339
+ throw new Error("Dont know if should search for attribute or scope?")
340
+ }
341
+
342
+ console.log({newSearchParams})
343
+
344
+ searchParams[filterIndex] = JSON.stringify(newSearchParams)
277
345
 
278
346
  const newParams = {}
279
347
 
@@ -17,7 +17,8 @@ class ApiMakerTableFilter extends React.PureComponent {
17
17
  }
18
18
 
19
19
  render() {
20
- const {a, p, pre, v} = digs(this.props, "a", "p", "pre", "v")
20
+ const {p, v} = digs(this.props, "p", "v")
21
+ const {a, pre, sc} = this.props
21
22
 
22
23
  return (
23
24
  <div style={{display: "inline-block", backgroundColor: "grey", padding: "10px 6px"}}>
@@ -25,7 +26,7 @@ class ApiMakerTableFilter extends React.PureComponent {
25
26
  {p.length > 0 &&
26
27
  `${p.join(".")}.`
27
28
  }
28
- {a} {pre} {v}
29
+ {a} {sc} {pre} {v}
29
30
  </span>
30
31
  <span>
31
32
  <a className="remove-filter-button" href="#" onClick={digg(this, "onRemoveFilterClicked")}>