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/preview/moleculeGroups/redux/dispatchActions.js b/js/components/preview/moleculeGroups/redux/dispatchActions.js index d85945b09..7ece4791e 100644 --- a/js/components/preview/moleculeGroups/redux/dispatchActions.js +++ b/js/components/preview/moleculeGroups/redux/dispatchActions.js @@ -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, @@ -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 diff --git a/js/components/preview/redux/dispatchActions.js b/js/components/preview/redux/dispatchActions.js index 68f3eb46d..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/addProjectDetail/index.js b/js/components/projects/addProjectDetail/index.js index 24706cf3e..b48ed4682 100644 --- a/js/components/projects/addProjectDetail/index.js +++ b/js/components/projects/addProjectDetail/index.js @@ -9,6 +9,7 @@ import { Autocomplete } from '@material-ui/lab'; import { Button } from '../../common/Inputs/Button'; import { useDispatch, useSelector } from 'react-redux'; import { createProjectFromSnapshotDialog } from '../redux/dispatchActions'; +import { manageSendTrackingActions } from '../../../reducers/tracking/dispatchActions'; const useStyles = makeStyles(theme => ({ body: { @@ -34,6 +35,7 @@ export const AddProjectDetail = memo(({ handleCloseModal }) => { const dispatch = useDispatch(); const targetId = useSelector(state => state.apiReducers.target_on); + const projectID = useSelector(state => state.projectReducers.currentProject.projectID); const isProjectModalLoading = useSelector(state => state.projectReducers.isProjectModalLoading); const [tags, setTags] = React.useState([]); @@ -65,11 +67,17 @@ export const AddProjectDetail = memo(({ handleCloseModal }) => { author: DJANGO_CONTEXT['pk'] || null, tags: JSON.stringify(tags) }; - dispatch(createProjectFromSnapshotDialog(data)).catch(error => { - setState(() => { - throw error; + + const oldProjectID = projectID; + dispatch(createProjectFromSnapshotDialog(data)) + .then(() => { + dispatch(manageSendTrackingActions(oldProjectID, true)); + }) + .catch(error => { + setState(() => { + throw error; + }); }); - }); }} > {({ submitForm, isSubmitting }) => ( diff --git a/js/components/projects/projectPreview/index.js b/js/components/projects/projectPreview/index.js index 03f07c8a6..844949010 100644 --- a/js/components/projects/projectPreview/index.js +++ b/js/components/projects/projectPreview/index.js @@ -5,6 +5,8 @@ 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, restoreAfterTargetActions } from '../../../reducers/tracking/dispatchActions'; +import { NglContext } from '../../nglView/nglProvider'; export const ProjectPreview = memo(({}) => { const { setSnackBarTitle } = useContext(HeaderContext); @@ -12,10 +14,13 @@ export const ProjectPreview = memo(({}) => { 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) { @@ -31,28 +36,36 @@ 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 (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); + } } else { + isSnapshotLoaded.current = response; setCanShow(false); } - } else { - isSnapshotLoaded.current = response; - 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]); + }, [currentSnapshotID, dispatch, projectId, snapshotId, isActionRestoring, nglViewList, canShow]); if (canShow === false) { setSnackBarTitle('Not valid snapshot!'); 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 8d5e9d123..6d5d22b56 100644 --- a/js/components/snapshot/redux/dispatchActions.js +++ b/js/components/snapshot/redux/dispatchActions.js @@ -25,10 +25,8 @@ 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 { - sendTruckingActionsByProjectId, - appendAndSendTruckingActions -} from '../../../reducers/tracking/dispatchActions'; +import { saveCurrentActionsList } from '../../../reducers/tracking/dispatchActions'; +import { sendTrackingActionsByProjectId, manageSendTrackingActions } from '../../../reducers/tracking/dispatchActions'; export const getListOfSnapshots = () => (dispatch, getState) => { const userID = DJANGO_CONTEXT['pk'] || null; @@ -86,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 => @@ -185,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; @@ -212,13 +208,15 @@ export const createNewSnapshot = ({ title, description, type, author, parent, se author, parent, session_project, - data: JSON.stringify(data), + data: '[]', children: [] }, method: METHOD.POST }).then(res => { // redirect to project with newest created snapshot /:projectID/:snapshotID if (res.data.id && session_project) { + 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 // use react-router ! @@ -253,6 +251,7 @@ export const activateSnapshotDialog = (loggedInUserID = undefined, finallyShareS const projectID = state.projectReducers.currentProject.projectID; const currentSnapshotAuthor = state.projectReducers.currentSnapshot.author; + dispatch(manageSendTrackingActions()); dispatch(setDisableRedirect(finallyShareSnapshot)); if (!loggedInUserID && targetId) { @@ -265,7 +264,7 @@ export const activateSnapshotDialog = (loggedInUserID = undefined, finallyShareS }; dispatch(createProjectFromSnapshotDialog(data)) .then(() => { - dispatch(appendAndSendTruckingActions(null)); + dispatch(manageSendTrackingActions(projectID, true)); dispatch(setOpenSnapshotSavingDialog(true)); }) .catch(error => { @@ -287,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!'); } @@ -311,7 +306,7 @@ export const createNewSnapshotWithoutStateModification = ({ author, parent, session_project, - data: JSON.stringify(data), + data: '[]', children: [] }, method: METHOD.POST @@ -325,6 +320,7 @@ export const createNewSnapshotWithoutStateModification = ({ disableRedirect: true }) ); + dispatch(saveCurrentActionsList(res.data.id, session_project)); } }); }); @@ -358,7 +354,7 @@ export const saveAndShareSnapshot = (target = undefined) => (dispatch, getState) const parent = null; const session_project = projectID; - dispatch(sendTruckingActionsByProjectId(projectID, author)); + dispatch(sendTrackingActionsByProjectId(projectID, author)); return dispatch( createNewSnapshotWithoutStateModification({ title, description, type, author, parent, session_project }) diff --git a/js/components/snapshot/withSnapshotManagement.js b/js/components/snapshot/withSnapshotManagement.js index 4bd6eafac..7c8b0b662 100644 --- a/js/components/snapshot/withSnapshotManagement.js +++ b/js/components/snapshot/withSnapshotManagement.js @@ -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. @@ -64,7 +64,7 @@ export const withSnapshotManagement = WrappedComponent => {