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 });
return api(getState)
.get('/api/pleroma/admin/reports', { params })
.then(({ data }) => {
dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, data, params });
.then(({ data: { reports } }) => {
dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports, params });
}).catch(error => {
dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params });
});

View File

@ -6,7 +6,7 @@ import { Helmet } from'react-helmet';
const getNotifTotals = state => {
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 reports = state.getIn(['admin', 'open_report_count'], 0);
const reports = state.getIn(['admin', 'openReports']).count();
const approvals = state.getIn(['admin', 'awaitingApproval']).count();
return notifications + chats + reports + approvals;
};

View File

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

View File

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

View File

@ -154,7 +154,7 @@ class TabsBar extends React.PureComponent {
const mapStateToProps = state => {
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();
return {
account: state.getIn(['accounts', me]),

View File

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

View File

@ -15,9 +15,9 @@ import {
} from 'immutable';
const initialState = ImmutableMap({
reports: ImmutableList(),
reports: ImmutableMap(),
openReports: ImmutableOrderedSet(),
users: ImmutableMap(),
open_report_count: 0,
awaitingApproval: ImmutableOrderedSet(),
configs: ImmutableList(),
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) {
switch(action.type) {
case ADMIN_CONFIG_FETCH_SUCCESS:
return state.set('configs', fromJS(action.configs));
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));
}
return importReports(state, action.reports);
case ADMIN_USERS_FETCH_SUCCESS:
return importUsers(state, action.data.users);
case ADMIN_USERS_DELETE_REQUEST: