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 19, 2020
1 parent db940e5 commit efb869e
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { OBJECT_TYPE } from '../../../nglView/constants';
import { setSortDialogOpen } from '../../molecule/redux/actions';
import { resetCurrentCompoundsSettings } from '../../compounds/redux/actions';
import { reloadSession } from '../../../snapshot/redux/dispatchActions';
import { restoreCurrentActionsList } from '../../../../reducers/tracking/dispatchActions';

export const clearAfterDeselectingMoleculeGroup = ({ molGroupId, currentMolGroup, majorViewStage }) => (
dispatch,
Expand Down Expand Up @@ -246,10 +247,13 @@ export const clearMoleculeGroupSelection = ({ getNglView }) => (dispatch, getSta

export const restoreFromCurrentSnapshot = ({ nglViewList }) => (dispatch, getState) => {
const snapshot = getState().projectReducers.currentSnapshot.data;

dispatch(reloadSession(snapshot, nglViewList));
};

export const restoreSnapshotActions = ({ nglViewList }) => (dispatch, getState) => {
dispatch(restoreCurrentActionsList(nglViewList));
};

export const onDeselectMoleculeGroup = ({ moleculeGroup, stageSummaryView, majorViewStage }) => (
dispatch,
getState
Expand Down
2 changes: 1 addition & 1 deletion js/components/preview/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const shouldLoadProtein = ({
(!routeSnapshotID || routeSnapshotID === currentSnapshotID.toString()) &&
currentSnapshotData !== null
) {
dispatch(reloadSession(currentSnapshotData, nglViewList));
//dispatch(reloadSession(currentSnapshotData, nglViewList));
}

if (targetOnName !== undefined) {
Expand Down
16 changes: 12 additions & 4 deletions js/components/projects/projectPreview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ 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 { NglContext } from '../../nglView/nglProvider';

export const ProjectPreview = memo(({}) => {
const { setSnackBarTitle } = useContext(HeaderContext);
const [canShow, setCanShow] = useState(undefined);
const isSnapshotLoaded = useRef(undefined);
let match = useRouteMatch();
const dispatch = useDispatch();
const { nglViewList } = useContext(NglContext);

const projectId = match && match.params && match.params.projectId;
const snapshotId = match && match.params && match.params.snapshotId;
const currentSnapshotID = useSelector(state => state.projectReducers.currentSnapshot.id);
const currentProject = useSelector(state => state.projectReducers.currentProject);
const isActionRestoring = useSelector(state => state.trackingReducers.isActionRestoring);

useEffect(() => {
if (!snapshotId && currentSnapshotID === null) {
Expand All @@ -38,12 +43,15 @@ export const ProjectPreview = memo(({}) => {
if (response.session_project && `${response.session_project.id}` === projectId) {
isSnapshotLoaded.current = response.id;
setCanShow(true);
if (isActionRestoring === false) {
dispatch(restoreCurrentActionsList(nglViewList));
} else {
setCanShow(false);
}
} else {
isSnapshotLoaded.current = response;
setCanShow(false);
}
} else {
isSnapshotLoaded.current = response;
setCanShow(false);
}
}
})
Expand All @@ -52,7 +60,7 @@ export const ProjectPreview = memo(({}) => {
throw new Error(error);
});
}
}, [currentSnapshotID, dispatch, projectId, snapshotId]);
}, [currentSnapshotID, dispatch, projectId, snapshotId, isActionRestoring, nglViewList, canShow]);

if (canShow === false) {
setSnackBarTitle('Not valid snapshot!');
Expand Down
3 changes: 3 additions & 0 deletions js/components/snapshot/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { base_url, URLS } from '../../routes/constants';
import { resetCurrentSnapshot, setCurrentSnapshot, setForceCreateProject } from '../../projects/redux/actions';
import { selectFirstMolGroup } from '../../preview/moleculeGroups/redux/dispatchActions';
import { reloadDatasetsReducer } from '../../datasets/redux/actions';
import { saveCurrentActionsList } from '../../../reducers/tracking/dispatchActions';

export const getListOfSnapshots = () => (dispatch, getState) => {
const userID = DJANGO_CONTEXT['pk'] || null;
Expand Down Expand Up @@ -215,6 +216,8 @@ 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));

if (disableRedirect === false) {
// Really bad usage or redirection. Hint for everybody in this line ignore it, but in other parts of code
// use react-router !
Expand Down
4 changes: 2 additions & 2 deletions js/components/snapshot/withSnapshotManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DJANGO_CONTEXT } from '../../utils/djangoContext';
import { useDisableUserInteraction } from '../helpers/useEnableUserInteracion';
import { activateSnapshotDialog, saveAndShareSnapshot } from './redux/dispatchActions';
import { NglContext } from '../nglView/nglProvider';
import { restoreFromCurrentSnapshot } from '../preview/moleculeGroups/redux/dispatchActions';
import { restoreSnapshotActions } from '../preview/moleculeGroups/redux/dispatchActions';

/**
* Created by ricgillams on 13/06/2018.
Expand Down Expand Up @@ -64,7 +64,7 @@ export const withSnapshotManagement = WrappedComponent => {
<Button
key="restoreSnapshot"
color="primary"
onClick={() => dispatch(restoreFromCurrentSnapshot({ nglViewList }))}
onClick={() => dispatch(restoreSnapshotActions({ nglViewList }))}
startIcon={<Restore />}
disabled={disableShareButton || disableUserInteraction}
>
Expand Down
12 changes: 8 additions & 4 deletions js/components/target/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const loadTargetList = onCancel => (dispatch, getState) => {
});
};

export const updateTarget = ({ target, setIsLoading, targetIdList, projectId }) => dispatch => {
export const updateTarget = ({ target, setIsLoading, targetIdList, projectId, snapshotId }) => dispatch => {
// Get from the REST API
let targetUnrecognisedFlag = true;
if (target !== undefined) {
Expand Down Expand Up @@ -63,8 +63,11 @@ export const updateTarget = ({ target, setIsLoading, targetIdList, projectId })
setIsLoading(true);
return api({ url: `${base_url}/api/session-projects/${projectId}/` })
.then(response => {
return Promise.all([
dispatch(setTargetOn(response.data.target.id)),
let promises = [];
if (!snapshotId) {
promises.push(dispatch(setTargetOn(response.data.target.id)));
}
promises.push(
dispatch(
setCurrentProject({
projectID: response.data.id,
Expand All @@ -75,7 +78,8 @@ export const updateTarget = ({ target, setIsLoading, targetIdList, projectId })
tags: JSON.parse(response.data.tags)
})
)
]);
);
return Promise.all(promises);
})
.finally(() => setIsLoading(false));
}
Expand Down
5 changes: 3 additions & 2 deletions js/components/target/withUpdatingTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const withUpdatingTarget = WrappedContainer => {
const target = match && match.params && match.params.target;
const uuid = match && match.params && match.params.uuid;
const snapshotUuid = match && match.params && match.params.snapshotUuid;
const snapshotId = match && match.params && match.params.snapshotId;
const projectId = match && match.params && match.params.projectId;

const { isLoading, setIsLoading } = useContext(HeaderContext);
Expand All @@ -27,12 +28,12 @@ export const withUpdatingTarget = WrappedContainer => {
}, [setTargetUUIDs, snapshotUuid, uuid]);

useEffect(() => {
updateTarget({ target, setIsLoading, targetIdList, projectId }).catch(error => {
updateTarget({ target, setIsLoading, targetIdList, projectId, snapshotId }).catch(error => {
setState(() => {
throw error;
});
});
}, [setIsLoading, target, updateTarget, targetIdList, projectId]);
}, [setIsLoading, target, updateTarget, targetIdList, projectId, snapshotId]);

if (isLoading === true) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions js/components/tracking/trackingModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Check, Clear, Save, Restore, Close } from '@material-ui/icons';
import palette from '../../theme/palette';
import { Panel } from '../common';
import {
selectCurrentActionsList,
saveCurrentActionsList,
restoreCurrentActionsList,
setProjectTruckingActions
} from '../../reducers/tracking/dispatchActions';
Expand Down Expand Up @@ -62,7 +62,7 @@ export const TrackingModal = memo(({ openModal, onModalClose }) => {
}

const actions = [
<IconButton color={'inherit'} onClick={() => dispatch(selectCurrentActionsList())}>
<IconButton color={'inherit'} onClick={() => dispatch(saveCurrentActionsList())}>
<Tooltip title="Save">
<Save />
</Tooltip>
Expand Down
14 changes: 14 additions & 0 deletions js/reducers/tracking/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,17 @@ export const setProjectActionList = function(project_actions_list) {
project_actions_list: project_actions_list
};
};

export const setIsActionsSaving = function(isActionSaving) {
return {
type: constants.SET_IS_ACTIONS_SAVING,
isActionSaving: isActionSaving
};
};

export const setIsActionsRestoring = function(isActionRestoring) {
return {
type: constants.SET_IS_ACTIONS_RESTORING,
isActionRestoring: isActionRestoring
};
};
4 changes: 3 additions & 1 deletion js/reducers/tracking/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export const constants = {
APPEND_SEND_ACTIONS_LIST: prefix + 'APPEND_SEND_ACTIONS_LIST',
SET_IS_ACTIONS_SENDING: prefix + 'SET_IS_ACTIONS_SENDING',
SET_IS_ACTIONS_LOADING: prefix + 'SET_IS_ACTIONS_LOADING',
SET_PROJECT_ACTIONS_LIST: prefix + 'SET_PROJECT_ACTIONS_LIST'
SET_PROJECT_ACTIONS_LIST: prefix + 'SET_PROJECT_ACTIONS_LIST',
SET_IS_ACTIONS_SAVING: prefix + 'SET_IS_ACTIONS_SAVING',
SET_IS_ACTIONS_RESTORING: prefix + 'SET_IS_ACTIONS_RESTORING'
};

export const actionType = {
Expand Down
Loading

0 comments on commit efb869e

Please sign in to comment.