bigbuffet-rw/app/soapbox/reducers/admin-log.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-03-15 15:29:48 -07:00
import {
Map as ImmutableMap,
Record as ImmutableRecord,
2021-03-15 15:29:48 -07:00
OrderedSet as ImmutableOrderedSet,
} from 'immutable';
2022-01-10 14:01:24 -08:00
import { ADMIN_LOG_FETCH_SUCCESS } from 'soapbox/actions/admin';
2021-03-15 15:29:48 -07:00
import type { AnyAction } from 'redux';
import type { APIEntity } from 'soapbox/types/entities';
export const LogEntryRecord = ImmutableRecord({
data: ImmutableMap<string, any>(),
id: 0,
message: '',
time: 0,
});
const ReducerRecord = ImmutableRecord({
items: ImmutableMap<string, LogEntry>(),
index: ImmutableOrderedSet<number>(),
2021-03-15 15:29:48 -07:00
total: 0,
});
type LogEntry = ReturnType<typeof LogEntryRecord>;
type State = ReturnType<typeof ReducerRecord>;
type APIEntities = Array<APIEntity>;
const parseItems = (items: APIEntities) => {
const ids: Array<number> = [];
const map: Record<string, LogEntry> = {};
2021-03-15 15:29:48 -07:00
items.forEach(item => {
ids.push(item.id);
map[item.id] = LogEntryRecord(item);
2021-03-15 15:29:48 -07:00
});
return { ids: ids, map: map };
};
const importItems = (state: State, items: APIEntities, total: number) => {
2021-03-15 15:29:48 -07:00
const { ids, map } = parseItems(items);
return state.withMutations(state => {
state.update('index', v => v.union(ids));
state.update('items', v => v.merge(map));
2021-03-15 15:29:48 -07:00
state.set('total', total);
});
};
export default function admin_log(state = ReducerRecord(), action: AnyAction) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case ADMIN_LOG_FETCH_SUCCESS:
return importItems(state, action.items, action.total);
default:
return state;
2021-03-15 15:29:48 -07:00
}
2021-08-03 12:22:51 -07:00
}