bigbuffet-rw/app/soapbox/reducers/trends.ts
marcin mikołajczak a66c174c2d Add normalizers, fix tests
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2022-06-08 23:43:27 +02:00

35 lines
1,006 B
TypeScript

import { List as ImmutableList, Record as ImmutableRecord } from 'immutable';
import { normalizeTag } from 'soapbox/normalizers';
import {
TRENDS_FETCH_REQUEST,
TRENDS_FETCH_SUCCESS,
TRENDS_FETCH_FAIL,
} from '../actions/trends';
import type { AnyAction } from 'redux';
import type { APIEntity, Tag } from 'soapbox/types/entities';
const ReducerRecord = ImmutableRecord({
items: ImmutableList<Tag>(),
isLoading: false,
});
type State = ReturnType<typeof ReducerRecord>;
export default function trendsReducer(state: State = ReducerRecord(), action: AnyAction) {
switch (action.type) {
case TRENDS_FETCH_REQUEST:
return state.set('isLoading', true);
case TRENDS_FETCH_SUCCESS:
return state.withMutations(map => {
map.set('items', ImmutableList(action.tags.map((item: APIEntity) => normalizeTag(item))));
map.set('isLoading', false);
});
case TRENDS_FETCH_FAIL:
return state.set('isLoading', false);
default:
return state;
}
}