From c64a88ae84589498426d5823fc7f3df25b0318f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1na=20Kohanov=C3=A1?= Date: Wed, 25 Nov 2020 19:38:25 +0100 Subject: [PATCH] #455 Store and retrieve snapshot actions in/from the backend --- .../preview/redux/dispatchActions.js | 9 ---- .../projects/projectPreview/index.js | 37 +++++++------ .../projects/redux/dispatchActions.js | 8 +-- .../snapshot/redux/dispatchActions.js | 17 ++---- js/components/target/redux/dispatchActions.js | 6 ++- js/components/target/withUpdatingTarget.js | 2 +- js/reducers/tracking/dispatchActions.js | 54 +++++++++++-------- 7 files changed, 65 insertions(+), 68 deletions(-) diff --git a/js/components/preview/redux/dispatchActions.js b/js/components/preview/redux/dispatchActions.js index 9c91e6191..ebd038920 100644 --- a/js/components/preview/redux/dispatchActions.js +++ b/js/components/preview/redux/dispatchActions.js @@ -78,15 +78,6 @@ export const shouldLoadProtein = ({ }); } - // decide to load existing snapshot - else if ( - currentSnapshotID !== null && - (!routeSnapshotID || routeSnapshotID === currentSnapshotID.toString()) && - currentSnapshotData !== null - ) { - //dispatch(reloadSession(currentSnapshotData, nglViewList)); - } - if (targetOnName !== undefined) { document.title = targetOnName + ': Fragalysis'; } diff --git a/js/components/projects/projectPreview/index.js b/js/components/projects/projectPreview/index.js index ea7b69e54..844949010 100644 --- a/js/components/projects/projectPreview/index.js +++ b/js/components/projects/projectPreview/index.js @@ -5,7 +5,7 @@ import { useRouteMatch } from 'react-router-dom'; import { loadCurrentSnapshotByID, loadSnapshotByProjectID } from '../redux/dispatchActions'; import { HeaderContext } from '../../header/headerContext'; import { DJANGO_CONTEXT } from '../../../utils/djangoContext'; -import { restoreCurrentActionsList } from '../../../reducers/tracking/dispatchActions'; +import { restoreCurrentActionsList, restoreAfterTargetActions } from '../../../reducers/tracking/dispatchActions'; import { NglContext } from '../../nglView/nglProvider'; export const ProjectPreview = memo(({}) => { @@ -36,15 +36,14 @@ export const ProjectPreview = memo(({}) => { throw new Error(error); }); } else { - dispatch(loadCurrentSnapshotByID(snapshotId || currentSnapshotID)) - .then(response => { - if (response !== false) { - if (response) { - if (response.session_project && `${response.session_project.id}` === projectId) { - isSnapshotLoaded.current = response.id; - setCanShow(true); - if (isActionRestoring === false) { - dispatch(restoreCurrentActionsList(nglViewList)); + if (currentSnapshotID === null) { + dispatch(loadCurrentSnapshotByID(snapshotId)) + .then(response => { + if (response !== false) { + if (response) { + if (response.session_project && `${response.session_project.id}` === projectId) { + isSnapshotLoaded.current = response.id; + setCanShow(true); } else { setCanShow(false); } @@ -53,12 +52,18 @@ export const ProjectPreview = memo(({}) => { setCanShow(false); } } - } - }) - .catch(error => { - setCanShow(false); - throw new Error(error); - }); + }) + .catch(error => { + setCanShow(false); + throw new Error(error); + }); + } else { + if (isActionRestoring === false) { + dispatch(restoreCurrentActionsList(nglViewList)); + } else if (nglViewList && nglViewList.length > 0) { + dispatch(restoreAfterTargetActions(nglViewList)); + } + } } }, [currentSnapshotID, dispatch, projectId, snapshotId, isActionRestoring, nglViewList, canShow]); diff --git a/js/components/projects/redux/dispatchActions.js b/js/components/projects/redux/dispatchActions.js index 12afa6586..d79067025 100644 --- a/js/components/projects/redux/dispatchActions.js +++ b/js/components/projects/redux/dispatchActions.js @@ -38,7 +38,7 @@ export const assignSnapshotToProject = ({ projectID, snapshotID, ...rest }) => ( created: response.data.created, children: response.data.children, parent: response.data.parent, - data: JSON.parse(response.data.data) + data: '[]' }) ) ) @@ -147,7 +147,7 @@ export const loadSnapshotByProjectID = projectID => (dispatch, getState) => { created: response.data.results[0].created, children: response.data.results[0].children, parent: response.data.results[0].parent, - data: JSON.parse(response.data.results[0].data) + data: '[]' }) ); return Promise.resolve(response.data.results[0].id); @@ -184,7 +184,7 @@ export const loadCurrentSnapshotByID = snapshotID => (dispatch, getState) => { created: response.data.created, children: response.data.children, parent: response.data.parent, - data: JSON.parse(response.data.data) + data: '[]' }) ); return Promise.resolve(response.data); @@ -294,7 +294,7 @@ const copySnapshot = (selectedSnapshot, projectID, history) => dispatch => { title: selectedSnapshot.title, author: (selectedSnapshot && selectedSnapshot.author && selectedSnapshot.author.id) || null, description: selectedSnapshot.description, - data: JSON.parse(selectedSnapshot.data), + data: '[]', created: selectedSnapshot.created, parent: null, children: selectedSnapshot.children, diff --git a/js/components/snapshot/redux/dispatchActions.js b/js/components/snapshot/redux/dispatchActions.js index 42ceb2101..86917955a 100644 --- a/js/components/snapshot/redux/dispatchActions.js +++ b/js/components/snapshot/redux/dispatchActions.js @@ -26,10 +26,6 @@ import { resetCurrentSnapshot, setCurrentSnapshot, setForceCreateProject } from import { selectFirstMolGroup } from '../../preview/moleculeGroups/redux/dispatchActions'; import { reloadDatasetsReducer } from '../../datasets/redux/actions'; import { saveCurrentActionsList } from '../../../reducers/tracking/dispatchActions'; -import { - sendTruckingActionsByProjectId, - appendAndSendTruckingActions -} from '../../../reducers/tracking/dispatchActions'; import { sendTrackingActionsByProjectId, manageSendTrackingActions } from '../../../reducers/tracking/dispatchActions'; export const getListOfSnapshots = () => (dispatch, getState) => { @@ -88,7 +84,7 @@ export const saveCurrentSnapshot = ({ dispatch(resetCurrentSnapshot()); return api({ url: `${base_url}/api/snapshots/`, - data: { type, title, author, description, data: JSON.stringify(data), created, parent, children, session_project }, + data: { type, title, author, description, created, parent, data: '[]', children, session_project }, method: METHOD.POST }) .then(response => @@ -187,8 +183,6 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se getState ) => { const state = getState(); - const { apiReducers, nglReducers, selectionReducers, previewReducers, datasetsReducers } = state; - const data = { apiReducers, nglReducers, selectionReducers, previewReducers, datasetsReducers }; const selectedSnapshotToSwitch = state.snapshotReducers.selectedSnapshotToSwitch; const disableRedirect = state.snapshotReducers.disableRedirect; @@ -214,7 +208,7 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se author, parent, session_project, - data: JSON.stringify(data), + data: '[]', children: [] }, method: METHOD.POST @@ -292,10 +286,6 @@ export const createNewSnapshotWithoutStateModification = ({ parent, session_project }) => (dispatch, getState) => { - const state = getState(); - const { apiReducers, nglReducers, selectionReducers, previewReducers, datasetsReducers } = state; - const data = { apiReducers, nglReducers, selectionReducers, previewReducers, datasetsReducers }; - if (!session_project) { return Promise.reject('Project ID is missing!'); } @@ -316,7 +306,7 @@ export const createNewSnapshotWithoutStateModification = ({ author, parent, session_project, - data: JSON.stringify(data), + data: '[]', children: [] }, method: METHOD.POST @@ -330,6 +320,7 @@ export const createNewSnapshotWithoutStateModification = ({ disableRedirect: true }) ); + dispatch(saveCurrentActionsList(res.data.id)); } }); }); diff --git a/js/components/target/redux/dispatchActions.js b/js/components/target/redux/dispatchActions.js index 52eb27834..3bd547e39 100644 --- a/js/components/target/redux/dispatchActions.js +++ b/js/components/target/redux/dispatchActions.js @@ -30,7 +30,9 @@ export const loadTargetList = onCancel => (dispatch, getState) => { }); }; -export const updateTarget = ({ target, setIsLoading, targetIdList, projectId, snapshotId }) => dispatch => { +export const updateTarget = ({ target, setIsLoading, targetIdList, projectId }) => (dispatch, getState) => { + const isActionRestoring = getState().trackingReducers.isActionRestoring; + // Get from the REST API let targetUnrecognisedFlag = true; if (target !== undefined) { @@ -64,7 +66,7 @@ export const updateTarget = ({ target, setIsLoading, targetIdList, projectId, sn return api({ url: `${base_url}/api/session-projects/${projectId}/` }) .then(response => { let promises = []; - if (!snapshotId) { + if (!isActionRestoring || isActionRestoring === false) { promises.push(dispatch(setTargetOn(response.data.target.id, true))); } promises.push( diff --git a/js/components/target/withUpdatingTarget.js b/js/components/target/withUpdatingTarget.js index ccaec02fa..fcecead88 100644 --- a/js/components/target/withUpdatingTarget.js +++ b/js/components/target/withUpdatingTarget.js @@ -28,7 +28,7 @@ export const withUpdatingTarget = WrappedContainer => { }, [setTargetUUIDs, snapshotUuid, uuid]); useEffect(() => { - updateTarget({ target, setIsLoading, targetIdList, projectId, snapshotId }).catch(error => { + updateTarget({ target, setIsLoading, targetIdList, projectId }).catch(error => { setState(() => { throw error; }); diff --git a/js/reducers/tracking/dispatchActions.js b/js/reducers/tracking/dispatchActions.js index 0706192e9..7e1899f5f 100644 --- a/js/reducers/tracking/dispatchActions.js +++ b/js/reducers/tracking/dispatchActions.js @@ -276,8 +276,6 @@ const getCollectionOfDatasetOfRepresentation = dataList => { }; export const restoreCurrentActionsList = (stages = []) => (dispatch, getState) => { - const state = getState(); - const isRestoring = state.trackingReducers.setIsActionsRestoring; dispatch(setIsActionsRestoring(true)); Promise.resolve(dispatch(restoreTruckingActions())).then(response => { @@ -324,7 +322,6 @@ const restoreStateBySavedActionList = stages => (dispatch, getState) => { const orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp); dispatch(restoreTargetActions(orderedActionList, stages)); - dispatch(restoreRepresentationActions(orderedActionList, stages)); }; const restoreTargetActions = (orderedActionList, stages) => (dispatch, getState) => { @@ -335,29 +332,40 @@ const restoreTargetActions = (orderedActionList, stages) => (dispatch, getState) let target = getTarget(targetAction.object_name, state); if (target) { dispatch(setTargetOn(target.id)); + } + } +}; - const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW); - const summaryView = stages.find(view => view.id === VIEWS.SUMMARY_VIEW); +export const restoreAfterTargetActions = stages => (dispatch, getState) => { + const state = getState(); - dispatch(shouldLoadProtein({ nglViewList: stages, currentSnapshotID: null, isLoadingCurrentSnapshot: false })); + const currentActionList = state.trackingReducers.current_actions_list; + const orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp); + const targetId = state.apiReducers.target_on; - dispatch( - loadMoleculeGroupsOfTarget({ - summaryView: summaryView.stage, - isStateLoaded: false, - setOldUrl: url => {}, - target_on: target.id - }) - ) - .catch(error => { - throw error; - }) - .finally(() => { - dispatch(restoreSitesActions(orderedActionList, summaryView)); - dispatch(loadAllMolecules(orderedActionList, target.id, majorView.stage)); - dispatch(loadAllDatasets(orderedActionList, target.id, majorView.stage)); - }); - } + if (targetId && stages && stages.length > 0) { + const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW); + const summaryView = stages.find(view => view.id === VIEWS.SUMMARY_VIEW); + + dispatch(shouldLoadProtein({ nglViewList: stages, currentSnapshotID: null, isLoadingCurrentSnapshot: false })); + + dispatch( + loadMoleculeGroupsOfTarget({ + summaryView: summaryView.stage, + isStateLoaded: false, + setOldUrl: url => {}, + target_on: targetId + }) + ) + .catch(error => { + throw error; + }) + .finally(() => { + dispatch(restoreSitesActions(orderedActionList, summaryView)); + dispatch(loadAllMolecules(orderedActionList, targetId, majorView.stage)); + dispatch(loadAllDatasets(orderedActionList, targetId, majorView.stage)); + dispatch(restoreRepresentationActions(orderedActionList, stages)); + }); } };