Admin: refactor Reports reducer

This commit is contained in:
Alex Gleason 2020-12-31 14:29:31 -06:00
parent b38dfda4be
commit 51faa660ca
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
7 changed files with 23 additions and 20 deletions

View File

@ -55,8 +55,8 @@ export function fetchReports(params) {
dispatch({ type: ADMIN_REPORTS_FETCH_REQUEST, params }); dispatch({ type: ADMIN_REPORTS_FETCH_REQUEST, params });
return api(getState) return api(getState)
.get('/api/pleroma/admin/reports', { params }) .get('/api/pleroma/admin/reports', { params })
.then(({ data }) => { .then(({ data: { reports } }) => {
dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, data, params }); dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports, params });
}).catch(error => { }).catch(error => {
dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params }); dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params });
}); });

View File

@ -6,7 +6,7 @@ import { Helmet } from'react-helmet';
const getNotifTotals = state => { const getNotifTotals = state => {
const notifications = state.getIn(['notifications', 'unread'], 0); const notifications = state.getIn(['notifications', 'unread'], 0);
const chats = state.get('chats').reduce((acc, curr) => acc + Math.min(curr.get('unread', 0), 1), 0); const chats = state.get('chats').reduce((acc, curr) => acc + Math.min(curr.get('unread', 0), 1), 0);
const reports = state.getIn(['admin', 'open_report_count'], 0); const reports = state.getIn(['admin', 'openReports']).count();
const approvals = state.getIn(['admin', 'awaitingApproval']).count(); const approvals = state.getIn(['admin', 'awaitingApproval']).count();
return notifications + chats + reports + approvals; return notifications + chats + reports + approvals;
}; };

View File

@ -10,7 +10,7 @@ import { FormattedMessage } from 'react-intl';
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
instance: state.get('instance'), instance: state.get('instance'),
approvalCount: state.getIn(['admin', 'awaitingApproval']).count(), approvalCount: state.getIn(['admin', 'awaitingApproval']).count(),
reportsCount: state.getIn(['admin', 'open_report_count']), reportsCount: state.getIn(['admin', 'openReports']).count(),
}); });
export default @connect(mapStateToProps) export default @connect(mapStateToProps)

View File

@ -17,7 +17,6 @@ const messages = defineMessages({
const mapStateToProps = (state, props) => ({ const mapStateToProps = (state, props) => ({
mode: modeFromInstance(state.get('instance')), mode: modeFromInstance(state.get('instance')),
openReportCount: state.getIn(['admin', 'open_report_count']),
}); });
const generateConfig = mode => { const generateConfig = mode => {

View File

@ -154,7 +154,7 @@ class TabsBar extends React.PureComponent {
const mapStateToProps = state => { const mapStateToProps = state => {
const me = state.get('me'); const me = state.get('me');
const reportsCount = state.getIn(['admin', 'open_report_count']); const reportsCount = state.getIn(['admin', 'openReports']).count();
const approvalCount = state.getIn(['admin', 'awaitingApproval']).count(); const approvalCount = state.getIn(['admin', 'awaitingApproval']).count();
return { return {
account: state.getIn(['accounts', me]), account: state.getIn(['accounts', me]),

View File

@ -3,18 +3,17 @@ import {
Map as ImmutableMap, Map as ImmutableMap,
List as ImmutableList, List as ImmutableList,
OrderedSet as ImmutableOrderedSet, OrderedSet as ImmutableOrderedSet,
fromJS,
} from 'immutable'; } from 'immutable';
describe('admin reducer', () => { describe('admin reducer', () => {
it('should return the initial state', () => { it('should return the initial state', () => {
expect(reducer(undefined, {})).toEqual(fromJS({ expect(reducer(undefined, {})).toEqual({
reports: [], reports: ImmutableMap(),
open_report_count: 0, openReports: ImmutableOrderedSet(),
users: ImmutableMap(), users: ImmutableMap(),
awaitingApproval: ImmutableOrderedSet(), awaitingApproval: ImmutableOrderedSet(),
configs: ImmutableList(), configs: ImmutableList(),
needsReboot: false, needsReboot: false,
})); });
}); });
}); });

View File

@ -15,9 +15,9 @@ import {
} from 'immutable'; } from 'immutable';
const initialState = ImmutableMap({ const initialState = ImmutableMap({
reports: ImmutableList(), reports: ImmutableMap(),
openReports: ImmutableOrderedSet(),
users: ImmutableMap(), users: ImmutableMap(),
open_report_count: 0,
awaitingApproval: ImmutableOrderedSet(), awaitingApproval: ImmutableOrderedSet(),
configs: ImmutableList(), configs: ImmutableList(),
needsReboot: false, needsReboot: false,
@ -52,18 +52,23 @@ function approveUsers(state, users) {
}); });
} }
function importReports(state, reports) {
return state.withMutations(state => {
reports.forEach(report => {
if (report.state === 'open') {
state.update('openReports', orderedSet => orderedSet.add(report.id));
}
state.setIn(['reports', report.id], fromJS(report));
});
});
}
export default function admin(state = initialState, action) { export default function admin(state = initialState, action) {
switch(action.type) { switch(action.type) {
case ADMIN_CONFIG_FETCH_SUCCESS: case ADMIN_CONFIG_FETCH_SUCCESS:
return state.set('configs', fromJS(action.configs)); return state.set('configs', fromJS(action.configs));
case ADMIN_REPORTS_FETCH_SUCCESS: case ADMIN_REPORTS_FETCH_SUCCESS:
if (action.params && action.params.state === 'open') { return importReports(state, action.reports);
return state
.set('reports', fromJS(action.data.reports))
.set('open_report_count', action.data.total);
} else {
return state.set('reports', fromJS(action.data.reports));
}
case ADMIN_USERS_FETCH_SUCCESS: case ADMIN_USERS_FETCH_SUCCESS:
return importUsers(state, action.data.users); return importUsers(state, action.data.users);
case ADMIN_USERS_DELETE_REQUEST: case ADMIN_USERS_DELETE_REQUEST: