mirror of
https://github.com/glitch-soc/mastodon.git
synced 2025-01-31 20:12:55 -05:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import type { Reducer } from '@reduxjs/toolkit';
|
|
import { Map as ImmutableMap } from 'immutable';
|
|
|
|
import { createList, updateList } from 'mastodon/actions/lists_typed';
|
|
import type { ApiListJSON } from 'mastodon/api_types/lists';
|
|
import { createList as createListFromJSON } from 'mastodon/models/list';
|
|
import type { List } from 'mastodon/models/list';
|
|
|
|
import {
|
|
LIST_FETCH_SUCCESS,
|
|
LIST_FETCH_FAIL,
|
|
LISTS_FETCH_SUCCESS,
|
|
LIST_DELETE_SUCCESS,
|
|
} from '../actions/lists';
|
|
|
|
const initialState = ImmutableMap<string, List | null>();
|
|
type State = typeof initialState;
|
|
|
|
const normalizeList = (state: State, list: ApiListJSON) =>
|
|
state.set(list.id, createListFromJSON(list));
|
|
|
|
const normalizeLists = (state: State, lists: ApiListJSON[]) => {
|
|
lists.forEach((list) => {
|
|
state = normalizeList(state, list);
|
|
});
|
|
|
|
return state;
|
|
};
|
|
|
|
export const listsReducer: Reducer<State> = (state = initialState, action) => {
|
|
if (
|
|
createList.fulfilled.match(action) ||
|
|
updateList.fulfilled.match(action)
|
|
) {
|
|
return normalizeList(state, action.payload);
|
|
} else {
|
|
switch (action.type) {
|
|
case LIST_FETCH_SUCCESS:
|
|
return normalizeList(state, action.list as ApiListJSON);
|
|
case LISTS_FETCH_SUCCESS:
|
|
return normalizeLists(state, action.lists as ApiListJSON[]);
|
|
case LIST_DELETE_SUCCESS:
|
|
case LIST_FETCH_FAIL:
|
|
return state.set(action.id as string, null);
|
|
default:
|
|
return state;
|
|
}
|
|
}
|
|
};
|