@kaspernj/api-maker 1.0.355 → 1.0.357
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 +1 -1
- package/src/use-collection.mjs +23 -42
package/package.json
CHANGED
package/src/use-collection.mjs
CHANGED
|
@@ -6,8 +6,8 @@ import {useCallback, useEffect} from "react"
|
|
|
6
6
|
import useShape from "set-state-compare/src/use-shape.js"
|
|
7
7
|
import useQueryParams from "on-location-changed/src/use-query-params.js"
|
|
8
8
|
|
|
9
|
-
const useCollection = (
|
|
10
|
-
{
|
|
9
|
+
const useCollection = (props, cacheKeys = []) => {
|
|
10
|
+
const {
|
|
11
11
|
abilities,
|
|
12
12
|
collection,
|
|
13
13
|
defaultParams,
|
|
@@ -21,41 +21,22 @@ const useCollection = (
|
|
|
21
21
|
pagination = false,
|
|
22
22
|
preloads = [],
|
|
23
23
|
queryMethod,
|
|
24
|
-
queryName,
|
|
24
|
+
queryName: initialQueryName,
|
|
25
25
|
ransack,
|
|
26
26
|
select = {},
|
|
27
27
|
selectColumns,
|
|
28
28
|
...restProps
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
) => {
|
|
29
|
+
} = props
|
|
30
|
+
|
|
32
31
|
if (Object.keys(restProps).length > 0) {
|
|
33
32
|
throw new Error(`Unknown props given to useCollection: ${Object.keys(restProps).join(", ")}`)
|
|
34
33
|
}
|
|
35
34
|
|
|
36
|
-
const s = useShape(
|
|
37
|
-
|
|
38
|
-
collection,
|
|
39
|
-
defaultParams,
|
|
40
|
-
groupBy,
|
|
41
|
-
ifCondition,
|
|
42
|
-
limit,
|
|
43
|
-
modelClass,
|
|
44
|
-
noRecordsAvailableContent,
|
|
45
|
-
noRecordsFoundContent,
|
|
46
|
-
onModelsLoaded,
|
|
47
|
-
pagination,
|
|
48
|
-
preloads,
|
|
49
|
-
queryMethod,
|
|
50
|
-
ransack,
|
|
51
|
-
select,
|
|
52
|
-
selectColumns
|
|
53
|
-
})
|
|
35
|
+
const s = useShape(props)
|
|
36
|
+
const queryName = initialQueryName || digg(modelClass.modelClassData(), "collectionKey")
|
|
54
37
|
|
|
55
38
|
s.meta.queryParams = useQueryParams()
|
|
56
39
|
|
|
57
|
-
if (!queryName) queryName = digg(modelClass.modelClassData(), "collectionKey")
|
|
58
|
-
|
|
59
40
|
const hasQParams = useCallback(() => {
|
|
60
41
|
if (s.s.queryQName in s.m.queryParams) return true
|
|
61
42
|
|
|
@@ -108,7 +89,7 @@ const useCollection = (
|
|
|
108
89
|
}, [])
|
|
109
90
|
|
|
110
91
|
const loadQParams = useCallback(() => {
|
|
111
|
-
const qParamsToSet = hasQParams() ? qParams() : Object.assign({}, s.
|
|
92
|
+
const qParamsToSet = hasQParams() ? qParams() : Object.assign({}, s.props.defaultParams)
|
|
112
93
|
const searchParams = []
|
|
113
94
|
|
|
114
95
|
if (s.m.queryParams[s.s.querySName]) {
|
|
@@ -126,9 +107,9 @@ const useCollection = (
|
|
|
126
107
|
}, [])
|
|
127
108
|
|
|
128
109
|
const loadModels = useCallback(async () => {
|
|
129
|
-
let query = s.
|
|
110
|
+
let query = s.props.collection?.clone() || s.p.modelClass.ransack()
|
|
130
111
|
|
|
131
|
-
if (s.
|
|
112
|
+
if (s.props.pagination) {
|
|
132
113
|
const page = s.m.queryParams[s.s.queryPageName] || 1
|
|
133
114
|
let per = s.m.queryParams[s.s.queryPerKey] || 30
|
|
134
115
|
|
|
@@ -141,25 +122,25 @@ const useCollection = (
|
|
|
141
122
|
query.page(page).per(per)
|
|
142
123
|
}
|
|
143
124
|
|
|
144
|
-
if (s.
|
|
125
|
+
if (s.props.groupBy) query = query.groupBy(...s.p.groupBy)
|
|
145
126
|
|
|
146
127
|
query = query
|
|
147
128
|
.ransack(s.s.qParams)
|
|
148
|
-
.ransack(s.props.ransack)
|
|
149
129
|
.search(s.s.searchParams)
|
|
150
130
|
.searchKey(s.s.queryQName)
|
|
151
131
|
.pageKey(s.s.queryPageName)
|
|
152
132
|
.perKey(s.s.queryPerKey)
|
|
153
|
-
.preload(s.p.preloads)
|
|
154
|
-
.select(s.p.select)
|
|
155
133
|
|
|
156
|
-
if (s.
|
|
157
|
-
if (s.
|
|
158
|
-
if (s.
|
|
134
|
+
if (s.props.abilities) query.abilities(s.p.abilities)
|
|
135
|
+
if (s.props.limit !== undefined) query.limit(s.p.limit)
|
|
136
|
+
if (s.props.preloads) query.preload(s.p.preloads)
|
|
137
|
+
if (s.props.ransack) query.ransack(s.props.ransack)
|
|
138
|
+
if (s.props.select) query.select(s.p.select)
|
|
139
|
+
if (s.props.selectColumns) query.selectColumns(s.p.selectColumns)
|
|
159
140
|
|
|
160
141
|
let result
|
|
161
142
|
|
|
162
|
-
if (s.
|
|
143
|
+
if (s.props.queryMethod) {
|
|
163
144
|
result = await s.p.queryMethod({query})
|
|
164
145
|
} else {
|
|
165
146
|
result = await query.result()
|
|
@@ -167,7 +148,7 @@ const useCollection = (
|
|
|
167
148
|
|
|
168
149
|
const models = result.models()
|
|
169
150
|
|
|
170
|
-
if (s.
|
|
151
|
+
if (s.props.onModelsLoaded) {
|
|
171
152
|
s.p.onModelsLoaded({
|
|
172
153
|
models,
|
|
173
154
|
qParams: s.s.qParams,
|
|
@@ -233,11 +214,11 @@ const useCollection = (
|
|
|
233
214
|
overallCount = s.s.overallCount
|
|
234
215
|
}
|
|
235
216
|
|
|
236
|
-
if (models === undefined || s.
|
|
217
|
+
if (models === undefined || s.props.noRecordsFoundContent === undefined) return false
|
|
237
218
|
|
|
238
219
|
// Dont show noRecordsAvailableContent together with noRecordsAvailableContent
|
|
239
|
-
if (models.length === 0 && overallCount === 0 && s.
|
|
240
|
-
if (models.length === 0 && s.
|
|
220
|
+
if (models.length === 0 && overallCount === 0 && s.props.noRecordsAvailableContent) return false
|
|
221
|
+
if (models.length === 0 && s.props.noRecordsFoundContent) return true
|
|
241
222
|
}, [])
|
|
242
223
|
|
|
243
224
|
useEffect(
|
|
@@ -258,7 +239,7 @@ const useCollection = (
|
|
|
258
239
|
)
|
|
259
240
|
|
|
260
241
|
useEffect(() => {
|
|
261
|
-
if (s.
|
|
242
|
+
if (s.props.noRecordsAvailableContent) loadOverallCount()
|
|
262
243
|
}, [])
|
|
263
244
|
|
|
264
245
|
const onCreated = useCallback(() => {
|