From 7341a749457b5a7c65832737f5f90cafde893002 Mon Sep 17 00:00:00 2001 From: Doljinsuren Enkhbayar Date: Fri, 17 May 2024 17:53:59 +0800 Subject: [PATCH 1/3] implemented `import()` method --- addon/services/crud.js | 99 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/addon/services/crud.js b/addon/services/crud.js index 9f08f91..113f61a 100644 --- a/addon/services/crud.js +++ b/addon/services/crud.js @@ -237,4 +237,103 @@ export default class CrudService extends Service { ...options, }); } + + @action import(modelName, option = {}) { + // 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, done) => { + 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 }); + } catch (error) { + return this.notifications.serverError(error); + } + + if (typeof options.onImportCompleted === 'function') { + options.onImportCompleted(response); + } + }, + ...options, + }); + } } From 482fa95f03b102bc312d370d7969dc058f9e2e16 Mon Sep 17 00:00:00 2001 From: Doljinsuren Enkhbayar Date: Fri, 17 May 2024 18:13:10 +0800 Subject: [PATCH 2/3] fixed miss spelling --- addon/services/crud.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addon/services/crud.js b/addon/services/crud.js index 113f61a..81d6c72 100644 --- a/addon/services/crud.js +++ b/addon/services/crud.js @@ -238,7 +238,7 @@ export default class CrudService extends Service { }); } - @action import(modelName, option = {}) { + @action import(modelName, options = {}) { // always lowercase modelname modelName = modelName.toLowerCase(); @@ -286,7 +286,7 @@ export default class CrudService extends Service { queue.remove(file); checkQueue(); }, - confirm: async (modal, done) => { + confirm: async (modal) => { const uploadQueue = this.modalsManager.getOption('uploadQueue'); const uploadedFiles = []; const uploadTask = (file) => { From 891f16c482b17be368cf6b8b78e4a6eb401e6a7c Mon Sep 17 00:00:00 2001 From: Doljinsuren Enkhbayar Date: Mon, 20 May 2024 18:47:30 +0800 Subject: [PATCH 3/3] added current user --- addon/services/crud.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/addon/services/crud.js b/addon/services/crud.js index 81d6c72..e259c2e 100644 --- a/addon/services/crud.js +++ b/addon/services/crud.js @@ -40,6 +40,11 @@ export default class CrudService extends Service { */ @service store; + /** + * @service currentUser + */ + @service currentUser; + /** * Generic deletion modal with options * @@ -278,6 +283,7 @@ export default class CrudService extends Service { uploadQueue.pushObject(file); checkQueue(); }, + removeFile: (file) => { const { queue } = file; const uploadQueue = this.modalsManager.getOption('uploadQueue'); @@ -299,7 +305,6 @@ export default class CrudService extends Service { }, (uploadedFile) => { uploadedFiles.pushObject(uploadedFile); - resolve(uploadedFile); } ); @@ -325,13 +330,13 @@ export default class CrudService extends Service { 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); } - - if (typeof options.onImportCompleted === 'function') { - options.onImportCompleted(response); - } }, ...options, });