bigbuffet-rw/app/soapbox/reducers/reports.ts

96 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Record as ImmutableRecord, Set as ImmutableSet } from 'immutable';
2022-09-29 06:45:57 -07:00
import { ChatMessage } from 'soapbox/types/entities';
2020-03-27 13:59:38 -07:00
import {
REPORT_INIT,
REPORT_SUBMIT_REQUEST,
REPORT_SUBMIT_SUCCESS,
REPORT_SUBMIT_FAIL,
REPORT_CANCEL,
REPORT_STATUS_TOGGLE,
REPORT_COMMENT_CHANGE,
REPORT_FORWARD_CHANGE,
REPORT_BLOCK_CHANGE,
2022-04-27 07:11:21 -07:00
REPORT_RULE_CHANGE,
2020-03-27 13:59:38 -07:00
} from '../actions/reports';
import type { AnyAction } from 'redux';
const NewReportRecord = ImmutableRecord({
isSubmitting: false,
account_id: null as string | null,
status_ids: ImmutableSet<string>(),
2022-09-29 06:45:57 -07:00
chat_message: null as null | ChatMessage,
comment: '',
forward: false,
block: false,
rule_ids: ImmutableSet<string>(),
});
const ReducerRecord = ImmutableRecord({
new: NewReportRecord(),
2020-03-27 13:59:38 -07:00
});
type State = ReturnType<typeof ReducerRecord>;
export default function reports(state: State = ReducerRecord(), action: AnyAction) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case REPORT_INIT:
return state.withMutations(map => {
map.setIn(['new', 'isSubmitting'], false);
map.setIn(['new', 'account_id'], action.account.id);
2020-03-27 13:59:38 -07:00
2022-09-29 06:45:57 -07:00
if (action.chatMessage) {
map.setIn(['new', 'chat_message'], action.chatMessage);
}
if (state.new.account_id !== action.account.id) {
map.setIn(['new', 'status_ids'], action.status ? ImmutableSet([action.status.reblog?.id || action.status.id]) : ImmutableSet());
2022-05-11 14:06:35 -07:00
map.setIn(['new', 'comment'], '');
} else if (action.status) {
map.updateIn(['new', 'status_ids'], set => (set as ImmutableSet<string>).add(action.status.reblog?.id || action.status.id));
2022-05-11 14:06:35 -07:00
}
});
case REPORT_STATUS_TOGGLE:
return state.updateIn(['new', 'status_ids'], set => {
2022-05-11 14:06:35 -07:00
if (action.checked) {
return (set as ImmutableSet<string>).add(action.statusId);
2022-05-11 14:06:35 -07:00
}
2020-03-27 13:59:38 -07:00
return (set as ImmutableSet<string>).remove(action.statusId);
2022-05-11 14:06:35 -07:00
});
case REPORT_COMMENT_CHANGE:
return state.setIn(['new', 'comment'], action.comment);
case REPORT_FORWARD_CHANGE:
return state.setIn(['new', 'forward'], action.forward);
case REPORT_BLOCK_CHANGE:
return state.setIn(['new', 'block'], action.block);
case REPORT_RULE_CHANGE:
return state.updateIn(['new', 'rule_ids'], (set) => {
if ((set as ImmutableSet<string>).includes(action.rule_id)) {
return (set as ImmutableSet<string>).remove(action.rule_id);
2022-05-11 14:06:35 -07:00
}
2022-05-03 05:13:26 -07:00
return (set as ImmutableSet<string>).add(action.rule_id);
2022-05-11 14:06:35 -07:00
});
case REPORT_SUBMIT_REQUEST:
return state.setIn(['new', 'isSubmitting'], true);
case REPORT_SUBMIT_FAIL:
return state.setIn(['new', 'isSubmitting'], false);
case REPORT_CANCEL:
case REPORT_SUBMIT_SUCCESS:
return state.withMutations(map => {
map.setIn(['new', 'account_id'], null);
map.setIn(['new', 'status_ids'], ImmutableSet());
2022-09-29 06:45:57 -07:00
map.setIn(['new', 'chat_message'], null);
2022-05-11 14:06:35 -07:00
map.setIn(['new', 'comment'], '');
map.setIn(['new', 'isSubmitting'], false);
map.setIn(['new', 'rule_ids'], ImmutableSet());
map.setIn(['new', 'block'], false);
});
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}