Skip to content

Commit 5059f57

Browse files
author
Adriána Kohanová
committed
#454 Store and retrieve project actions in/from the backend
1 parent e7aaded commit 5059f57

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

js/constants/constants.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const main_identifier = 'x';
22
const sub_identifier = '_';
3+
const count_send_truck_actions = 5;
34

45
export const CONSTANTS = {
56
MAIN_IDENTIFIER: main_identifier,
6-
SUB_IDENTIFIER: sub_identifier
7+
SUB_IDENTIFIER: sub_identifier,
8+
COUNT_SEND_TRUCK_ACTIONS: count_send_truck_actions
79
};
810

911
export const VIEWS = {

js/reducers/tracking/dispatchActions.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ import {
5454
import * as listType from '../../constants/listTypes';
5555
import { assignRepresentationToComp } from '../../components/nglView/generatingObjects';
5656
import { deleteObject } from '../../../js/reducers/ngl/dispatchActions';
57-
import { appendToSendActionList, setSendActionsList, setIsActionsSending } from './actions';
58-
import { api, getCsrfToken, METHOD } from '../../../js/utils/api';
57+
import { setSendActionsList, setIsActionsSending } from './actions';
58+
import { api, METHOD } from '../../../js/utils/api';
5959
import { base_url } from '../../components/routes/constants';
60+
import { CONSTANTS } from '../../../js/constants/constants';
61+
import moment from 'moment';
6062

6163
export const selectCurrentActionsList = () => (dispatch, getState) => {
6264
const state = getState();
@@ -881,18 +883,14 @@ export const getCanRedo = () => (dispatch, getState) => {
881883
return state.undoableTrackingReducers.future.length > 0;
882884
};
883885

884-
export const checkSendTruckingActions = truckAction => (dispatch, getState) => {
886+
export const checkSendTruckingActions = () => (dispatch, getState) => {
885887
const state = getState();
886888
const currentProject = state.projectReducers.currentProject;
887889
const sendActions = state.trackingReducers.send_actions_list;
888890
const length = sendActions.length;
889891

890-
if (length >= 5) {
891-
Promise.resolve(dispatch(sendTruckingActions(sendActions, currentProject))).then(response => {
892-
dispatch(appendToSendActionList(truckAction));
893-
});
894-
} else {
895-
dispatch(appendToSendActionList(truckAction));
892+
if (length >= CONSTANTS.COUNT_SEND_TRUCK_ACTIONS) {
893+
dispatch(sendTruckingActions(sendActions, currentProject));
896894
}
897895
};
898896

@@ -902,14 +900,20 @@ const sendTruckingActions = (sendActions, currentProject) => (dispatch, getState
902900

903901
if (projectID) {
904902
dispatch(setIsActionsSending(true));
903+
904+
const dataToSend = {
905+
session_project: projectID,
906+
author: currentProject.authorID,
907+
last_update_date: moment().format(),
908+
actions: JSON.stringify(sendActions)
909+
};
905910
return api({
906-
url: `${base_url}/api/session-actions/${projectID}/`,
911+
url: `${base_url}/api/session-actions/`,
907912
method: METHOD.POST,
908-
data: JSON.stringify(sendActions)
913+
data: JSON.stringify(dataToSend)
909914
})
910915
.then(response => {
911916
dispatch(setSendActionsList([]));
912-
console.log('RES:' + response);
913917
})
914918
.catch(error => {
915919
throw new Error(error);
@@ -924,4 +928,4 @@ const sendTruckingActions = (sendActions, currentProject) => (dispatch, getState
924928
return Promise.resolve();
925929
}
926930
};
927-
export const getTruckingActions = sendActions => (dispatch, getState) => {};
931+
export const getTruckingActions = () => (dispatch, getState) => {};

js/reducers/tracking/trackingMiddleware.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { appendToActionList } from './actions';
1+
import { appendToActionList, appendToSendActionList } from './actions';
22
import { checkSendTruckingActions } from './dispatchActions';
33
import { constants } from './constants';
44
import { findTruckAction } from './trackingActions';
@@ -12,7 +12,8 @@ const trackingMiddleware = ({ dispatch, getState }) => next => action => {
1212
let truckAction = findTruckAction(action, state);
1313
if (truckAction && truckAction != null) {
1414
dispatch(appendToActionList(truckAction));
15-
dispatch(checkSendTruckingActions(truckAction));
15+
dispatch(appendToSendActionList(truckAction));
16+
dispatch(checkSendTruckingActions());
1617
}
1718
}
1819

0 commit comments

Comments
 (0)