Skip to content

Commit

Permalink
#429 Restore current state as a result of the actions applied on the …
Browse files Browse the repository at this point in the history
…default state
  • Loading branch information
Adriána Kohanová committed Nov 4, 2020
1 parent 27dad43 commit 383f7e7
Showing 1 changed file with 134 additions and 1 deletion.
135 changes: 134 additions & 1 deletion js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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;
};

0 comments on commit 383f7e7

Please sign in to comment.