Skip to content

Commit

Permalink
Merge pull request #45 from fleetbase/dev-v0.2.11
Browse files Browse the repository at this point in the history
v0.2.11
  • Loading branch information
roncodes authored May 30, 2024
2 parents ae1176f + ce982dc commit b17d74d
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
110 changes: 110 additions & 0 deletions addon/services/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export default class CrudService extends Service {
*/
@service store;

/**
* @service currentUser
*/
@service currentUser;

/**
* Generic deletion modal with options
*
Expand Down Expand Up @@ -241,4 +246,109 @@ export default class CrudService extends Service {
...options,
});
}

/**
* Prompts a spreadsheet upload for an import process.
*
* @param {String} modelName
* @param {Object} [options={}]
* @memberof CrudService
*/
@action import(modelName, options = {}) {
// always lowercase modelname
modelName = modelName.toLowerCase();

// set the model uri endpoint
const modelEndpoint = dasherize(pluralize(modelName));

// function to check if queue is empty
const checkQueue = () => {
const uploadQueue = this.modalsManager.getOption('uploadQueue');

if (uploadQueue.length) {
this.modalsManager.setOption('acceptButtonDisabled', false);
} else {
this.modalsManager.setOption('acceptButtonDisabled', true);
}
};

this.modalsManager.show('modals/import-form', {
title: `Import ${pluralize(modelName)} with spreadsheets`,
acceptButtonText: 'Start Import',
acceptButtonScheme: 'magic',
acceptButtonIcon: 'upload',
acceptButtonDisabled: true,
isProcessing: false,
uploadQueue: [],
fileQueueColumns: [
{ name: 'Type', valuePath: 'extension', key: 'type' },
{ name: 'File Name', valuePath: 'name', key: 'fileName' },
{ name: 'File Size', valuePath: 'size', key: 'fileSize' },
{ name: 'Upload Date', valuePath: 'file.lastModifiedDate', key: 'uploadDate' },
{ name: '', valuePath: '', key: 'delete' },
],
acceptedFileTypes: ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv'],
queueFile: (file) => {
const uploadQueue = this.modalsManager.getOption('uploadQueue');

uploadQueue.pushObject(file);
checkQueue();
},

removeFile: (file) => {
const { queue } = file;
const uploadQueue = this.modalsManager.getOption('uploadQueue');

uploadQueue.removeObject(file);
queue.remove(file);
checkQueue();
},
confirm: async (modal) => {
const uploadQueue = this.modalsManager.getOption('uploadQueue');
const uploadedFiles = [];
const uploadTask = (file) => {
return new Promise((resolve) => {
this.fetch.uploadFile.perform(
file,
{
path: `uploads/import-sources/${this.currentUser.companyId}/${modelEndpoint}`,
type: 'import-source',
},
(uploadedFile) => {
uploadedFiles.pushObject(uploadedFile);
resolve(uploadedFile);
}
);
});
};

if (!uploadQueue.length) {
return this.notifications.warning('No spreadsheets uploaded for import to process.');
}

modal.startLoading();
modal.setOption('acceptButtonText', 'Uploading...');

for (let i = 0; i < uploadQueue.length; i++) {
const file = uploadQueue.objectAt(i);
await uploadTask(file);
}

this.modalsManager.setOption('acceptButtonText', 'Processing...');
this.modalsManager.setOption('isProcessing', true);

const files = uploadedFiles.map((file) => file.id);

try {
const response = await this.fetch.post(`${modelEndpoint}/import`, { files });
if (typeof options.onImportCompleted === 'function') {
options.onImportCompleted(response, files);
}
} catch (error) {
return this.notifications.serverError(error);
}
},
...options,
});
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/ember-core",
"version": "0.2.10",
"version": "0.2.11",
"description": "Provides all the core services, decorators and utilities for building a Fleetbase extension for the Console.",
"keywords": [
"fleetbase-core",
Expand Down

0 comments on commit b17d74d

Please sign in to comment.