mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-23 08:58:26 +00:00
refactor auto save
This commit is contained in:
parent
c1a42e7012
commit
49681f05bd
2 changed files with 84 additions and 38 deletions
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue