Skip to content

Commit

Permalink
feat: allow import multiple workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kholid060 committed Feb 24, 2022
1 parent 9632a84 commit 738c1f0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/components/newtab/workflow/edit/EditExportData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
class="mt-2"
@change="updateData({ addBOMHeader: $event })"
>
Add UTF-8 BOM
{{ t('workflow.blocks.export-data.bomHeader') }}
</ui-checkbox>
</div>
</template>
Expand Down
2 changes: 1 addition & 1 deletion src/components/newtab/workflow/edit/EditLoopData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function updateLoopID(id) {
}
function importFile() {
openFilePicker(['application/json', 'text/csv', 'application/vnd.ms-excel'])
.then(async (fileObj) => {
.then(async ([fileObj]) => {
if (fileObj.size > maxFileSize) {
toast.error(t('message.maxSizeExceeded'));
return;
Expand Down
1 change: 1 addition & 0 deletions src/locales/en/blocks.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
"description": "Export workflow data",
"exportAs": "Export as",
"refKey": "Reference key",
"bomHeader": "Add UTF-8 BOM",
"dataToExport": {
"placeholder": "Data to export",
"options": {
Expand Down
2 changes: 1 addition & 1 deletion src/newtab/pages/Workflows.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<v-remixicon name="riCompass3Line" class="mr-2 -ml-1" />
{{ t('workflow.browse') }}
</ui-button>
<ui-button @click="importWorkflow">
<ui-button @click="importWorkflow({ multiple: true })">
<v-remixicon name="riUploadLine" class="mr-2 -ml-1" />
{{ t('workflow.import') }}
</ui-button>
Expand Down
21 changes: 4 additions & 17 deletions src/newtab/pages/settings/Backup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -215,29 +215,16 @@ function backupWorkflows() {
}
async function restoreWorkflows() {
try {
const file = await openFilePicker('application/json');
const [file] = await openFilePicker('application/json');
const reader = new FileReader();
const insertWorkflows = (workflows) => {
workflows.forEach((workflow) => {
const isWorkflowExists = Workflow.query()
.where('id', workflow.id)
.exists();
if (!state.updateIfExists || !isWorkflowExists) {
const newWorkflows = workflows.map((workflow) => {
if (!state.updateIfExists) {
workflow.createdAt = Date.now();
delete workflow.id;
Workflow.insert({
data: workflow,
});
return;
}
Workflow.update({
where: workflow.id,
data: workflow,
});
return workflow;
});
const showMessage = (event) => {
toast(
Expand Down
16 changes: 9 additions & 7 deletions src/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function replaceMustache(str, replacer) {
}

export function openFilePicker(acceptedFileTypes = [], attrs = {}) {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
const input = document.createElement('input');
input.type = 'file';
input.accept = Array.isArray(acceptedFileTypes)
Expand All @@ -110,14 +110,16 @@ export function openFilePicker(acceptedFileTypes = [], attrs = {}) {
});

input.onchange = (event) => {
const file = event.target.files[0];
const { files } = event.target;
const validFiles = [];

if (!file || !acceptedFileTypes.includes(file.type)) {
reject(new Error(`Invalid ${file.type} file type`));
return;
}
files.forEach((file) => {
if (!acceptedFileTypes.includes(file.type)) return;

validFiles.push(file);
});

resolve(file);
resolve(validFiles);
};

input.click();
Expand Down
17 changes: 10 additions & 7 deletions src/utils/workflow-data.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { parseJSON, fileSaver, openFilePicker, isObject } from './helper';
import Workflow from '@/models/workflow';

export function importWorkflow() {
openFilePicker(['application/json'])
.then((file) => {
const reader = new FileReader();
export function importWorkflow(attrs = {}) {
openFilePicker(['application/json'], attrs)
.then((files) => {
const getDrawflow = ({ drawflow }) => {
if (isObject(drawflow)) return JSON.stringify(drawflow);

return drawflow;
};

reader.onload = ({ target }) => {
const handleOnLoadReader = ({ target }) => {
const workflow = JSON.parse(target.result);

if (workflow.includedWorkflows) {
Expand Down Expand Up @@ -52,7 +50,12 @@ export function importWorkflow() {
});
};

reader.readAsText(file);
files.forEach((file) => {
const reader = new FileReader();

reader.onload = handleOnLoadReader;
reader.readAsText(file);
});
})
.catch((error) => {
console.error(error);
Expand Down

0 comments on commit 738c1f0

Please sign in to comment.