Commit f8d8ea87 authored by VLaroye's avatar VLaroye

bugfix(view/duplication): Added helper for duplication functions + Adapted components

parent a880c5e8
Pipeline #4458 passed with stage
in 1 minute and 58 seconds
......@@ -73,8 +73,8 @@ import find from 'lodash/find';
import omit from 'lodash/omit';
import { MODALS, USERS_RIGHTS_TYPES, USERS_RIGHTS_MASKS } from '@/constants';
import { generateView, generateViewTab, generateViewRow, generateRight, generateRoleRightByChecksum } from '@/helpers/entities';
import uuid from '@/helpers/uid';
import { generateView, generateRight, generateRoleRightByChecksum } from '@/helpers/entities';
import copyViewTabs from '@/helpers/duplication';
import authMixin from '@/mixins/auth';
import popupMixin from '@/mixins/popup';
import modalInnerMixin from '@/mixins/modal/inner';
......@@ -82,6 +82,7 @@ import entitiesViewMixin from '@/mixins/entities/view';
import entitiesRoleMixin from '@/mixins/entities/role';
import entitiesRightMixin from '@/mixins/entities/right';
import entitiesViewGroupMixin from '@/mixins/entities/view/group';
import entitiesUserPreferenceMixin from '@/mixins/entities/user-preference';
import rightsTechnicalViewMixin from '@/mixins/rights/technical/view';
import vuetifyComboboxMixin from '@/mixins/vuetify/combobox';
......@@ -101,6 +102,7 @@ export default {
entitiesRoleMixin,
entitiesRightMixin,
entitiesViewGroupMixin,
entitiesUserPreferenceMixin,
rightsTechnicalViewMixin,
vuetifyComboboxMixin,
],
......@@ -269,15 +271,10 @@ export default {
};
if (this.config.isDuplicating) {
data.tabs = this.config.view.tabs.map(tab => ({
...generateViewTab(),
title: tab.title,
rows: tab.rows.map(row => ({
...generateViewRow(),
title: row.title,
widgets: row.widgets.map(widget => ({ ...widget, _id: uuid(`widget_${widget.type}`) })),
})),
}));
const { tabs, widgetsIdsMap } = copyViewTabs(this.config.view.tabs);
data.tabs = tabs;
await this.copyUserPreferencesForWidgets(widgetsIdsMap);
}
const response = await this.createView({ data });
......
......@@ -53,25 +53,17 @@
</template>
<script>
import omit from 'lodash/omit';
import Draggable from 'vuedraggable';
import { createNamespacedHelpers } from 'vuex';
import { VUETIFY_ANIMATION_DELAY } from '@/config';
import { MODALS } from '@/constants';
import {
generateViewTab,
generateViewRow,
generateWidgetByType,
generateUserPreferenceByWidgetAndUser,
} from '@/helpers/entities';
import { copyTab } from '@/helpers/duplication';
import authMixin from '@/mixins/auth';
import modalMixin from '@/mixins/modal';
import vuetifyTabsMixin from '@/mixins/vuetify/tabs';
const { mapActions: userPreferenceMapActions } = createNamespacedHelpers('userPreference');
import entitiesUserPreferenceMixin from '@/mixins/entities/user-preference';
export default {
components: { Draggable },
......@@ -79,6 +71,7 @@ export default {
authMixin,
modalMixin,
vuetifyTabsMixin,
entitiesUserPreferenceMixin,
],
props: {
view: {
......@@ -133,11 +126,6 @@ export default {
},
},
methods: {
...userPreferenceMapActions({
createUserPreference: 'create',
fetchUserPreferenceByWidgetIdWithoutStore: 'fetchItemByWidgetIdWithoutStore',
}),
showUpdateTabModal(tab) {
this.showModal({
name: MODALS.textFieldEditor,
......@@ -183,56 +171,13 @@ export default {
},
async duplicateTabAction(tab, title) {
const widgetsIdsMap = {};
const newTab = {
...generateViewTab(),
title,
rows: tab.rows.map(row => ({
...generateViewRow(),
title: row.title,
widgets: row.widgets.map((widget) => {
const newWidget = generateWidgetByType(widget.type);
widgetsIdsMap[widget._id] = newWidget._id;
return {
...newWidget,
...omit(widget, ['_id']),
};
}),
})),
};
const { tab: newTab, widgetsIdsMap } = copyTab(tab, title);
await this.copyUserPreferencesForWidgets(widgetsIdsMap);
return this.addTab(newTab);
},
async copyUserPreferencesForWidgets(widgetsIdsMap) {
const oldWidgetsIds = Object.keys(widgetsIdsMap);
const userPreferences = await Promise.all(oldWidgetsIds.map(widgetId =>
this.fetchUserPreferenceByWidgetIdWithoutStore({ widgetId })));
return Promise.all(userPreferences.map((userPreference) => {
if (!userPreference) {
return Promise.resolve();
}
const newWidgetId = widgetsIdsMap[userPreference.widget_id];
const newUserPreference = generateUserPreferenceByWidgetAndUser({ _id: newWidgetId }, this.currentUser);
return this.createUserPreference({
userPreference: {
...newUserPreference,
...omit(userPreference, ['_id', 'widget_id']),
},
});
}));
},
updateTab(tab) {
const view = {
...this.view,
......
import { omit } from 'lodash';
import {
generateViewTab,
generateViewRow,
generateWidgetByType,
} from '@/helpers/entities';
export default function copyViewTabs(tabs) {
const widgetsIdsMap = {};
const newTabs = tabs.map(tab => ({
...generateViewTab(),
title: tab.title,
rows: tab.rows.map(row => ({
...generateViewRow(),
title: row.title,
widgets: row.widgets.map((widget) => {
const newWidget = generateWidgetByType(widget.type);
widgetsIdsMap[widget._id] = newWidget._id; // Needed for userPreferences copy.
return {
...newWidget,
...omit(widget, ['_id']),
};
}),
})),
}));
return { tabs: newTabs, widgetsIdsMap };
}
export function copyTab(tab, title) {
const widgetsIdsMap = {};
const newTab = {
...generateViewTab(),
title,
rows: tab.rows.map(row => ({
...generateViewRow(),
title: row.title,
widgets: row.widgets.map((widget) => {
const newWidget = generateWidgetByType(widget.type);
widgetsIdsMap[widget._id] = newWidget._id; // Needed for userPreferences copy.
return {
...newWidget,
...omit(widget, ['_id']),
};
}),
})),
};
return { tab: newTab, widgetsIdsMap };
}
import omit from 'lodash/omit';
import { createNamespacedHelpers } from 'vuex';
import { generateUserPreferenceByWidgetAndUser } from '@/helpers/entities';
const { mapActions, mapGetters } = createNamespacedHelpers('userPreference');
/**
......@@ -19,6 +22,7 @@ export default {
...mapActions({
fetchUserPreferencesList: 'fetchList',
fetchUserPreferenceByWidgetId: 'fetchItemByWidgetId',
fetchUserPreferenceByWidgetIdWithoutStore: 'fetchItemByWidgetIdWithoutStore',
createUserPreference: 'create',
}),
......@@ -30,5 +34,31 @@ export default {
},
});
},
async copyUserPreferencesForWidgets(widgetsIdsMap) {
const oldWidgetsIds = Object.keys(widgetsIdsMap);
const userPreferences = await Promise.all(oldWidgetsIds.map(widgetId => (
this.fetchUserPreferenceByWidgetIdWithoutStore({ widgetId })
)));
return Promise.all(userPreferences.map((userPreference) => {
if (!userPreference) {
return Promise.resolve();
}
const newWidgetId = widgetsIdsMap[userPreference.widget_id];
const newUserPreference = generateUserPreferenceByWidgetAndUser({
_id: newWidgetId,
}, this.currentUser);
return this.createUserPreference({
userPreference: {
...newUserPreference,
...omit(userPreference, ['_id', 'widget_id']),
},
});
}));
},
},
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment