Skip to content

Commit

Permalink
#454 Store and retrieve project actions in/from the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriána Kohanová committed Nov 13, 2020
1 parent 6469799 commit d5ba1e2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
28 changes: 28 additions & 0 deletions js/reducers/tracking/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,31 @@ export const setIsUndoRedoAction = function(isUndoRedoAction) {
isUndoRedoAction: isUndoRedoAction
};
};

export const setIsActionsSending = function(isActionsSending) {
return {
type: constants.SET_IS_ACTIONS_SENDING,
isActionsSending: isActionsSending
};
};

export const setIsActionsLoading = function(isActionsLoading) {
return {
type: constants.SET_IS_ACTIONS_LOADING,
isActionsLoading: isActionsLoading
};
};

export const setSendActionsList = function(truck_actions_list) {
return {
type: constants.SET_SEND_ACTIONS_LIST,
send_actions_list: truck_actions_list
};
};

export const appendToSendActionList = function(truck_action) {
return {
type: constants.APPEND_SEND_ACTIONS_LIST,
truck_action: truck_action
};
};
6 changes: 5 additions & 1 deletion js/reducers/tracking/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export const constants = {
SET_CURRENT_ACTIONS_LIST: prefix + 'SET_CURRENT_ACTIONS_LIST',
SET_IS_TRACKING_COMPOUNDS_RESTORING: prefix + 'SET_IS_TRACKING_COMPOUNDS_RESTORING',
SET_IS_TRACKING_MOLECULES_RESTORING: prefix + 'SET_IS_TRACKING_MOLECULES_RESTORING',
SET_IS_UNDO_REDO_ACTION: prefix + 'SET_IS_UNDO_REDO_ACTION'
SET_IS_UNDO_REDO_ACTION: prefix + 'SET_IS_UNDO_REDO_ACTION',
SET_SEND_ACTIONS_LIST: prefix + 'SET_SEND_ACTIONS_LIST',
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'
};

export const actionType = {
Expand Down
48 changes: 48 additions & 0 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ import {
import * as listType from '../../constants/listTypes';
import { assignRepresentationToComp } from '../../components/nglView/generatingObjects';
import { deleteObject } from '../../../js/reducers/ngl/dispatchActions';
import { appendToSendActionList, setSendActionsList, setIsActionsSending } from './actions';
import { api, getCsrfToken, METHOD } from '../../../js/utils/api';
import { base_url } from '../../components/routes/constants';

export const selectCurrentActionsList = () => (dispatch, getState) => {
const state = getState();
Expand Down Expand Up @@ -877,3 +880,48 @@ export const getCanRedo = () => (dispatch, getState) => {
const state = getState();
return state.undoableTrackingReducers.future.length > 0;
};

export const checkSendTruckingActions = truckAction => (dispatch, getState) => {
const state = getState();
const currentProject = state.projectReducers.currentProject;
const sendActions = state.trackingReducers.send_actions_list;
const length = sendActions.length;

if (length >= 5) {
Promise.resolve(dispatch(sendTruckingActions(sendActions, currentProject))).then(response => {
dispatch(appendToSendActionList(truckAction));
});
} else {
dispatch(appendToSendActionList(truckAction));
}
};

const sendTruckingActions = (sendActions, currentProject) => (dispatch, getState) => {
if (currentProject) {
const projectID = currentProject && currentProject.projectID;

if (projectID) {
dispatch(setIsActionsSending(true));
return api({
url: `${base_url}/api/session-actions/${projectID}/`,
method: METHOD.PUT,
data: JSON.stringify(sendActions)
})
.then(response => {
dispatch(setSendActionsList([]));
console.log('RES:' + response);
})
.catch(error => {
throw new Error(error);
})
.finally(() => {
dispatch(setIsActionsSending(false));
});
} else {
return Promise.resolve();
}
} else {
return Promise.resolve();
}
};
export const getTruckingActions = sendActions => (dispatch, getState) => {};
2 changes: 2 additions & 0 deletions js/reducers/tracking/trackingMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { appendToActionList } from './actions';
import { checkSendTruckingActions } from './dispatchActions';
import { constants } from './constants';
import { findTruckAction } from './trackingActions';

Expand All @@ -11,6 +12,7 @@ const trackingMiddleware = ({ dispatch, getState }) => next => action => {
let truckAction = findTruckAction(action, state);
if (truckAction && truckAction != null) {
dispatch(appendToActionList(truckAction));
dispatch(checkSendTruckingActions(truckAction));
}
}

Expand Down
26 changes: 25 additions & 1 deletion js/reducers/tracking/trackingReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export const INITIAL_STATE = {
current_actions_list: [],
isTrackingMoleculesRestoring: false,
isTrackingCompoundsRestoring: false,
isUndoRedoAction: false
isUndoRedoAction: false,
isActionsSending: false,
isActionsLoading: false,
send_actions_list: []
};

export function trackingReducers(state = INITIAL_STATE, action = {}) {
Expand Down Expand Up @@ -35,11 +38,32 @@ export function trackingReducers(state = INITIAL_STATE, action = {}) {
return Object.assign({}, state, {
isTrackingCompoundsRestoring: action.isTrackingCompoundsRestoring
});

case constants.SET_IS_UNDO_REDO_ACTION:
return Object.assign({}, state, {
isUndoRedoAction: action.isUndoRedoAction
});

case constants.SET_IS_ACTIONS_SENDING:
return Object.assign({}, state, {
isActionsSending: action.isActionsSending
});

case constants.SET_IS_ACTIONS_LOADING:
return Object.assign({}, state, {
isActionsLoading: action.isActionsLoading
});

case constants.SET_SEND_ACTIONS_LIST:
return Object.assign({}, state, {
send_actions_list: action.send_actions_list
});

case constants.APPEND_SEND_ACTIONS_LIST:
return Object.assign({}, state, {
send_actions_list: [...new Set([...state.send_actions_list, action.truck_action])]
});

default:
return state;
}
Expand Down

0 comments on commit d5ba1e2

Please sign in to comment.