Skip to content

Commit

Permalink
#455 Store and retrieve snapshot actions in/from the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriána Kohanová committed Nov 25, 2020
1 parent 83bdb85 commit c64a88a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 68 deletions.
9 changes: 0 additions & 9 deletions js/components/preview/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
37 changes: 21 additions & 16 deletions js/components/projects/projectPreview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(({}) => {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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]);

Expand Down
8 changes: 4 additions & 4 deletions js/components/projects/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[]'
})
)
)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 4 additions & 13 deletions js/components/snapshot/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 =>
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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!');
}
Expand All @@ -316,7 +306,7 @@ export const createNewSnapshotWithoutStateModification = ({
author,
parent,
session_project,
data: JSON.stringify(data),
data: '[]',
children: []
},
method: METHOD.POST
Expand All @@ -330,6 +320,7 @@ export const createNewSnapshotWithoutStateModification = ({
disableRedirect: true
})
);
dispatch(saveCurrentActionsList(res.data.id));
}
});
});
Expand Down
6 changes: 4 additions & 2 deletions js/components/target/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion js/components/target/withUpdatingTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
54 changes: 31 additions & 23 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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));
});
}
};

Expand Down

0 comments on commit c64a88a

Please sign in to comment.