Skip to content

Commit

Permalink
Merge pull request #44 from fleetbase/feature/resource-imports
Browse files Browse the repository at this point in the history
resource imports
  • Loading branch information
roncodes authored May 22, 2024
2 parents ba25b6e + 891f16c commit c11b18b
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 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,103 @@ export default class CrudService extends Service {
...options,
});
}

@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);
console.log('options', options);
}
} catch (error) {
return this.notifications.serverError(error);
}
},
...options,
});
}
}

0 comments on commit c11b18b

Please sign in to comment.