Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update flag count after moderator flags an annotation themselves #347

Merged
merged 4 commits into from
Apr 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/sidebar/reducers/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,20 @@ var update = {
var annotations = state.annotations.map(function (annot) {
var match = (annot.id && annot.id === action.id);
if (match) {
return Object.assign({}, annot, {
if (annot.flagged === action.isFlagged) {
return annot;
}

var newAnn = Object.assign({}, annot, {
flagged: action.isFlagged,
});
if (newAnn.moderation) {
var countDelta = action.isFlagged ? 1 : -1;
newAnn.moderation = Object.assign({}, annot.moderation, {
flagCount: annot.moderation.flagCount + countDelta,
});
}
return newAnn;
} else {
return annot;
}
Expand Down
57 changes: 57 additions & 0 deletions src/sidebar/reducers/test/annotations-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var thunk = require('redux-thunk').default;
var annotations = require('../annotations');
var fixtures = require('../../test/annotation-fixtures');
var util = require('../util');
var unroll = require('../../../shared/test/util').unroll;

var actions = annotations.actions;

Expand Down Expand Up @@ -59,8 +60,10 @@ describe('annotations reducer', function () {
it('sets the `hidden` state to `true`', function () {
var store = createStore();
var ann = fixtures.moderatedAnnotation({ hidden: false });

store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.hideAnnotation(ann.id));

var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, true);
});
Expand All @@ -70,10 +73,64 @@ describe('annotations reducer', function () {
it('sets the `hidden` state to `false`', function () {
var store = createStore();
var ann = fixtures.moderatedAnnotation({ hidden: true });

store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.unhideAnnotation(ann.id));

var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, false);
});
});

describe('#updateFlagStatus', function () {
unroll('updates the flagged status of an annotation', function (testCase) {
var store = createStore();
var ann = fixtures.defaultAnnotation();
ann.flagged = testCase.wasFlagged;
ann.moderation = testCase.oldModeration;

store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.updateFlagStatus(ann.id, testCase.nowFlagged));

var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.flagged, testCase.nowFlagged);
assert.deepEqual(storeAnn.moderation, testCase.newModeration);
}, [{
// Non-moderator flags annotation
wasFlagged: false,
nowFlagged: true,
oldModeration: undefined,
newModeration: undefined,
}, {
// Non-moderator un-flags annotation
wasFlagged: true,
nowFlagged: false,
oldModeration: undefined,
newModeration: undefined,
},{
// Moderator un-flags an already unflagged annotation
wasFlagged: false,
nowFlagged: false,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 1 },
},{
// Moderator flags an already flagged annotation
wasFlagged: true,
nowFlagged: true,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 1 },
},{
// Moderator flags annotation
wasFlagged: false,
nowFlagged: true,
oldModeration: { flagCount: 0 },
newModeration: { flagCount: 1 },
},{
// Moderator un-flags annotation
wasFlagged: true,
nowFlagged: false,
oldModeration: { flagCount: 1 },
newModeration: { flagCount: 0 },
}]);
});
});
9 changes: 0 additions & 9 deletions src/sidebar/test/annotation-ui-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,6 @@ describe('annotationUI', function () {
});
});

describe('#updateFlagStatus', function () {
it('updates the flaged status of an annotation', function () {
var annot = defaultAnnotation();
annotationUI.addAnnotations([annot]);
annotationUI.updateFlagStatus(annot.id, true);
assert.equal(annotationUI.getState().annotations[0].flagged, true);
});
});

describe('selector functions', function () {
// The individual state management modules in reducers/*.js define various
// 'selector' functions for extracting data from the app state. These are
Expand Down