bigbuffet-rw/app/soapbox/reducers/admin.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-12-29 13:55:04 -08:00
import {
ADMIN_REPORTS_FETCH_SUCCESS,
ADMIN_USERS_FETCH_SUCCESS,
ADMIN_USERS_DELETE_REQUEST,
ADMIN_USERS_DELETE_SUCCESS,
ADMIN_USERS_APPROVE_REQUEST,
ADMIN_USERS_APPROVE_SUCCESS,
2020-12-29 13:55:04 -08:00
} from '../actions/admin';
import {
Map as ImmutableMap,
List as ImmutableList,
OrderedSet as ImmutableOrderedSet,
fromJS,
} from 'immutable';
const initialState = ImmutableMap({
reports: ImmutableList(),
2020-12-29 13:55:04 -08:00
users: ImmutableMap(),
open_report_count: 0,
2020-12-29 13:55:04 -08:00
awaitingApproval: ImmutableOrderedSet(),
});
2020-12-29 13:55:04 -08:00
function importUsers(state, users) {
return state.withMutations(state => {
users.forEach(user => {
if (user.approval_pending) {
state.update('awaitingApproval', orderedSet => orderedSet.add(user.nickname));
2020-12-29 13:55:04 -08:00
}
state.setIn(['users', user.nickname], fromJS(user));
});
});
}
function deleteUsers(state, nicknames) {
return state.withMutations(state => {
nicknames.forEach(nickname => {
state.update('awaitingApproval', orderedSet => orderedSet.delete(nickname));
state.deleteIn(['users', nickname]);
});
});
}
function approveUsers(state, users) {
return state.withMutations(state => {
users.forEach(user => {
state.update('awaitingApproval', orderedSet => orderedSet.delete(user.nickname));
state.setIn(['users', user.nickname], fromJS(user));
2020-12-29 13:55:04 -08:00
});
});
}
export default function admin(state = initialState, action) {
switch(action.type) {
case ADMIN_REPORTS_FETCH_SUCCESS:
if (action.params && action.params.state === 'open') {
return state
.set('reports', fromJS(action.data.reports))
.set('open_report_count', action.data.total);
} else {
return state.set('reports', fromJS(action.data.reports));
}
2020-12-29 13:55:04 -08:00
case ADMIN_USERS_FETCH_SUCCESS:
return importUsers(state, action.data.users);
case ADMIN_USERS_DELETE_REQUEST:
case ADMIN_USERS_DELETE_SUCCESS:
return deleteUsers(state, action.nicknames);
case ADMIN_USERS_APPROVE_REQUEST:
2020-12-29 16:53:06 -08:00
return state.update('awaitingApproval', set => set.subtract(action.nicknames));
case ADMIN_USERS_APPROVE_SUCCESS:
return approveUsers(state, action.users);
default:
return state;
}
};