Skip to content

Commit 3a9db85

Browse files
author
Adriána Kohanová
committed
#429 Restore current state as a result of the actions applied on the default state
1 parent 15effdf commit 3a9db85

File tree

8 files changed

+135
-64
lines changed

8 files changed

+135
-64
lines changed

js/components/datasets/redux/reducer.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,15 @@ export const datasetsReducers = (state = INITIAL_STATE, action = {}) => {
363363
return Object.assign({}, state, lists);
364364

365365
case constants.RESET_DATASETS_STATE:
366-
return INITIAL_STATE;
366+
const datasetsLists = {
367+
ligandLists: reloadLists([], 'ligandLists'),
368+
proteinLists: reloadLists([], 'proteinLists'),
369+
complexLists: reloadLists([], 'complexLists'),
370+
surfaceLists: reloadLists([], 'surfaceLists'),
371+
inspirationLists: reloadLists([], 'inspirationLists'),
372+
compoundsToBuyDatasetMap: reloadLists([], 'compoundsToBuyDatasetMap')
373+
};
374+
return Object.assign({}, state, { ...INITIAL_STATE, ...datasetsLists });
367375

368376
default:
369377
return state;

js/components/preview/Preview.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ const Preview = memo(({ isStateLoaded, hideProjects }) => {
9494
const [selectedDatasetIndex, setSelectedDatasetIndex] = useState();
9595
const currentDataset = customDatasets[selectedDatasetIndex];
9696
const target_on = useSelector(state => state.apiReducers.target_on);
97+
const isTrackingRestoring = useSelector(state => state.trackingReducers.isTrackingCompoundsRestoring);
9798

9899
/*
99100
Loading datasets
100101
*/
101102
useEffect(() => {
102-
if (customDatasets.length === 0) {
103+
if (customDatasets.length === 0 && isTrackingRestoring === false) {
103104
dispatch(setMoleculeListIsLoading(true));
104105
dispatch(loadDataSets(target_on))
105106
.then(results => {
@@ -115,7 +116,7 @@ const Preview = memo(({ isStateLoaded, hideProjects }) => {
115116
dispatch(setMoleculeListIsLoading(false));
116117
});
117118
}
118-
}, [customDatasets.length, dispatch, target_on]);
119+
}, [customDatasets.length, dispatch, target_on, isTrackingRestoring]);
119120

120121
const [molGroupsHeight, setMolGroupsHeight] = useState(0);
121122
const [filterItemsHeight, setFilterItemsHeight] = useState(0);

js/components/preview/molecule/moleculeList.js

+23-20
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import { useRouteMatch } from 'react-router-dom';
6060
import { setSortDialogOpen } from './redux/actions';
6161
import { setMoleculeList, setAllMolLists } from '../../../reducers/api/actions';
6262
import { AlertModal } from '../../common/Modal/AlertModal';
63-
import {selectMoleculeGroup} from '../moleculeGroups/redux/dispatchActions'
63+
import { selectMoleculeGroup } from '../moleculeGroups/redux/dispatchActions';
6464

6565
const useStyles = makeStyles(theme => ({
6666
container: {
@@ -252,7 +252,8 @@ export const MoleculeList = memo(({ height, setFilterItemsHeight, filterItemsHei
252252
const all_mol_lists = useSelector(state => state.apiReducers.all_mol_lists);
253253
const directDisplay = useSelector(state => state.apiReducers.direct_access);
254254
const directAccessProcessed = useSelector(state => state.apiReducers.direct_access_processed);
255-
255+
const isTrackingRestoring = useSelector(state => state.trackingReducers.isTrackingMoleculesRestoring);
256+
256257
const proteinsHasLoaded = useSelector(state => state.nglReducers.proteinsHasLoaded);
257258

258259
const [predefinedFilter, setPredefinedFilter] = useState(filter !== undefined ? filter.predefined : DEFAULT_FILTER);
@@ -314,26 +315,31 @@ export const MoleculeList = memo(({ height, setFilterItemsHeight, filterItemsHei
314315
target_on &&
315316
mol_group_list &&
316317
mol_group_list.length > 0 &&
317-
Object.keys(all_mol_lists).length <= 0
318+
Object.keys(all_mol_lists).length <= 0 &&
319+
isTrackingRestoring === false
318320
) {
319321
let promises = [];
320322
mol_group_list.forEach(molGroup => {
321323
let id = molGroup.id;
322324
let url = getUrl({ list_type, target_on, mol_group_on: id });
323-
promises.push(loadAllMolsFromMolGroup({
324-
url,
325-
mol_group: id
326-
}))
325+
promises.push(
326+
loadAllMolsFromMolGroup({
327+
url,
328+
mol_group: id
329+
})
330+
);
327331
});
328-
Promise.all(promises).then((results) => {
329-
let listToSet = {};
330-
results.forEach(molResult => {
331-
listToSet[molResult.mol_group] = molResult.molecules;
332-
});
333-
dispatch(setAllMolLists(listToSet))
334-
}).catch((err) => console.log(err));
332+
Promise.all(promises)
333+
.then(results => {
334+
let listToSet = {};
335+
results.forEach(molResult => {
336+
listToSet[molResult.mol_group] = molResult.molecules;
337+
});
338+
dispatch(setAllMolLists(listToSet));
339+
})
340+
.catch(err => console.log(err));
335341
}
336-
}, [proteinsHasLoaded, mol_group_list, list_type, target_on, dispatch, all_mol_lists]);
342+
}, [proteinsHasLoaded, mol_group_list, list_type, target_on, dispatch, all_mol_lists, isTrackingRestoring]);
337343

338344
useEffect(() => {
339345
loadAllMolecules();
@@ -345,16 +351,13 @@ export const MoleculeList = memo(({ height, setFilterItemsHeight, filterItemsHei
345351
mol_group_list.forEach(mg => {
346352
molGroupMap[mg.description] = mg.id;
347353
});
348-
return molGroupMap;
354+
return molGroupMap;
349355
}
350356
}, [mol_group_list]);
351357

352358
useEffect(() => {
353359
const allMolsGroupsCount = Object.keys(all_mol_lists || {}).length;
354-
if (
355-
(proteinsHasLoaded === true || proteinsHasLoaded === null) &&
356-
allMolsGroupsCount > 0
357-
) {
360+
if ((proteinsHasLoaded === true || proteinsHasLoaded === null) && allMolsGroupsCount > 0) {
358361
dispatch(setMoleculeList({ ...(all_mol_lists[mol_group_on] || []) }));
359362
if (!directAccessProcessed && directDisplay && directDisplay.molecules && directDisplay.molecules.length > 0) {
360363
dispatch(applyDirectSelection(stage, stageSummaryView));

js/components/tracking/trackingModal.js

+50-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import React, { memo } from 'react';
2-
import { useSelector } from 'react-redux';
1+
import React, { memo, useContext } from 'react';
2+
import { useSelector, useDispatch } from 'react-redux';
33
import Modal from '../common/Modal';
4-
import { Grid, makeStyles } from '@material-ui/core';
4+
import { Grid, makeStyles, IconButton, Tooltip } from '@material-ui/core';
55
import { Timeline, TimelineEvent } from 'react-event-timeline';
6-
import { Check, Clear } from '@material-ui/icons';
6+
import { Check, Clear, Save, Restore, Close } from '@material-ui/icons';
77
import palette from '../../theme/palette';
88
import { Panel } from '../common';
9+
import { selectCurrentActionsList, restoreCurrentActionsList } from '../../reducers/tracking/dispatchActions';
10+
import { NglContext } from '../nglView/nglProvider';
911

1012
const useStyles = makeStyles(theme => ({
1113
customModal: {
@@ -34,6 +36,9 @@ const useStyles = makeStyles(theme => ({
3436

3537
export const TrackingModal = memo(({ openModal, onModalClose }) => {
3638
const classes = useStyles();
39+
const dispatch = useDispatch();
40+
const { nglViewList } = useContext(NglContext);
41+
3742
const actionList = useSelector(state => state.trackingReducers.truck_actions_list);
3843
const orderedActionList = actionList.sort((a, b) => a.timestamp - b.timestamp);
3944

@@ -42,37 +47,59 @@ export const TrackingModal = memo(({ openModal, onModalClose }) => {
4247
onModalClose();
4348
}
4449

50+
const actions = [
51+
<IconButton color={'inherit'} onClick={() => dispatch(selectCurrentActionsList())}>
52+
<Tooltip title="Save">
53+
<Save />
54+
</Tooltip>
55+
</IconButton>,
56+
<IconButton color={'inherit'} onClick={() => dispatch(restoreCurrentActionsList(nglViewList))}>
57+
<Tooltip title="Restore">
58+
<Restore />
59+
</Tooltip>
60+
</IconButton>,
61+
<IconButton color={'inherit'} onClick={() => onModalClose()}>
62+
<Tooltip title="Close">
63+
<Close />
64+
</Tooltip>
65+
</IconButton>
66+
];
67+
4568
return (
4669
<Modal
4770
otherClasses={classes.customModal}
4871
otherContentClasses={classes.customContentModal}
4972
open={openModal}
5073
onClose={() => onModalClose()}
5174
>
52-
<Panel bodyOverflow={true} hasHeader={true} title="Action List">
75+
<Panel bodyOverflow={true} hasHeader={true} title="Action List" headerActions={actions}>
5376
<Grid container justify="space-between" className={classes.containerExpanded}>
5477
<div className={classes.divContainer}>
5578
<div className={classes.divScrollable}>
5679
<Timeline>
5780
{orderedActionList &&
58-
orderedActionList.map((data, index) => (
59-
<TimelineEvent
60-
key={index}
61-
title={data.text}
62-
createdAt={new Date(data.timestamp).toLocaleString()}
63-
icon={
64-
data.type.includes('OFF') === true ||
65-
data.type.includes('DESELECTED') === true ||
66-
data.type.includes('REMOVED') === true ? (
67-
<Clear />
68-
) : (
69-
<Check />
70-
)
71-
}
72-
iconColor={palette.primary.main}
73-
className={classes.timelineEvent}
74-
></TimelineEvent>
75-
))}
81+
orderedActionList.map((data, index) => {
82+
if (data && data != null) {
83+
return (
84+
<TimelineEvent
85+
key={index}
86+
title={data.text}
87+
createdAt={new Date(data.timestamp).toLocaleString()}
88+
icon={
89+
data.type.includes('OFF') === true ||
90+
data.type.includes('DESELECTED') === true ||
91+
data.type.includes('REMOVED') === true ? (
92+
<Clear />
93+
) : (
94+
<Check />
95+
)
96+
}
97+
iconColor={palette.primary.main}
98+
className={classes.timelineEvent}
99+
></TimelineEvent>
100+
);
101+
}
102+
})}
76103
</Timeline>
77104
</div>
78105
</div>

js/reducers/tracking/actions.js

+14
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ export const setCurrentActionsList = function(current_actions_list) {
2020
current_actions_list: current_actions_list
2121
};
2222
};
23+
24+
export const setIsTrackingMoleculesRestoring = function(isTrackingMoleculesRestoring) {
25+
return {
26+
type: constants.SET_IS_TRACKING_MOLECULES_RESTORING,
27+
isTrackingMoleculesRestoring: isTrackingMoleculesRestoring
28+
};
29+
};
30+
31+
export const setIsTrackingCompoundsRestoring = function(isTrackingCompoundsRestoring) {
32+
return {
33+
type: constants.SET_IS_TRACKING_COMPOUNDS_RESTORING,
34+
isTrackingCompoundsRestoring: isTrackingCompoundsRestoring
35+
};
36+
};

js/reducers/tracking/constants.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const prefix = 'REDUCERS_TRACKING_';
33
export const constants = {
44
SET_ACTIONS_LIST: prefix + 'SET_ACTIONS_LIST',
55
APPEND_ACTIONS_LIST: prefix + 'APPEND_ACTIONS_LIST',
6-
SET_CURRENT_ACTIONS_LIST: prefix + 'SET_CURRENT_ACTIONS_LIST'
6+
SET_CURRENT_ACTIONS_LIST: prefix + 'SET_CURRENT_ACTIONS_LIST',
7+
SET_IS_TRACKING_COMPOUNDS_RESTORING: prefix + 'SET_IS_TRACKING_COMPOUNDS_RESTORING',
8+
SET_IS_TRACKING_MOLECULES_RESTORING: prefix + 'SET_IS_TRACKING_MOLECULES_RESTORING'
79
};
810

911
export const actionType = {

js/reducers/tracking/dispatchActions.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setCurrentActionsList } from './actions';
1+
import { setCurrentActionsList, setIsTrackingMoleculesRestoring, setIsTrackingCompoundsRestoring } from './actions';
22
import { actionType, actionObjectType } from './constants';
33
import { VIEWS } from '../../../js/constants/constants';
44
import { setCurrentVector } from '../selection/actions';
@@ -24,7 +24,10 @@ import {
2424
loadDataSets,
2525
loadDatasetCompoundsWithScores
2626
} from '../../components/datasets/redux/dispatchActions';
27-
import { appendMoleculeToCompoundsOfDatasetToBuy } from '../../components/datasets/redux/actions';
27+
import {
28+
appendMoleculeToCompoundsOfDatasetToBuy,
29+
setMoleculeListIsLoading
30+
} from '../../components/datasets/redux/actions';
2831
import { setAllMolLists } from '../api/actions';
2932
import { getUrl, loadAllMolsFromMolGroup } from '../../../js/utils/genericList';
3033
import * as listType from '../../constants/listTypes';
@@ -157,7 +160,8 @@ const mapCurrentAction = action => {
157160
timestamp: action.timestamp,
158161
object_name: action.object_name,
159162
object_type: action.object_type,
160-
action_type: action.type
163+
action_type: action.type,
164+
dataset_id: action.dataset_id
161165
});
162166
};
163167

@@ -212,6 +216,8 @@ const getCollectionOfDatasetOfRepresentation = dataList => {
212216
};
213217

214218
export const restoreCurrentActionsList = (stages = []) => (dispatch, getState) => {
219+
dispatch(setIsTrackingMoleculesRestoring(true));
220+
dispatch(setIsTrackingCompoundsRestoring(true));
215221
dispatch(unmountPreviewComponent(stages));
216222
dispatch(resetTargetState());
217223
dispatch(restoreStateBySavedActionList(stages));
@@ -226,9 +232,10 @@ const restoreStateBySavedActionList = stages => (dispatch, getState) => {
226232
dispatch(restoreTargetActions(orderedActionList, stages));
227233
};
228234

229-
const restoreTargetActions = (orderedActionList, stages, state) => (dispatch, getState) => {
235+
const restoreTargetActions = (orderedActionList, stages) => (dispatch, getState) => {
230236
const state = getState();
231237

238+
const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW);
232239
const summaryView = stages.find(view => view.id === VIEWS.SUMMARY_VIEW);
233240

234241
let targetAction = orderedActionList.find(action => action.action_type === actionType.TARGET_LOADED);
@@ -250,32 +257,28 @@ const restoreTargetActions = (orderedActionList, stages, state) => (dispatch, ge
250257
throw error;
251258
})
252259
.finally(() => {
253-
const majorView = stages.find(view => view.id === VIEWS.MAJOR_VIEW);
254-
const stage = majorView.stage;
255-
256260
dispatch(restoreSitesActions(orderedActionList, summaryView));
257-
dispatch(loadAllMolecules(orderedActionList, target.id, stage));
261+
dispatch(loadAllMolecules(orderedActionList, target.id, majorView.stage));
258262
});
259263

260-
dispatch(loadAllDatasaets);
264+
dispatch(loadAllDatasets(orderedActionList, target.id, majorView.stage));
261265
}
262266
}
263267
};
264268

265-
const loadAllDatasaets = (orderedActionList, target_on, stage) => (dispatch, getState) => {
266-
//dispatch(setMoleculeListIsLoading(true));
269+
const loadAllDatasets = (orderedActionList, target_on, stage) => (dispatch, getState) => {
270+
dispatch(setMoleculeListIsLoading(true));
267271
dispatch(loadDataSets(target_on))
268272
.then(results => {
269-
//setSelectedDatasetIndex(0);
270-
271273
return dispatch(loadDatasetCompoundsWithScores());
272274
})
273275
.catch(error => {
274276
throw new Error(error);
275277
})
276278
.finally(() => {
277-
//dispatch(setMoleculeListIsLoading(false));
278279
dispatch(restoreCompoundsActions(orderedActionList, stage));
280+
dispatch(setMoleculeListIsLoading(false));
281+
dispatch(setIsTrackingCompoundsRestoring(false));
279282
});
280283
};
281284

@@ -304,6 +307,7 @@ const loadAllMolecules = (orderedActionList, target_on, stage) => (dispatch, get
304307
});
305308
dispatch(setAllMolLists(listToSet));
306309
dispatch(restoreMoleculesActions(orderedActionList, stage));
310+
dispatch(setIsTrackingMoleculesRestoring(false));
307311
})
308312
.catch(err => console.log(err));
309313
};
@@ -390,7 +394,7 @@ const addNewType = (moleculesAction, actionType, type, stage, state) => dispatch
390394
actions.forEach(action => {
391395
let data = getMolecule(action.object_name, state);
392396
if (data) {
393-
dispatch(addType[type](stage, data, colourList[data.id % colourList.length]), true);
397+
dispatch(addType[type](stage, data, colourList[data.id % colourList.length]));
394398
}
395399
});
396400
}
@@ -402,7 +406,7 @@ const addNewTypeCompound = (moleculesAction, actionType, type, stage, state) =>
402406
actions.forEach(action => {
403407
let data = getCompound(action.object_name, state);
404408
if (data) {
405-
dispatch(addTypeCompound[type](stage, data, colourList[data.id % colourList.length]), data.datasetID);
409+
dispatch(addTypeCompound[type](stage, data, colourList[data.id % colourList.length], action.dataset_id));
406410
}
407411
});
408412
}

0 commit comments

Comments
 (0)