Commit 550f6b19 authored by Vladimir Erokhov's avatar Vladimir Erokhov

bugfix(view-duplication): Finished task

parent f8d8ea87
Pipeline #4483 passed with stage
in 2 minutes and 3 seconds
......@@ -73,8 +73,14 @@ import find from 'lodash/find';
import omit from 'lodash/omit';
import { MODALS, USERS_RIGHTS_TYPES, USERS_RIGHTS_MASKS } from '@/constants';
import { generateView, generateRight, generateRoleRightByChecksum } from '@/helpers/entities';
import copyViewTabs from '@/helpers/duplication';
import {
generateView,
generateRight,
generateRoleRightByChecksum,
generateCopyOfView,
getViewsWidgetsIdsMappings,
} from '@/helpers/entities';
import authMixin from '@/mixins/auth';
import popupMixin from '@/mixins/popup';
import modalInnerMixin from '@/mixins/modal/inner';
......@@ -271,10 +277,13 @@ export default {
};
if (this.config.isDuplicating) {
const { tabs, widgetsIdsMap } = copyViewTabs(this.config.view.tabs);
const { tabs } = generateCopyOfView(this.config.view);
data.tabs = tabs;
await this.copyUserPreferencesForWidgets(widgetsIdsMap);
const widgetsIdsMappings = getViewsWidgetsIdsMappings(this.config.view, data);
await this.copyUserPreferencesByWidgetsIdsMappings(widgetsIdsMappings);
}
const response = await this.createView({ data });
......
......@@ -58,7 +58,7 @@ import Draggable from 'vuedraggable';
import { VUETIFY_ANIMATION_DELAY } from '@/config';
import { MODALS } from '@/constants';
import { copyTab } from '@/helpers/duplication';
import { generateCopyOfViewTab, getViewsTabsWidgetsIdsMappings } from '@/helpers/entities';
import authMixin from '@/mixins/auth';
import modalMixin from '@/mixins/modal';
......@@ -171,9 +171,15 @@ export default {
},
async duplicateTabAction(tab, title) {
const { tab: newTab, widgetsIdsMap } = copyTab(tab, title);
const newTab = {
...generateCopyOfViewTab(tab),
await this.copyUserPreferencesForWidgets(widgetsIdsMap);
title,
};
const widgetsIdsMappings = getViewsTabsWidgetsIdsMappings(tab, newTab);
await this.copyUserPreferencesByWidgetsIdsMappings(widgetsIdsMappings);
return this.addTab(newTab);
},
......
import moment from 'moment';
import { get, omit } from 'lodash';
import i18n from '@/i18n';
import { PAGINATION_LIMIT } from '@/config';
......@@ -6,6 +7,12 @@ import { WIDGET_TYPES, STATS_CALENDAR_COLORS, STATS_DURATION_UNITS, SERVICE_WEAT
import uuid from './uuid';
/**
* Generate widget by type
*
* @param {string} type
* @returns {Object}
*/
export function generateWidgetByType(type) {
const widget = {
type,
......@@ -198,6 +205,11 @@ export function generateWidgetByType(type) {
return widget;
}
/**
* Generate view row
*
* @returns {Object}
*/
export function generateViewRow() {
return {
_id: uuid('view-row'),
......@@ -206,6 +218,11 @@ export function generateViewRow() {
};
}
/**
* Generate view tab
*
* @returns {Object}
*/
export function generateViewTab() {
return {
_id: uuid('view-tab'),
......@@ -214,6 +231,11 @@ export function generateViewTab() {
};
}
/**
* Generate view
*
* @returns {Object}
*/
export function generateView() {
const defaultTab = { ...generateViewTab(), title: 'Default' };
......@@ -228,6 +250,13 @@ export function generateView() {
};
}
/**
* Generate user preference by widget and user objects
*
* @param {Object} widget
* @param {Object} user
* @returns {Object}
*/
export function generateUserPreferenceByWidgetAndUser(widget, user) {
return {
_id: `${widget._id}_${user.crecord_name}`,
......@@ -239,6 +268,11 @@ export function generateUserPreferenceByWidgetAndUser(widget, user) {
};
}
/**
* Generate user
*
* @returns {Object}
*/
export function generateUser() {
return {
crecord_write_time: null,
......@@ -264,6 +298,11 @@ export function generateUser() {
};
}
/**
* Generate role
*
* @returns {Object}
*/
export function generateRole() {
return {
crecord_write_time: null,
......@@ -276,6 +315,11 @@ export function generateRole() {
};
}
/**
* Generate right
*
* @returns {Object}
*/
export function generateRight() {
return {
crecord_creation_time: null,
......@@ -290,6 +334,12 @@ export function generateRight() {
};
}
/**
* Generate role right by checksum
*
* @param {number} checksum
* @returns {Object}
*/
export function generateRoleRightByChecksum(checksum) {
return {
checksum,
......@@ -297,6 +347,78 @@ export function generateRoleRightByChecksum(checksum) {
};
}
/**
* Generate copy of view tab
*
* @param {Object} tab
* @returns {Object}
*/
export function generateCopyOfViewTab(tab) {
return {
...generateViewTab(),
rows: tab.rows.map(row => ({
...generateViewRow(),
title: row.title,
widgets: row.widgets.map(widget => ({
...generateWidgetByType(widget.type),
...omit(widget, ['_id']),
})),
})),
};
}
/**
* Generate copy of view
*
* @param {Object} view
* @returns {Object}
*/
export function generateCopyOfView(view) {
return {
...generateView(),
...omit(view, ['_id', 'tabs']),
tabs: view.tabs.map(tab => ({
...generateCopyOfViewTab(tab),
...omit(tab, ['_id', 'rows']),
})),
};
}
/**
* Get mappings for widgets ids from old tab to new tab
*
* @param {Object} oldTab
* @param {Object} newTab
* @returns {Array.<{ oldId: number, newId: number }>}
*/
export function getViewsTabsWidgetsIdsMappings(oldTab, newTab) {
return oldTab.rows.reduce((acc, row, rowIndex) => {
const widgetsIds = row.widgets.map((widget, widgetIndex) => ({
oldId: widget._id,
newId: get(newTab, `rows.${rowIndex}.widgets.${widgetIndex}._id`, null),
}));
return acc.concat(widgetsIds);
}, []);
}
/**
* Get mappings for widgets from old view to new view
*
* @param {Object} oldView
* @param {Object} newView
* @returns {Array.<{ oldId: number, newId: number }>}
*/
export function getViewsWidgetsIdsMappings(oldView, newView) {
return oldView.tabs.reduce((acc, tab, index) =>
acc.concat(getViewsTabsWidgetsIdsMappings(tab, newView.tabs[index])), []);
}
export default {
generateWidgetByType,
generateViewRow,
......@@ -306,4 +428,9 @@ export default {
generateRole,
generateRight,
generateRoleRightByChecksum,
generateCopyOfViewTab,
generateCopyOfView,
getViewsTabsWidgetsIdsMappings,
getViewsWidgetsIdsMappings,
};
......@@ -35,21 +35,22 @@ export default {
});
},
async copyUserPreferencesForWidgets(widgetsIdsMap) {
const oldWidgetsIds = Object.keys(widgetsIdsMap);
/**
* Send requests to create userPreference by widgetsIdsMappings
*
* @param {Array.<{oldId: string, newId: string}>} widgetsIdsMappings
* @returns {Promise.<*[]>}
*/
copyUserPreferencesByWidgetsIdsMappings(widgetsIdsMappings) {
return Promise.all(widgetsIdsMappings.map(async ({ oldId, newId }) => {
const userPreference = await this.fetchUserPreferenceByWidgetIdWithoutStore({ widgetId: oldId });
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,
_id: newId,
}, this.currentUser);
return this.createUserPreference({
......
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