import { Map as ImmutableMap, Record as ImmutableRecord, OrderedSet as ImmutableOrderedSet, } from 'immutable'; import { ADMIN_LOG_FETCH_SUCCESS } from 'soapbox/actions/admin'; import type { AnyAction } from 'redux'; const LogEntryRecord = ImmutableRecord({ data: ImmutableMap(), id: 0, message: '', time: 0, }); const ReducerRecord = ImmutableRecord({ items: ImmutableMap(), index: ImmutableOrderedSet(), total: 0, }); type LogEntry = ReturnType; type State = ReturnType; type APIEntity = Record; type APIEntities = Array; const parseItems = (items: APIEntities) => { const ids: Array = []; const map: Record = {}; items.forEach(item => { ids.push(item.id); map[item.id] = LogEntryRecord(item); }); return { ids: ids, map: map }; }; const importItems = (state: State, items: APIEntities, total: number) => { const { ids, map } = parseItems(items); return state.withMutations(state => { state.update('index', v => v.union(ids)); state.update('items', v => v.merge(map)); state.set('total', total); }); }; export default function admin_log(state = ReducerRecord(), action: AnyAction) { switch (action.type) { case ADMIN_LOG_FETCH_SUCCESS: return importItems(state, action.items, action.total); default: return state; } }