Skip to content

Commit

Permalink
Fixing redux updates that handle set/unset accept flag of solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
krulis-martin committed Oct 3, 2021
1 parent 41e1539 commit 05efd6d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/redux/modules/solutions.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,27 @@ const reducer = handleActions(
if (flag === 'accepted') {
// Accepted flag requires special treatement
const assignmentId = state.getIn(['resources', id, 'data', 'assignmentId']);

if (value) {
// Accepted solution needs to be updated
const userId = state.getIn(['resources', id, 'data', 'solution', 'userId']);
const userId = state.getIn(['resources', id, 'data', 'authorId']);
state = state.updateIn(
['resources', id, 'data'],
data => data.set('isBestSolution', true) // accepted also becomes best solution
);

if (assignmentId && userId)
if (assignmentId && userId) {
state = state
// All other solutions from the same assignment by the same author needs to be updated
.update('resources', resources =>
resources.map((item, itemId) => {
const aId = item.getIn(['data', 'assignmentId']);
const uId = item.getIn(['data', 'solution', 'userId']);
const uId = item.getIn(['data', 'authorId']);
return itemId === id || aId !== assignmentId || uId !== userId
? item // no modification (either it is accepted solution, or it is solution from another assignment/by another user)
: item.update('data', data => data.set('accepted', false).set('isBestSolution', false)); // no other solution can be accepted nor best
})
);
}
} else {
// Unaccepted -> best solution flag may change to another solution...
const assignmentStats = assignments.find(a => a.id === assignmentId);
Expand Down
8 changes: 4 additions & 4 deletions test/redux/modules/submissions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ describe('Solutions', () => {
const id = 'abcde123';
const id2 = `__${id}`;
const id3 = `__${id2}`;
const userId = 'userID';
const authorId = 'userID';
const assignmentId = 'assignmentId';
const pendingState = fromJS({
resources: {
[id]: {
status: 'FULFILLED',
data: { id, accepted: false, isBestSolution: false, assignmentId, solution: { userId } },
data: { id, accepted: false, isBestSolution: false, assignmentId, authorId },
},
[id2]: {
status: 'FULFILLED',
data: { id: id2, accepted: true, isBestSolution: true, assignmentId, solution: { userId } },
data: { id: id2, accepted: true, isBestSolution: true, assignmentId, authorId },
},
[id3]: {
status: 'FULFILLED',
data: { id: id3, accepted: true, isBestSolution: false, assignmentId, solution: { userId } },
data: { id: id3, accepted: true, isBestSolution: false, assignmentId, authorId },
},
},
});
Expand Down

0 comments on commit 05efd6d

Please sign in to comment.