Unverified Commit dce257e3 authored by Robert Knight's avatar Robert Knight Committed by Sean Hammond

Remove `moderation` state from Redux store

Moderation state was originally going to be retrieved via a separate API
call from /api/search but is now returned as a `moderation` field in the
annotation response.

This commit removes the separate `moderation` state from the Redux
store.
parent b3024621
......@@ -39,7 +39,6 @@ var thunk = require('redux-thunk').default;
var reducers = require('./reducers');
var annotationsReducer = require('./reducers/annotations');
var framesReducer = require('./reducers/frames');
var moderationReducer = require('./reducers/moderation');
var selectionReducer = require('./reducers/selection');
var sessionReducer = require('./reducers/session');
var viewerReducer = require('./reducers/viewer');
......@@ -96,7 +95,6 @@ module.exports = function ($rootScope, settings) {
var actionCreators = redux.bindActionCreators(Object.assign({},
annotationsReducer.actions,
framesReducer.actions,
moderationReducer.actions,
selectionReducer.actions,
sessionReducer.actions,
viewerReducer.actions
......@@ -119,9 +117,6 @@ module.exports = function ($rootScope, settings) {
frames: framesReducer.frames,
isHiddenByModerator: moderationReducer.isHiddenByModerator,
flagCount: moderationReducer.flagCount,
isSidebar: viewerReducer.isSidebar,
}, store.getState);
......
......@@ -19,7 +19,6 @@
var annotations = require('./annotations');
var frames = require('./frames');
var moderation = require('./moderation');
var selection = require('./selection');
var session = require('./session');
var viewer = require('./viewer');
......@@ -30,7 +29,6 @@ function init(settings) {
{},
annotations.init(),
frames.init(),
moderation.init(),
selection.init(settings),
session.init(),
viewer.init()
......@@ -40,7 +38,6 @@ function init(settings) {
var update = util.createReducer(Object.assign(
annotations.update,
frames.update,
moderation.update,
selection.update,
session.update,
viewer.update
......
'use strict';
/**
* This module defines application state and actions related to flagging and
* moderation status of annotations.
*/
var toSet = require('../util/array-util').toSet;
var util = require('./util');
function init() {
return {
// Map of ID -> number of times annotation has been flagged by users.
flagCounts: {},
// IDs of annotations hidden by a moderator.
hiddenByModerator: {},
};
}
/**
* Return a copy of `map` with `key` added or removed.
*
* @param {Object} map
* @param {string} key
* @param {boolean} enable
*/
function toggle(map, key, enable) {
var newMap = Object.assign({}, map);
if (enable) {
newMap[key] = true;
} else {
delete newMap[key];
}
return newMap;
}
var update = {
ANNOTATION_HIDDEN_CHANGED: function (state, action) {
return {
hiddenByModerator: toggle(state.hiddenByModerator, action.id, action.hidden),
};
},
FETCHED_FLAG_COUNTS: function (state, action) {
return { flagCounts: action.flagCounts };
},
FETCHED_HIDDEN_IDS: function (state, action) {
return { hiddenByModerator: toSet(action.ids) };
},
};
var actions = util.actionTypes(update);
/**
* Update the flag counts for annotations.
*
* @param {Object} flagCounts - Map from ID to count of flags
*/
function fetchedFlagCounts(flagCounts) {
return {
type: actions.FETCHED_FLAG_COUNTS,
flagCounts: flagCounts,
};
}
/**
* Update the set of annotations hidden by a moderator.
*
* @param {string[]} ids
*/
function fetchedHiddenByModeratorIds(ids) {
return {
type: actions.FETCHED_HIDDEN_IDS,
ids: ids,
};
}
/**
* An annotation was hidden or unhidden by a moderator.
*
* @param {string} id
* @param {boolean} hidden
*/
function annotationHiddenChanged(id, hidden) {
return {
type: actions.ANNOTATION_HIDDEN_CHANGED,
id: id,
hidden: hidden,
};
}
/**
* Return the number of items an annotation with a given `id` has been flagged
* by members of the annotation's group.
*
* @param {Object} state
* @param {string} id
*/
function flagCount(state, id) {
return state.flagCounts[id] || 0;
}
/**
* Return `true` if an annotation was hidden by a moderator.
*
* @param {Object} state
* @param {string} id
*/
function isHiddenByModerator(state, id) {
return !!state.hiddenByModerator[id];
}
module.exports = {
init: init,
update: update,
actions: {
annotationHiddenChanged: annotationHiddenChanged,
fetchedFlagCounts: fetchedFlagCounts,
fetchedHiddenByModeratorIds: fetchedHiddenByModeratorIds,
},
// Selectors
isHiddenByModerator: isHiddenByModerator,
flagCount: flagCount,
};
'use strict';
var moderation = require('../moderation');
var util = require('../util');
var init = moderation.init;
var actions = moderation.actions;
var update = util.createReducer(moderation.update);
describe('moderation reducer', function () {
describe('#fetchedFlagCounts', function () {
it('updates the flag counts', function () {
var state = update(init(), actions.fetchedFlagCounts({ 'flagged-id': 1, 'also-flagged-id': 2 }));
assert.deepEqual(state.flagCounts, { 'flagged-id': 1, 'also-flagged-id': 2});
});
});
describe('#flagCount', function () {
it('returns the number of times the annotation was flagged', function () {
var state = update(init(), actions.fetchedFlagCounts({ 'flagged-id': 1, 'also-flagged-id': 2 }));
assert.equal(moderation.flagCount(state, 'flagged-id'), 1);
assert.equal(moderation.flagCount(state, 'not-flagged-id'), 0);
});
});
describe('#fetchedHiddenByModeratorIds', function () {
it('updates the set of moderated IDs', function () {
var state = update(init(), actions.fetchedHiddenByModeratorIds(['hidden-id']));
assert.deepEqual(state.hiddenByModerator, {'hidden-id': true});
});
});
describe('#isHiddenByModerator', function () {
var state = update(init(), actions.fetchedHiddenByModeratorIds(['hidden-id']));
it('returns true if the annotation was hidden', function () {
assert.isTrue(moderation.isHiddenByModerator(state, 'hidden-id'));
});
it('returns false if the annotation was not hidden', function () {
assert.isFalse(moderation.isHiddenByModerator(state, 'not-hidden-id'));
});
});
describe('#annotationHiddenChanged', function () {
it('alters the hidden status of the annotation', function () {
var state = update(init(), actions.fetchedHiddenByModeratorIds(['hidden-id']));
state = update(state, actions.annotationHiddenChanged('hidden-id', false));
assert.isFalse(moderation.isHiddenByModerator(state, 'hidden-id'));
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment