Preserve mapping state between inventory import steps [SCI-10848]

This commit is contained in:
Martin Artnik 2024-07-08 12:54:09 +02:00
parent ae1034e994
commit 2aa6466de8
3 changed files with 50 additions and 41 deletions

View file

@ -10,7 +10,7 @@
@generatePreview="generatePreview" @generatePreview="generatePreview"
@changeStep="changeStep" @changeStep="changeStep"
@importRows="importRecords" @importRows="importRecords"
@updateAutoMapping="updateAutoMapping"
/> />
<ExportModal <ExportModal
v-else v-else
@ -49,7 +49,7 @@ export default {
return { return {
modalOpened: false, modalOpened: false,
activeStep: 'UploadStep', activeStep: 'UploadStep',
params: {}, params: { autoMapping: true },
modalId: null, modalId: null,
loading: false loading: false
}; };
@ -65,18 +65,20 @@ export default {
fetchRepository() { fetchRepository() {
axios.get(this.repositoryUrl) axios.get(this.repositoryUrl)
.then((response) => { .then((response) => {
this.params = response.data.data; this.params = { ...this.params, ...response.data.data };
this.modalId = Math.random().toString(36); this.modalId = Math.random().toString(36);
this.modalOpened = true; this.modalOpened = true;
}); });
}, },
uploadFile(params) { uploadFile(params) {
this.params = params; this.params = { ...this.params, ...params };
this.activeStep = 'MappingStep'; this.activeStep = 'MappingStep';
}, },
updateAutoMapping(value) {
generatePreview(mappings, updateWithEmptyCells, onlyAddNewItems) { this.params.autoMapping = value;
this.params.mapping = mappings; },
generatePreview(selectedItems, updateWithEmptyCells, onlyAddNewItems) {
this.params.selectedItems = selectedItems;
this.params.updateWithEmptyCells = updateWithEmptyCells; this.params.updateWithEmptyCells = updateWithEmptyCells;
this.params.onlyAddNewItems = onlyAddNewItems; this.params.onlyAddNewItems = onlyAddNewItems;
this.importRecords(true); this.importRecords(true);
@ -84,6 +86,12 @@ export default {
changeStep(step) { changeStep(step) {
this.activeStep = step; this.activeStep = step;
}, },
generateMapping() {
return this.params.selectedItems.reduce((obj, item) => {
obj[item.index] = item.key || '';
return obj;
}, {});
},
importRecords(preview) { importRecords(preview) {
if (this.loading) { if (this.loading) {
return; return;
@ -91,7 +99,7 @@ export default {
const jsonData = { const jsonData = {
file_id: this.params.temp_file.id, file_id: this.params.temp_file.id,
mappings: this.params.mapping, mappings: this.generateMapping(),
id: this.params.id, id: this.params.id,
preview: preview, preview: preview,
should_overwrite_with_empty_cells: this.params.updateWithEmptyCells, should_overwrite_with_empty_cells: this.params.updateWithEmptyCells,

View file

@ -19,7 +19,7 @@
<div class="flex gap-6 items-center my-6"> <div class="flex gap-6 items-center my-6">
<div class="flex items-center gap-2" :title="i18n.t('repositories.import_records.steps.step2.autoMappingTooltip')"> <div class="flex items-center gap-2" :title="i18n.t('repositories.import_records.steps.step2.autoMappingTooltip')">
<div class="sci-checkbox-container"> <div class="sci-checkbox-container">
<input type="checkbox" class="sci-checkbox" v-model="autoMapping" /> <input type="checkbox" class="sci-checkbox" @change="$emit('update-auto-mapping', $event.target.checked)" :checked="params.autoMapping" />
<span class="sci-checkbox-label"></span> <span class="sci-checkbox-label"></span>
</div> </div>
{{ i18n.t('repositories.import_records.steps.step2.autoMappingText') }} {{ i18n.t('repositories.import_records.steps.step2.autoMappingText') }}
@ -56,7 +56,7 @@
:params="params" :params="params"
:value="this.selectedItems.find((item) => item.index === index)" :value="this.selectedItems.find((item) => item.index === index)"
@selection:changed="handleChange" @selection:changed="handleChange"
:autoMapping="this.autoMapping" :autoMapping="params.autoMapping"
/> />
</template> </template>
</div> </div>
@ -98,7 +98,7 @@ import Loading from '../../../shared/loading.vue';
export default { export default {
name: 'MappingStep', name: 'MappingStep',
emits: ['close', 'generatePreview'], emits: ['close', 'generatePreview', 'updateAutoMapping'],
mixins: [modalMixin], mixins: [modalMixin],
components: { components: {
SelectDropdown, SelectDropdown,
@ -117,7 +117,6 @@ export default {
}, },
data() { data() {
return { return {
autoMapping: false,
updateWithEmptyCells: false, updateWithEmptyCells: false,
onlyAddNewItems: false, onlyAddNewItems: false,
columnLabels: { columnLabels: {
@ -154,11 +153,32 @@ export default {
item.key = key; item.key = key;
item.value = value; item.value = value;
}, },
generateMapping() { loadAvailableFields() {
return this.selectedItems.reduce((obj, item) => { // Adding alreadySelected attribute for tracking
obj[item.index] = item.key || ''; this.availableFields = [];
return obj;
}, {}); Object.entries(this.params.import_data.available_fields).forEach(([key, value]) => {
let columnTypeName = '';
if (key === '-1') {
columnTypeName = this.i18n.t('repositories.import_records.steps.step2.computedDropdownOptions.name');
} else if (key === '0') {
columnTypeName = this.i18n.t('repositories.import_records.steps.step2.computedDropdownOptions.id');
} else {
const column = this.repositoryColumns.find((el) => el[0] === parseInt(key, 10));
columnTypeName = this.i18n.t(`repositories.import_records.steps.step2.computedDropdownOptions.${column[2]}`);
}
const field = {
key, value, alreadySelected: false, typeName: columnTypeName
};
this.availableFields.push(field);
});
},
loadSelectedItems() {
if (this.params.selectedItems) {
this.selectedItems = this.params.selectedItems;
} else {
this.selectedItems = this.params.import_data.header.map((item, index) => ({ index, key: null, value: null }));
}
}, },
importRecords() { importRecords() {
if (!this.selectedItems.find((item) => item.key === '-1')) { if (!this.selectedItems.find((item) => item.key === '-1')) {
@ -168,7 +188,7 @@ export default {
this.$emit( this.$emit(
'generatePreview', 'generatePreview',
this.generateMapping() this.selectedItems
); );
return true; return true;
} }
@ -187,29 +207,8 @@ export default {
}, },
created() { created() {
this.repositoryColumns = this.params.attributes.repository_columns; this.repositoryColumns = this.params.attributes.repository_columns;
// Adding alreadySelected attribute for tracking this.loadAvailableFields();
this.availableFields = []; this.loadSelectedItems();
this.selectedItems = this.params.import_data.header.map((item, index) => { return { index, key: null, value: null }; });
Object.entries(this.params.import_data.available_fields).forEach(([key, value]) => {
let columnTypeName = '';
if (key === '-1') {
columnTypeName = this.i18n.t('repositories.import_records.steps.step2.computedDropdownOptions.name');
} else if (key === '0') {
columnTypeName = this.i18n.t('repositories.import_records.steps.step2.computedDropdownOptions.id');
} else {
const column = this.repositoryColumns.find((el) => el[0] === parseInt(key, 10));
columnTypeName = this.i18n.t(`repositories.import_records.steps.step2.computedDropdownOptions.${column[2]}`);
}
const field = {
key, value, alreadySelected: false, typeName: columnTypeName
};
this.availableFields.push(field);
});
},
mounted() {
this.autoMapping = true;
} }
}; };
</script> </script>

View file

@ -171,6 +171,8 @@ export default {
mounted() { mounted() {
if (this.autoMapping) { if (this.autoMapping) {
this.autoMap(); this.autoMap();
} else {
this.selectedColumnType = this.value;
} }
} }
}; };