@fleetbase/ember-core 0.2.10 → 0.2.11
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/addon/services/crud.js +110 -0
- package/package.json +1 -1
package/addon/services/crud.js
CHANGED
|
@@ -40,6 +40,11 @@ export default class CrudService extends Service {
|
|
|
40
40
|
*/
|
|
41
41
|
@service store;
|
|
42
42
|
|
|
43
|
+
/**
|
|
44
|
+
* @service currentUser
|
|
45
|
+
*/
|
|
46
|
+
@service currentUser;
|
|
47
|
+
|
|
43
48
|
/**
|
|
44
49
|
* Generic deletion modal with options
|
|
45
50
|
*
|
|
@@ -241,4 +246,109 @@ export default class CrudService extends Service {
|
|
|
241
246
|
...options,
|
|
242
247
|
});
|
|
243
248
|
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Prompts a spreadsheet upload for an import process.
|
|
252
|
+
*
|
|
253
|
+
* @param {String} modelName
|
|
254
|
+
* @param {Object} [options={}]
|
|
255
|
+
* @memberof CrudService
|
|
256
|
+
*/
|
|
257
|
+
@action import(modelName, options = {}) {
|
|
258
|
+
// always lowercase modelname
|
|
259
|
+
modelName = modelName.toLowerCase();
|
|
260
|
+
|
|
261
|
+
// set the model uri endpoint
|
|
262
|
+
const modelEndpoint = dasherize(pluralize(modelName));
|
|
263
|
+
|
|
264
|
+
// function to check if queue is empty
|
|
265
|
+
const checkQueue = () => {
|
|
266
|
+
const uploadQueue = this.modalsManager.getOption('uploadQueue');
|
|
267
|
+
|
|
268
|
+
if (uploadQueue.length) {
|
|
269
|
+
this.modalsManager.setOption('acceptButtonDisabled', false);
|
|
270
|
+
} else {
|
|
271
|
+
this.modalsManager.setOption('acceptButtonDisabled', true);
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
this.modalsManager.show('modals/import-form', {
|
|
276
|
+
title: `Import ${pluralize(modelName)} with spreadsheets`,
|
|
277
|
+
acceptButtonText: 'Start Import',
|
|
278
|
+
acceptButtonScheme: 'magic',
|
|
279
|
+
acceptButtonIcon: 'upload',
|
|
280
|
+
acceptButtonDisabled: true,
|
|
281
|
+
isProcessing: false,
|
|
282
|
+
uploadQueue: [],
|
|
283
|
+
fileQueueColumns: [
|
|
284
|
+
{ name: 'Type', valuePath: 'extension', key: 'type' },
|
|
285
|
+
{ name: 'File Name', valuePath: 'name', key: 'fileName' },
|
|
286
|
+
{ name: 'File Size', valuePath: 'size', key: 'fileSize' },
|
|
287
|
+
{ name: 'Upload Date', valuePath: 'file.lastModifiedDate', key: 'uploadDate' },
|
|
288
|
+
{ name: '', valuePath: '', key: 'delete' },
|
|
289
|
+
],
|
|
290
|
+
acceptedFileTypes: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv'],
|
|
291
|
+
queueFile: (file) => {
|
|
292
|
+
const uploadQueue = this.modalsManager.getOption('uploadQueue');
|
|
293
|
+
|
|
294
|
+
uploadQueue.pushObject(file);
|
|
295
|
+
checkQueue();
|
|
296
|
+
},
|
|
297
|
+
|
|
298
|
+
removeFile: (file) => {
|
|
299
|
+
const { queue } = file;
|
|
300
|
+
const uploadQueue = this.modalsManager.getOption('uploadQueue');
|
|
301
|
+
|
|
302
|
+
uploadQueue.removeObject(file);
|
|
303
|
+
queue.remove(file);
|
|
304
|
+
checkQueue();
|
|
305
|
+
},
|
|
306
|
+
confirm: async (modal) => {
|
|
307
|
+
const uploadQueue = this.modalsManager.getOption('uploadQueue');
|
|
308
|
+
const uploadedFiles = [];
|
|
309
|
+
const uploadTask = (file) => {
|
|
310
|
+
return new Promise((resolve) => {
|
|
311
|
+
this.fetch.uploadFile.perform(
|
|
312
|
+
file,
|
|
313
|
+
{
|
|
314
|
+
path: `uploads/import-sources/${this.currentUser.companyId}/${modelEndpoint}`,
|
|
315
|
+
type: 'import-source',
|
|
316
|
+
},
|
|
317
|
+
(uploadedFile) => {
|
|
318
|
+
uploadedFiles.pushObject(uploadedFile);
|
|
319
|
+
resolve(uploadedFile);
|
|
320
|
+
}
|
|
321
|
+
);
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
if (!uploadQueue.length) {
|
|
326
|
+
return this.notifications.warning('No spreadsheets uploaded for import to process.');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
modal.startLoading();
|
|
330
|
+
modal.setOption('acceptButtonText', 'Uploading...');
|
|
331
|
+
|
|
332
|
+
for (let i = 0; i < uploadQueue.length; i++) {
|
|
333
|
+
const file = uploadQueue.objectAt(i);
|
|
334
|
+
await uploadTask(file);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
this.modalsManager.setOption('acceptButtonText', 'Processing...');
|
|
338
|
+
this.modalsManager.setOption('isProcessing', true);
|
|
339
|
+
|
|
340
|
+
const files = uploadedFiles.map((file) => file.id);
|
|
341
|
+
|
|
342
|
+
try {
|
|
343
|
+
const response = await this.fetch.post(`${modelEndpoint}/import`, { files });
|
|
344
|
+
if (typeof options.onImportCompleted === 'function') {
|
|
345
|
+
options.onImportCompleted(response, files);
|
|
346
|
+
}
|
|
347
|
+
} catch (error) {
|
|
348
|
+
return this.notifications.serverError(error);
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
...options,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
244
354
|
}
|
package/package.json
CHANGED