From 49681f05bdb03f2d52b98c348ae863b4fd47d076 Mon Sep 17 00:00:00 2001 From: arpitnath Date: Tue, 15 Aug 2023 04:09:57 +0530 Subject: [PATCH] refactor auto save --- frontend/src/_services/appVersion.service.js | 41 +++++----- frontend/src/_stores/utils.js | 81 +++++++++++++++----- 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/frontend/src/_services/appVersion.service.js b/frontend/src/_services/appVersion.service.js index 248a1e6729..f96833bf05 100644 --- a/frontend/src/_services/appVersion.service.js +++ b/frontend/src/_services/appVersion.service.js @@ -45,7 +45,6 @@ function del(appId, versionId) { } function save(appId, versionId, values, isUserSwitchedVersion = false) { - // console.log('---piku [version saved]', { values }); const body = { is_user_switched_version: isUserSwitchedVersion }; if (values.definition) body['definition'] = values.definition; if (values.name) body['name'] = values.name; @@ -59,26 +58,31 @@ function save(appId, versionId, values, isUserSwitchedVersion = false) { }; return fetch(`${config.apiUrl}/apps/${appId}/versions/${versionId}`, requestOptions).then(handleResponse); } + function autoSaveApp(appId, versionId, diff, type, pageId, operation, isUserSwitchedVersion = false) { - const OPERATION = Object.freeze({ + const OPERATION = { create: 'POST', update: 'PUT', delete: 'DELETE', - }); + }; - let body = {}; + const bodyMappings = { + pages: { + create: { ...diff }, + delete: { ...diff }, + }, + global_settings: { + update: { ...diff }, + }, + }; - if (type === 'pages' && (operation === 'create' || operation === 'delete')) { - body = { - ...diff, - }; - } else if (!type || type === 'global_settings') { - body = { - ...diff, - }; - } else { - body = { is_user_switched_version: isUserSwitchedVersion, pageId, diff: diff }; - } + const body = !type + ? { ...diff } + : bodyMappings[type]?.[operation] || { + is_user_switched_version: isUserSwitchedVersion, + pageId, + diff, + }; const requestOptions = { method: OPERATION[operation], @@ -86,7 +90,8 @@ function autoSaveApp(appId, versionId, diff, type, pageId, operation, isUserSwit credentials: 'include', body: JSON.stringify(body), }; - return fetch(`${config.apiUrl}/v2/apps/${appId}/versions/${versionId}/${type ?? ''}`, requestOptions).then( - handleResponse - ); + + const url = `${config.apiUrl}/v2/apps/${appId}/versions/${versionId}/${type ?? ''}`; + + return fetch(url, requestOptions).then(handleResponse); } diff --git a/frontend/src/_stores/utils.js b/frontend/src/_stores/utils.js index f15a6387df..a0769f6f5c 100644 --- a/frontend/src/_stores/utils.js +++ b/frontend/src/_stores/utils.js @@ -49,31 +49,72 @@ export const computeAppDiff = (appDiff, currentPageId, opts) => { return { updateDiff, type, operation }; }; +// const updateFor = (appDiff, currentPageId, opts) => { +// const componentUpdates = ['componentAdded', 'componentDefinitionChanged', 'componentDeleted', 'containerChanges']; +// const pageUpdates = ['pageDefinitionChanged', 'pageSortingChanged', 'deletePageRequest', 'addNewPage']; +// const appUpdates = ['homePageChanged']; +// const globalSettings = ['globalSettings']; + +// const options = _.keys(opts); + +// if (_.intersection(options, componentUpdates).length > 0) { +// return computeComponentDiff(appDiff, currentPageId, opts); +// } else if (_.intersection(options, pageUpdates).length > 0) { +// return computePageUpdate(appDiff, currentPageId, opts); +// } else if (_.intersection(options, appUpdates).length > 0) { +// return { +// updateDiff: appDiff, +// type: null, +// operation: 'update', +// }; +// } else if (_.intersection(options, globalSettings).length > 0) { +// return { +// updateDiff: appDiff, +// type: 'global_settings', +// operation: 'update', +// }; +// } +// }; + const updateFor = (appDiff, currentPageId, opts) => { - const componentUpdates = ['componentAdded', 'componentDefinitionChanged', 'componentDeleted', 'containerChanges']; - const pageUpdates = ['pageDefinitionChanged', 'pageSortingChanged', 'deletePageRequest', 'addNewPage']; - const appUpdates = ['homePageChanged']; - const globalSettings = ['globalSettings']; + const updateTypeMappings = [ + { + updateTypes: ['componentAdded', 'componentDefinitionChanged', 'componentDeleted', 'containerChanges'], + processingFunction: computeComponentDiff, + }, + { + updateTypes: ['pageDefinitionChanged', 'pageSortingChanged', 'deletePageRequest', 'addNewPage'], + processingFunction: computePageUpdate, + }, + { + updateTypes: ['homePageChanged'], + processingFunction: () => ({ + updateDiff: appDiff, + type: null, + operation: 'update', + }), + }, + { + updateTypes: ['globalSettings'], + processingFunction: () => ({ + updateDiff: appDiff, + type: 'global_settings', + operation: 'update', + }), + }, + ]; const options = _.keys(opts); - if (_.intersection(options, componentUpdates).length > 0) { - return computeComponentDiff(appDiff, currentPageId, opts); - } else if (_.intersection(options, pageUpdates).length > 0) { - return computePageUpdate(appDiff, currentPageId, opts); - } else if (_.intersection(options, appUpdates).length > 0) { - return { - updateDiff: appDiff, - type: null, - operation: 'update', - }; - } else if (_.intersection(options, globalSettings).length > 0) { - return { - updateDiff: appDiff, - type: 'global_settings', - operation: 'update', - }; + for (const { updateTypes, processingFunction } of updateTypeMappings) { + if (_.intersection(options, updateTypes).length > 0) { + return processingFunction(appDiff, currentPageId, opts); + } } + + // Handle case when no matching update type is found + + return null; }; const computePageUpdate = (appDiff, currentPageId, opts) => {