From 383f7e7679f0fb6cdda88f17d57f9d931c283175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1na=20Kohanov=C3=A1?= Date: Wed, 4 Nov 2020 16:34:52 +0100 Subject: [PATCH] #429 Restore current state as a result of the actions applied on the default state --- js/reducers/tracking/dispatchActions.js | 135 +++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/js/reducers/tracking/dispatchActions.js b/js/reducers/tracking/dispatchActions.js index ae62942f7..07c02bf85 100644 --- a/js/reducers/tracking/dispatchActions.js +++ b/js/reducers/tracking/dispatchActions.js @@ -1,6 +1,18 @@ import { setCurrentActionsList } from './actions'; -import { actionType } from './constants'; +import { actionType, actionObjectType } from './constants'; import { VIEWS } from '../../../js/constants/constants'; +import { setCurrentVector } from '../selection/actions'; +import { unmountPreviewComponent, shouldLoadProtein } from '../../components/preview/redux/dispatchActions'; +import { selectMoleculeGroup } from '../../components/preview/moleculeGroups/redux/dispatchActions'; +import { setMolGroupOn, setTargetOn } from '../api/actions'; +import { + addComplex, + addLigand, + addHitProtein, + addSurface, + addVector +} from '../../components/preview/molecule/redux/dispatchActions'; +import { colourList } from '../../components/preview/molecule/moleculeView'; export const selectCurrentActionsList = () => (dispatch, getState) => { const state = getState(); @@ -183,3 +195,124 @@ const getCollectionOfDatasetOfRepresentation = dataList => { } return list; }; + +export const restoreCurrentActionsList = (stages = []) => (dispatch, getState) => { + const state = getState(); + const currentActionList = state.trackingReducers.current_actions_list; + + let majorStages = stages.filter(view => view.id !== VIEWS.SUMMARY_VIEW); + + dispatch(unmountPreviewComponent(majorStages)); + dispatch(setMolGroupOn(undefined)); + //dispatch(setTargetOn(undefined)); + + const summaryView = stages.find(view => view.id === VIEWS.SUMMARY_VIEW); + const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW); + + dispatch(restoreStateBySavedActionList(currentActionList, state, summaryView, majorView, majorStages)); +}; + +const restoreStateBySavedActionList = (currentActionList, state, summaryView, majorView, nglViewList) => dispatch => { + let stage = majorView.stage; + let orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp); + + let targetAction = orderedActionList.find(action => action.action_type === actionType.TARGET_LOADED); + if (targetAction) { + let target = getTarget(targetAction.object_name, state); + if (target) { + dispatch(setTargetOn(target.id)); + dispatch(shouldLoadProtein({ nglViewList, currentSnapshotID: null, isLoadingCurrentSnapshot: false })); + } + } + + let sitesAction = orderedActionList.filter(action => action.action_type === actionType.SITE_TURNED_ON); + if (sitesAction) { + sitesAction.forEach(action => { + let molGroup = getMolGroup(action.object_name, state); + if (molGroup) { + dispatch(selectMoleculeGroup(molGroup, summaryView.stage)); + } + }); + } + + dispatch(restoreMoleculesActions(orderedActionList, stage, state)); + dispatch(restoreCompoundsActions(orderedActionList, stage, state)); + + let vectorAction = orderedActionList.find(action => action.action_type === actionType.VECTOR_SELECTED); + if (vectorAction) { + dispatch(setCurrentVector(vectorAction.object_name)); + } +}; + +const restoreMoleculesActions = (orderedActionList, stage, state) => dispatch => { + let moleculesAction = orderedActionList.filter( + action => action.object_type === actionObjectType.MOLECULE || action.object_type === actionObjectType.INSPIRATION + ); + + if (moleculesAction) { + dispatch(addNewType(moleculesAction, actionType.LIGAND_TURNED_ON, 'ligand', stage, state)); + dispatch(addNewType(moleculesAction, actionType.SIDECHAINS_TURNED_ON, 'protein', stage, state)); + dispatch(addNewType(moleculesAction, actionType.INTERACTIONS_TURNED_ON, 'complex', stage, state)); + dispatch(addNewType(moleculesAction, actionType.SURFACE_TURNED_ON, 'surface', stage, state)); + dispatch(addNewType(moleculesAction, actionType.VECTOR_SELECTED, 'vector', stage, state)); + } +}; + +const restoreCompoundsActions = (orderedActionList, stage, state) => dispatch => { + let compoundsAction = orderedActionList.filter( + action => + action.object_type === actionObjectType.COMPOUND || action.action_type === actionObjectType.CROSS_REFERENCE + ); + + if (compoundsAction) { + } +}; + +const addType = { + ligand: addLigand, + protein: addHitProtein, + complex: addComplex, + surface: addSurface, + vector: addVector +}; + +const addNewType = (moleculesAction, actionType, type, stage, state) => dispatch => { + let actions = moleculesAction.filter(action => action.action_type === actionType); + if (actions) { + actions.forEach(action => { + let molecule = getMolecule(action.object_name, state); + if (molecule) { + dispatch(addType[type](stage, molecule, colourList[molecule.id % colourList.length]), true); + } + }); + } +}; + +const getTarget = (targetName, state) => { + let targetList = state.apiReducers.target_id_list; + let target = targetList.find(target => target.title === targetName); + return target; +}; + +const getMolGroup = (molGroupName, state) => { + let molGroupList = state.apiReducers.mol_group_list; + let molGroup = molGroupList.find(group => group.description === molGroupName); + return molGroup; +}; + +const getMolecule = (moleculeName, state) => { + let moleculeList = state.apiReducers.all_mol_lists; + let molecule = null; + + if (moleculeList) { + for (const group in moleculeList) { + let molecules = moleculeList[group]; + + molecule = molecules.find(m => m.protein_code === moleculeName); + if (molecule && molecule != null) { + break; + } + } + } + return molecule; +};