@kaspernj/api-maker 1.0.419 → 1.0.420

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.419",
3
+ "version": "1.0.420",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "main": "index.js",
@@ -9,12 +9,12 @@ import Result from "./result.mjs"
9
9
  export default class ApiMakerCollection {
10
10
  static apiMakerType = "Collection"
11
11
 
12
- constructor (args, queryArgs = {}) {
12
+ constructor(args, queryArgs = {}) {
13
13
  this.queryArgs = queryArgs
14
14
  this.args = args
15
15
  }
16
16
 
17
- abilities (originalAbilities) {
17
+ abilities(originalAbilities) {
18
18
  const newAbilities = {}
19
19
 
20
20
  for (const originalAbilityName in originalAbilities) {
@@ -33,21 +33,21 @@ export default class ApiMakerCollection {
33
33
  return this._merge({abilities: newAbilities})
34
34
  }
35
35
 
36
- accessibleBy (abilityName) {
36
+ accessibleBy(abilityName) {
37
37
  return this._merge({accessibleBy: inflection.underscore(abilityName)})
38
38
  }
39
39
 
40
- async count () {
40
+ async count() {
41
41
  const response = await this.clone()._merge({count: true})._response()
42
42
 
43
43
  return digg(response, "count")
44
44
  }
45
45
 
46
- distinct () {
46
+ distinct() {
47
47
  return this._merge({distinct: true})
48
48
  }
49
49
 
50
- async each (callback) {
50
+ async each(callback) {
51
51
  const array = await this.toArray()
52
52
 
53
53
  for (const model in array) {
@@ -55,12 +55,24 @@ export default class ApiMakerCollection {
55
55
  }
56
56
  }
57
57
 
58
- async first () {
58
+ except(...keys) {
59
+ for (const key of keys) {
60
+ if (key == "page") {
61
+ delete this.queryArgs[key]
62
+ } else {
63
+ throw new Error(`Unhandled key: ${key}`)
64
+ }
65
+ }
66
+
67
+ return this
68
+ }
69
+
70
+ async first() {
59
71
  const models = await this.toArray()
60
72
  return models[0]
61
73
  }
62
74
 
63
- groupBy (...arrayOfTablesAndColumns) {
75
+ groupBy(...arrayOfTablesAndColumns) {
64
76
  const arrayOfTablesAndColumnsWithLowercaseColumns = arrayOfTablesAndColumns.map((tableAndColumn) => {
65
77
  if (Array.isArray(tableAndColumn)) {
66
78
  return [tableAndColumn[0], tableAndColumn[1].toLowerCase()]
@@ -76,18 +88,18 @@ export default class ApiMakerCollection {
76
88
  })
77
89
  }
78
90
 
79
- isLoaded () {
91
+ isLoaded() {
80
92
  if (this.args.reflectionName in this.args.model.relationshipsCache)
81
93
  return true
82
94
 
83
95
  return false
84
96
  }
85
97
 
86
- limit (amount) {
98
+ limit(amount) {
87
99
  return this._merge({limit: amount})
88
100
  }
89
101
 
90
- preloaded () {
102
+ preloaded() {
91
103
  if (!(this.args.reflectionName in this.args.model.relationshipsCache)) {
92
104
  throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
93
105
  }
@@ -95,7 +107,7 @@ export default class ApiMakerCollection {
95
107
  return this.args.model.relationshipsCache[this.args.reflectionName]
96
108
  }
97
109
 
98
- loaded () {
110
+ loaded() {
99
111
  const {model, reflectionName} = this.args
100
112
 
101
113
  if (reflectionName in model.relationships) {
@@ -135,22 +147,22 @@ export default class ApiMakerCollection {
135
147
  forEach = (...args) => this.loaded().forEach(...args)
136
148
  map = (...args) => this.loaded().map(...args)
137
149
 
138
- preload (preloadValue) {
150
+ preload(preloadValue) {
139
151
  return this._merge({preload: preloadValue})
140
152
  }
141
153
 
142
- page (page) {
154
+ page(page) {
143
155
  if (!page)
144
156
  page = 1
145
157
 
146
158
  return this._merge({page})
147
159
  }
148
160
 
149
- pageKey (pageKey) {
161
+ pageKey(pageKey) {
150
162
  return this._merge({pageKey})
151
163
  }
152
164
 
153
- params () {
165
+ params() {
154
166
  let params = {}
155
167
 
156
168
  if (this.queryArgs.params) params = incorporate(params, this.queryArgs.params)
@@ -171,20 +183,20 @@ export default class ApiMakerCollection {
171
183
  return params
172
184
  }
173
185
 
174
- per (per) {
186
+ per(per) {
175
187
  return this._merge({per})
176
188
  }
177
189
 
178
- perKey (perKey) {
190
+ perKey(perKey) {
179
191
  return this._merge({perKey})
180
192
  }
181
193
 
182
- ransack (params) {
194
+ ransack(params) {
183
195
  if (params) this._merge({ransack: params})
184
196
  return this
185
197
  }
186
198
 
187
- async result () {
199
+ async result() {
188
200
  const response = await this._response()
189
201
  const models = digg(response, "collection")
190
202
 
@@ -242,11 +254,11 @@ export default class ApiMakerCollection {
242
254
  return this._merge({selectColumns: newSelect})
243
255
  }
244
256
 
245
- sort (sortBy) {
257
+ sort(sortBy) {
246
258
  return this._merge({ransack: {s: sortBy}})
247
259
  }
248
260
 
249
- async toArray () {
261
+ async toArray() {
250
262
  const response = await this._response()
251
263
  const models = digg(response, "collection")
252
264
 
@@ -255,13 +267,13 @@ export default class ApiMakerCollection {
255
267
  return models
256
268
  }
257
269
 
258
- modelClass () {
270
+ modelClass() {
259
271
  const modelName = digg(this.args.modelClass.modelClassData(), "name")
260
272
 
261
273
  return modelClassRequire(modelName)
262
274
  }
263
275
 
264
- clone () {
276
+ clone() {
265
277
  const clonedQueryArgs = cloneDeep(this.queryArgs)
266
278
 
267
279
  return new ApiMakerCollection(this.args, clonedQueryArgs)
@@ -274,13 +286,13 @@ export default class ApiMakerCollection {
274
286
  }
275
287
  }
276
288
 
277
- _merge (newQueryArgs) {
289
+ _merge(newQueryArgs) {
278
290
  incorporate(this.queryArgs, newQueryArgs)
279
291
 
280
292
  return this
281
293
  }
282
294
 
283
- _response () {
295
+ _response() {
284
296
  const modelClassData = this.args.modelClass.modelClassData()
285
297
 
286
298
  return CommandsPool.addCommand(
@@ -183,6 +183,10 @@ export default memo(shapeComponent(class ApiMakerTable extends BaseComponent {
183
183
  select,
184
184
  selectColumns: this.props.selectColumns
185
185
  })
186
+ this.queryWithoutPagination = useMemo(
187
+ () => this.collection?.query?.clone()?.except("page"),
188
+ [this.collection.query]
189
+ )
186
190
  }
187
191
 
188
192
  async loadCurrentWorkplace() {
@@ -509,7 +513,7 @@ export default memo(shapeComponent(class ApiMakerTable extends BaseComponent {
509
513
  }
510
514
 
511
515
  listHeaderComponent = () => {
512
- const {query} = digs(this.collection, "query")
516
+ const {queryWithoutPagination} = this.tt
513
517
 
514
518
  return (
515
519
  <Row dataSet={{class: "live-table-header-row"}} style={this.styleForRow()}>
@@ -517,7 +521,7 @@ export default memo(shapeComponent(class ApiMakerTable extends BaseComponent {
517
521
  <Header style={this.styleForHeader({style: {width: 41}})}>
518
522
  <WorkerPluginsCheckAllCheckbox
519
523
  currentWorkplace={this.s.currentWorkplace}
520
- query={query}
524
+ query={queryWithoutPagination}
521
525
  style={{marginHorizontal: "auto"}}
522
526
  />
523
527
  </Header>
@@ -1,39 +1,47 @@
1
+ import {memo, useEffect, useMemo, useRef} from "react"
2
+ import BaseComponent from "../base-component"
1
3
  import classNames from "classnames"
2
4
  import Collection from "../collection.mjs"
3
- import EventConnection from "../event-connection"
4
5
  import PropTypes from "prop-types"
5
6
  import propTypesExact from "prop-types-exact"
6
- import React from "react"
7
+ import {shapeComponent} from "set-state-compare/src/shape-component"
7
8
  import {simpleObjectDifferent} from "set-state-compare/src/diff-utils"
8
- import {useEffect, useRef} from "react"
9
+ import useModelEvent from "../use-model-event.js"
9
10
 
10
- const Checkbox = (props) => {
11
- const {indeterminate, ...restProps} = props
12
- const checkboxRef = useRef()
11
+ const Checkbox = memo(shapeComponent(class Checkbox extends BaseComponent {
12
+ render() {
13
+ const {indeterminate, ...restProps} = this.props
14
+ const checkboxRef = useRef()
13
15
 
14
- useEffect(() => {
15
- checkboxRef.current.indeterminate = indeterminate
16
- })
16
+ useEffect(() => {
17
+ checkboxRef.current.indeterminate = indeterminate
18
+ })
17
19
 
18
- return (
19
- <input ref={checkboxRef} type="checkbox" {...restProps} />
20
- )
21
- }
20
+ return (
21
+ <input ref={checkboxRef} type="checkbox" {...restProps} />
22
+ )
23
+ }
24
+ }))
22
25
 
23
- export default class ApiMakerTableWorkerPluginsCheckAllCheckbox extends React.PureComponent {
26
+ export default memo(shapeComponent(class ApiMakerTableWorkerPluginsCheckAllCheckbox extends BaseComponent {
24
27
  static propTypes = propTypesExact({
25
28
  currentWorkplace: PropTypes.object,
26
29
  query: PropTypes.instanceOf(Collection),
27
30
  style: PropTypes.object
28
31
  })
29
32
 
30
- state = {
31
- checked: false,
32
- indeterminate: false
33
- }
33
+ setup() {
34
+ this.useStates({
35
+ checked: false,
36
+ indeterminate: false
37
+ })
38
+
39
+ useMemo(() => {
40
+ this.updateAllChecked()
41
+ }, [])
34
42
 
35
- componentDidMount() {
36
- this.updateAllChecked()
43
+ useModelEvent(this.props.currentWorkplace, "workplace_links_created", this.tt.onLinksCreated)
44
+ useModelEvent(this.props.currentWorkplace, "workplace_links_destroyed", this.tt.onLinksDestroyed)
37
45
  }
38
46
 
39
47
  componentDidUpdate(prevProps) {
@@ -58,21 +66,17 @@ export default class ApiMakerTableWorkerPluginsCheckAllCheckbox extends React.Pu
58
66
  }
59
67
 
60
68
  render() {
61
- const {className, currentWorkplace, style} = this.props
69
+ const {className, style} = this.props
62
70
  const {checked, indeterminate} = this.state
63
71
 
64
72
  return (
65
- <>
66
- <EventConnection event="workplace_links_created" model={currentWorkplace} onCall={this.onLinksCreated} />
67
- <EventConnection event="workplace_links_destroyed" model={currentWorkplace} onCall={this.onLinksDestroyed} />
68
- <Checkbox
69
- checked={checked}
70
- className={classNames("api-maker--table--worker-plugins-check-all-checkbox", className)}
71
- indeterminate={indeterminate}
72
- onChange={this.onCheckedChanged}
73
- style={style}
74
- />
75
- </>
73
+ <Checkbox
74
+ checked={checked}
75
+ className={classNames("api-maker--table--worker-plugins-check-all-checkbox", className)}
76
+ indeterminate={indeterminate}
77
+ onChange={this.onCheckedChanged}
78
+ style={style}
79
+ />
76
80
  )
77
81
  }
78
82
 
@@ -104,4 +108,4 @@ export default class ApiMakerTableWorkerPluginsCheckAllCheckbox extends React.Pu
104
108
  this.updateAllChecked()
105
109
  }
106
110
  }
107
- }
111
+ }))