@kaspernj/api-maker 1.0.146 → 1.0.147
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/.eslintrc.js +1 -0
- package/package.json +1 -1
- package/src/collection-loader.jsx +225 -0
package/.eslintrc.js
CHANGED
|
@@ -239,6 +239,7 @@ module.exports = {
|
|
|
239
239
|
"react/jsx-newline": "off",
|
|
240
240
|
"react/jsx-no-bind": "error",
|
|
241
241
|
"react/jsx-no-constructed-context-values": "error",
|
|
242
|
+
"react/jsx-no-leaked-render": "error",
|
|
242
243
|
"react/jsx-no-literals": "error",
|
|
243
244
|
"react/jsx-no-script-url": "error",
|
|
244
245
|
"react/jsx-no-useless-fragment": "error",
|
package/package.json
CHANGED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
const Collection = require("@kaspernj/api-maker/src/collection")
|
|
2
|
+
const {debounce} = require("debounce")
|
|
3
|
+
const {digg, digs} = require("diggerize")
|
|
4
|
+
const EventCreated = require("@kaspernj/api-maker/src/event-created").default
|
|
5
|
+
const EventDestroyed = require("@kaspernj/api-maker/src/event-destroyed").default
|
|
6
|
+
const EventUpdated = require("@kaspernj/api-maker/src/event-updated").default
|
|
7
|
+
const instanceOfClassName = require("@kaspernj/api-maker/src/instance-of-class-name")
|
|
8
|
+
const {LocationChanged} = require("on-location-changed/location-changed-component")
|
|
9
|
+
const Params = require("@kaspernj/api-maker/src/params")
|
|
10
|
+
const PropTypes = require("prop-types")
|
|
11
|
+
const React = require("react")
|
|
12
|
+
|
|
13
|
+
export default class CollectionLoader extends React.PureComponent {
|
|
14
|
+
static defaultProps = {
|
|
15
|
+
destroyEnabled: true,
|
|
16
|
+
noRecordsAvailableContent: undefined,
|
|
17
|
+
noRecordsFoundContent: undefined,
|
|
18
|
+
preloads: [],
|
|
19
|
+
select: {}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static propTypes = {
|
|
23
|
+
abilities: PropTypes.object,
|
|
24
|
+
appHistory: PropTypes.object,
|
|
25
|
+
className: PropTypes.string,
|
|
26
|
+
collection: PropTypes.oneOfType([
|
|
27
|
+
instanceOfClassName("ApiMakerCollection"),
|
|
28
|
+
PropTypes.instanceOf(Collection)
|
|
29
|
+
]),
|
|
30
|
+
component: PropTypes.object,
|
|
31
|
+
defaultParams: PropTypes.object,
|
|
32
|
+
groupBy: PropTypes.array,
|
|
33
|
+
modelClass: PropTypes.func.isRequired,
|
|
34
|
+
noRecordsAvailableContent: PropTypes.func,
|
|
35
|
+
noRecordsFoundContent: PropTypes.func,
|
|
36
|
+
onModelsLoaded: PropTypes.func,
|
|
37
|
+
paginateContent: PropTypes.func,
|
|
38
|
+
preloads: PropTypes.array.isRequired,
|
|
39
|
+
queryName: PropTypes.string,
|
|
40
|
+
select: PropTypes.object,
|
|
41
|
+
selectColumns: PropTypes.object
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
shape = digg(this, "props", "component", "shape")
|
|
45
|
+
|
|
46
|
+
constructor (props) {
|
|
47
|
+
super(props)
|
|
48
|
+
|
|
49
|
+
let queryName = props.queryName
|
|
50
|
+
|
|
51
|
+
if (!queryName) queryName = digg(props.modelClass.modelClassData(), "collectionKey")
|
|
52
|
+
|
|
53
|
+
this.shape.set({
|
|
54
|
+
models: undefined,
|
|
55
|
+
overallCount: undefined,
|
|
56
|
+
query: undefined,
|
|
57
|
+
queryName,
|
|
58
|
+
queryQName: `${queryName}_q`,
|
|
59
|
+
queryPageName: `${queryName}_page`,
|
|
60
|
+
qParams: undefined,
|
|
61
|
+
result: undefined,
|
|
62
|
+
showNoRecordsAvailableContent: false,
|
|
63
|
+
showNoRecordsFoundContent: false
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
componentDidMount () {
|
|
68
|
+
this.loadQParams()
|
|
69
|
+
this.loadModels()
|
|
70
|
+
|
|
71
|
+
const {noRecordsAvailableContent} = digs(this.props, "noRecordsAvailableContent")
|
|
72
|
+
|
|
73
|
+
if (noRecordsAvailableContent) this.loadOverallCount()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async loadOverallCount () {
|
|
77
|
+
const baseQuery = this.props.collection || this.props.modelClass.all()
|
|
78
|
+
const overallCount = await baseQuery.count()
|
|
79
|
+
|
|
80
|
+
this.shape.set({
|
|
81
|
+
overallCount,
|
|
82
|
+
showNoRecordsAvailableContent: this.showNoRecordsAvailableContent({overallCount}),
|
|
83
|
+
showNoRecordsFoundContent: this.showNoRecordsFoundContent({overallCount})
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
loadQParams () {
|
|
88
|
+
const {queryQName} = digs(this.shape, "queryQName")
|
|
89
|
+
const params = Params.parse()
|
|
90
|
+
const qParams = Object.assign({}, this.props.defaultParams, params[queryQName])
|
|
91
|
+
|
|
92
|
+
this.shape.set({qParams})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
loadModelsDebounce = debounce(this.loadModels)
|
|
96
|
+
|
|
97
|
+
loadModels = async () => {
|
|
98
|
+
const params = Params.parse()
|
|
99
|
+
const {abilities, collection, groupBy, modelClass, onModelsLoaded, preloads, select, selectColumns} = this.props
|
|
100
|
+
const {qParams, queryPageName, queryQName} = digs(this.shape, "qParams", "queryPageName", "queryQName")
|
|
101
|
+
|
|
102
|
+
let query = collection?.clone() || modelClass
|
|
103
|
+
|
|
104
|
+
if (groupBy) query = query.groupBy(groupBy)
|
|
105
|
+
|
|
106
|
+
query = query
|
|
107
|
+
.ransack(qParams)
|
|
108
|
+
.searchKey(queryQName)
|
|
109
|
+
.page(params[queryPageName])
|
|
110
|
+
.pageKey(queryPageName)
|
|
111
|
+
.preload(preloads)
|
|
112
|
+
.select(select)
|
|
113
|
+
|
|
114
|
+
if (abilities) query = query.abilities(abilities)
|
|
115
|
+
if (selectColumns) query = query.selectColumns(selectColumns)
|
|
116
|
+
|
|
117
|
+
const result = await query.result()
|
|
118
|
+
const models = result.models()
|
|
119
|
+
|
|
120
|
+
if (onModelsLoaded) {
|
|
121
|
+
onModelsLoaded({
|
|
122
|
+
models,
|
|
123
|
+
qParams,
|
|
124
|
+
query,
|
|
125
|
+
result
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.shape.set({
|
|
130
|
+
query,
|
|
131
|
+
result,
|
|
132
|
+
models: result.models(),
|
|
133
|
+
showNoRecordsAvailableContent: this.showNoRecordsAvailableContent({models}),
|
|
134
|
+
showNoRecordsFoundContent: this.showNoRecordsFoundContent({models})
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
onModelCreated = () => this.loadModels()
|
|
139
|
+
|
|
140
|
+
onModelDestroyed = (args) => {
|
|
141
|
+
const {models} = digs(this.shape, "models")
|
|
142
|
+
|
|
143
|
+
this.shape.set({
|
|
144
|
+
models: models.filter((model) => model.id() != args.model.id())
|
|
145
|
+
})
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
onModelUpdated = (args) => {
|
|
149
|
+
const {models} = digs(this.shape, "models")
|
|
150
|
+
const updatedModel = digg(args, "model")
|
|
151
|
+
const foundModel = models.find((model) => model.id() == updatedModel.id())
|
|
152
|
+
|
|
153
|
+
if (foundModel) this.loadModelsDebounce()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
onLocationChanged = () => {
|
|
157
|
+
const {queryQName} = digs(this.shape, "queryQName")
|
|
158
|
+
const params = Params.parse()
|
|
159
|
+
const qParams = Object.assign({}, this.props.defaultParams, params[queryQName])
|
|
160
|
+
|
|
161
|
+
this.shape.set({qParams})
|
|
162
|
+
this.loadModels()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
showNoRecordsAvailableContent (args) {
|
|
166
|
+
const {noRecordsAvailableContent} = digs(this.props, "noRecordsAvailableContent")
|
|
167
|
+
let models, overallCount
|
|
168
|
+
|
|
169
|
+
if (args.models !== undefined) {
|
|
170
|
+
models = args.models
|
|
171
|
+
} else if (this.shape.models !== undefined) {
|
|
172
|
+
models = this.shape.models
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (args.overallCount !== undefined) {
|
|
176
|
+
overallCount = args.overallCount
|
|
177
|
+
} else if (this.shape.overallCount !== undefined) {
|
|
178
|
+
overallCount = this.shape.overallCount
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (models === undefined || overallCount === undefined || noRecordsAvailableContent === undefined) return false
|
|
182
|
+
if (models.length === 0 && overallCount === 0 && noRecordsAvailableContent) return true
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
showNoRecordsFoundContent (args) {
|
|
186
|
+
const {noRecordsAvailableContent, noRecordsFoundContent} = digs(this.props, "noRecordsAvailableContent", "noRecordsFoundContent")
|
|
187
|
+
let models, overallCount
|
|
188
|
+
|
|
189
|
+
if (args.models !== undefined) {
|
|
190
|
+
models = args.models
|
|
191
|
+
} else if (this.shape.models !== undefined) {
|
|
192
|
+
models = this.shape.models
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (args.overallCount !== undefined) {
|
|
196
|
+
overallCount = args.overallCount
|
|
197
|
+
} else if (this.shape.overallCount !== undefined) {
|
|
198
|
+
overallCount = this.shape.overallCount
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (models === undefined || noRecordsFoundContent === undefined) return false
|
|
202
|
+
|
|
203
|
+
// Dont show noRecordsAvailableContent together with noRecordsAvailableContent
|
|
204
|
+
if (models.length === 0 && overallCount === 0 && noRecordsAvailableContent) return false
|
|
205
|
+
if (models.length === 0 && noRecordsFoundContent) return true
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
render() {
|
|
209
|
+
const {modelClass} = digs(this.props, "modelClass")
|
|
210
|
+
const {models} = digs(this.shape, "models")
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<>
|
|
214
|
+
<EventCreated modelClass={modelClass} onCreated={digg(this, "onModelCreated")} />
|
|
215
|
+
<LocationChanged onChanged={digg(this, "onLocationChanged")} />
|
|
216
|
+
{models && models.map((model) =>
|
|
217
|
+
<React.Fragment key={model.id()}>
|
|
218
|
+
<EventDestroyed model={model} onDestroyed={digg(this, "onModelDestroyed")} />
|
|
219
|
+
<EventUpdated model={model} onUpdated={digg(this, "onModelUpdated")} />
|
|
220
|
+
</React.Fragment>
|
|
221
|
+
)}
|
|
222
|
+
</>
|
|
223
|
+
)
|
|
224
|
+
}
|
|
225
|
+
}
|