2020-12-29 13:55:04 -08:00
|
|
|
import {
|
|
|
|
ADMIN_REPORTS_FETCH_SUCCESS,
|
|
|
|
ADMIN_USERS_FETCH_SUCCESS,
|
2020-12-29 16:38:58 -08:00
|
|
|
ADMIN_USERS_DELETE_REQUEST,
|
2020-12-29 16:22:31 -08:00
|
|
|
ADMIN_USERS_DELETE_SUCCESS,
|
2020-12-29 16:38:58 -08:00
|
|
|
ADMIN_USERS_APPROVE_REQUEST,
|
2020-12-29 16:22:31 -08:00
|
|
|
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';
|
2020-08-24 15:18:53 -07:00
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
reports: ImmutableList(),
|
2020-12-29 13:55:04 -08:00
|
|
|
users: ImmutableMap(),
|
2020-08-24 16:00:09 -07:00
|
|
|
open_report_count: 0,
|
2020-12-29 13:55:04 -08:00
|
|
|
awaitingApproval: ImmutableOrderedSet(),
|
2020-08-24 15:18:53 -07:00
|
|
|
});
|
|
|
|
|
2020-12-29 13:55:04 -08:00
|
|
|
function importUsers(state, users) {
|
|
|
|
return state.withMutations(state => {
|
|
|
|
users.forEach(user => {
|
|
|
|
if (user.approval_pending) {
|
2020-12-29 16:22:31 -08:00
|
|
|
state.update('awaitingApproval', orderedSet => orderedSet.add(user.nickname));
|
2020-12-29 13:55:04 -08:00
|
|
|
}
|
2020-12-29 16:22:31 -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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-24 15:18:53 -07:00
|
|
|
export default function admin(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case ADMIN_REPORTS_FETCH_SUCCESS:
|
2020-08-24 16:00:09 -07:00
|
|
|
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);
|
2020-12-29 16:38:58 -08:00
|
|
|
case ADMIN_USERS_DELETE_REQUEST:
|
2020-12-29 16:22:31 -08:00
|
|
|
case ADMIN_USERS_DELETE_SUCCESS:
|
|
|
|
return deleteUsers(state, action.nicknames);
|
2020-12-29 16:38:58 -08:00
|
|
|
case ADMIN_USERS_APPROVE_REQUEST:
|
2020-12-29 16:53:06 -08:00
|
|
|
return state.update('awaitingApproval', set => set.subtract(action.nicknames));
|
2020-12-29 16:22:31 -08:00
|
|
|
case ADMIN_USERS_APPROVE_SUCCESS:
|
|
|
|
return approveUsers(state, action.users);
|
2020-08-24 15:18:53 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|