Skip to content

Commit 371c146

Browse files
committed
Implement the answering from UI
1 parent 0755005 commit 371c146

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

src/api/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function normalizeErrors(err) {
4747
function apiCall(url, options = {}) {
4848
const token = TokenService.get();
4949

50+
if (options.body) {
51+
options.body = JSON.stringify(options.body)
52+
}
53+
5054
return fetch(apiUrl(url), {
5155
...options,
5256
headers: {
@@ -81,13 +85,23 @@ function getFilePair(experimentId, pairId) {
8185
return apiCall(`/api/experiments/${experimentId}/file-pairs/${pairId}`);
8286
}
8387

88+
function putAnswer(experimentId, assignmentId, answer) {
89+
return apiCall(
90+
`/api/experiments/${experimentId}/assignments/${assignmentId}`, {
91+
method: "PUT",
92+
body: answer
93+
}
94+
);
95+
}
96+
8497
// eslint-disable-next-line
8598
let exportObject = {
8699
me,
87100

88101
getExperiment,
89102
getAssignments,
90103
getFilePair,
104+
putAnswer,
91105
};
92106

93107
// eslint-disable-next-line

src/state/experiment.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ const reducer = (state = initialState, action) => {
8989
...state,
9090
assignments: state.assignments.map(a => {
9191
if (a.id === action.id) {
92+
const currentDuration = new Date() - state.currentAssigmentStartTime;
9293
return {
9394
...a,
9495
answer: action.answer,
95-
duration: new Date() - state.currentAssigmentStartTime,
96+
duration: a.duration + currentDuration,
9697
};
9798
}
9899
return a;
@@ -125,6 +126,19 @@ const loadFilePairIfNeeded = id => (dispatch, getState) => {
125126
);
126127
};
127128

129+
const pushAnswer = (experimentId, assignmentId, answer) => (dispatch, getState) => {
130+
const { experiment } = getState();
131+
const assigment = experiment.assignments.find(a => a.id === assignmentId);
132+
const answerPayload = {
133+
answer: answer,
134+
duration: assigment.duration
135+
}
136+
return api.putAnswer(experimentId, assignmentId, answerPayload).catch(e => {
137+
dispatch(addErrors(e));
138+
dispatch({ type: LOAD_ERROR, error: e });
139+
});
140+
};
141+
128142
export const selectAssigment = id => (dispatch, getState) => {
129143
const { experiment } = getState();
130144
const assigment = experiment.assignments.find(a => a.id === +id);
@@ -187,6 +201,7 @@ export const mark = (assignmentId, answer) => dispatch => {
187201
id: assignmentId,
188202
answer,
189203
});
204+
dispatch(pushAnswer(experimentId, assignmentId, answer));
190205
return dispatch(nextAssigment());
191206
};
192207

0 commit comments

Comments
 (0)