From 11539d607e00cd122215cca80e976dc4d97a9816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1na=20Kohanov=C3=A1?= Date: Thu, 26 Nov 2020 08:01:32 +0100 Subject: [PATCH] #455 Store and retrieve snapshot actions in/from the backend --- js/components/preview/Preview.js | 2 +- .../snapshot/redux/dispatchActions.js | 4 +-- js/reducers/tracking/dispatchActions.js | 25 +++++++++++++++---- js/reducers/tracking/trackingActions.js | 3 ++- js/reducers/tracking/trackingReducers.js | 1 - 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/js/components/preview/Preview.js b/js/components/preview/Preview.js index be374f61b..a5f890105 100644 --- a/js/components/preview/Preview.js +++ b/js/components/preview/Preview.js @@ -91,7 +91,7 @@ const Preview = memo(({ isStateLoaded, hideProjects }) => { const dispatch = useDispatch(); const customDatasets = useSelector(state => state.datasetsReducers.datasets); - const [selectedDatasetIndex, setSelectedDatasetIndex] = useState(); + const [selectedDatasetIndex, setSelectedDatasetIndex] = useState(0); const currentDataset = customDatasets[selectedDatasetIndex]; const target_on = useSelector(state => state.apiReducers.target_on); const isTrackingRestoring = useSelector(state => state.trackingReducers.isTrackingCompoundsRestoring); diff --git a/js/components/snapshot/redux/dispatchActions.js b/js/components/snapshot/redux/dispatchActions.js index 86917955a..6d5d22b56 100644 --- a/js/components/snapshot/redux/dispatchActions.js +++ b/js/components/snapshot/redux/dispatchActions.js @@ -215,7 +215,7 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se }).then(res => { // redirect to project with newest created snapshot /:projectID/:snapshotID if (res.data.id && session_project) { - dispatch(saveCurrentActionsList(res.data.id)); + dispatch(saveCurrentActionsList(res.data.id, session_project)); if (disableRedirect === false) { // Really bad usage or redirection. Hint for everybody in this line ignore it, but in other parts of code @@ -320,7 +320,7 @@ export const createNewSnapshotWithoutStateModification = ({ disableRedirect: true }) ); - dispatch(saveCurrentActionsList(res.data.id)); + dispatch(saveCurrentActionsList(res.data.id, session_project)); } }); }); diff --git a/js/reducers/tracking/dispatchActions.js b/js/reducers/tracking/dispatchActions.js index 7e1899f5f..8314a080e 100644 --- a/js/reducers/tracking/dispatchActions.js +++ b/js/reducers/tracking/dispatchActions.js @@ -67,10 +67,24 @@ import { setIsActionsRestoring } from './actions'; -export const saveCurrentActionsList = snapshotID => (dispatch, getState) => { +export const saveCurrentActionsList = (snapshotID, projectID) => (dispatch, getState) => { + const state = getState(); + + let actionList = state.trackingReducers.track_actions_list; + + if (!actionList || actionList.length === 0) { + Promise.resolve(dispatch(getTrackingActions(projectID))).then(response => { + actionList = response; + dispatch(saveActionsList(snapshotID, actionList)); + }); + } else { + dispatch(saveActionsList(snapshotID, actionList)); + } +}; + +export const saveActionsList = (snapshotID, actionList) => (dispatch, getState) => { const state = getState(); - const actionList = state.trackingReducers.track_actions_list; const currentTargetOn = state.apiReducers.target_on; const currentSites = state.selectionReducers.mol_group_selection; const currentLigands = state.selectionReducers.fragmentDisplayList; @@ -89,10 +103,11 @@ export const saveCurrentActionsList = snapshotID => (dispatch, getState) => { const currentDatasetBuyList = state.datasetsReducers.compoundsToBuyDatasetMap; const currentobjectsInView = state.nglReducers.objectsInView; - const orderedActionList = actionList.reverse((a, b) => a.timestamp - b.timestamp); const currentTargets = (currentTargetOn && [currentTargetOn]) || []; const currentVectorSmiles = (currentVector && [currentVector]) || []; + let orderedActionList = actionList.reverse((a, b) => a.timestamp - b.timestamp); + let currentActions = []; getCurrentActionList(orderedActionList, actionType.TARGET_LOADED, getCollection(currentTargets), currentActions); @@ -1078,7 +1093,7 @@ const getTrackingActions = projectID => (dispatch, getState) => { let projectActions = [...listToSet, ...sendActions]; dispatch(setProjectActionList(projectActions)); - return Promise.resolve(); + return Promise.resolve(projectActions); }) .catch(error => { throw new Error(error); @@ -1089,7 +1104,7 @@ const getTrackingActions = projectID => (dispatch, getState) => { } else { let projectActions = [...sendActions]; dispatch(setProjectActionList(projectActions)); - return Promise.resolve(); + return Promise.resolve(projectActions); } }; diff --git a/js/reducers/tracking/trackingActions.js b/js/reducers/tracking/trackingActions.js index 31530908e..d65242a17 100644 --- a/js/reducers/tracking/trackingActions.js +++ b/js/reducers/tracking/trackingActions.js @@ -9,9 +9,10 @@ export const findTrackAction = (action, state) => { const username = DJANGO_CONTEXT['username']; const target_on_name = state.apiReducers.target_on_name; const isUndoRedoAction = state.trackingReducers.isUndoRedoAction; + const isActionRestoring = state.trackingReducers.isActionRestoring; let trackAction = null; - if (isUndoRedoAction === false && action.skipTracking !== true) { + if (isUndoRedoAction === false && isActionRestoring === false && action.skipTracking !== true) { if (action.type.includes(apiConstants.SET_TARGET_ON)) { if (action.target_on) { let targetName = getTargetName(action.target_on, state); diff --git a/js/reducers/tracking/trackingReducers.js b/js/reducers/tracking/trackingReducers.js index edccd2567..eb8269339 100644 --- a/js/reducers/tracking/trackingReducers.js +++ b/js/reducers/tracking/trackingReducers.js @@ -10,7 +10,6 @@ export const INITIAL_STATE = { isActionsSending: false, isActionsLoading: false, isActionSaving: false, - setIsActionsRestoring: false, send_actions_list: [], project_actions_list: [], isActionRestoring: false