Merge remote-tracking branch 'parent/main' into upstream-20241126
This commit is contained in:
commit
8a075ba4c6
303 changed files with 7495 additions and 4498 deletions
70
app/javascript/mastodon/reducers/bookmark_categories.ts
Normal file
70
app/javascript/mastodon/reducers/bookmark_categories.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
import type { Reducer } from '@reduxjs/toolkit';
|
||||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import {
|
||||
createBookmarkCategory,
|
||||
updateBookmarkCategory,
|
||||
} from 'mastodon/actions/bookmark_categories_typed';
|
||||
import type { ApiBookmarkCategoryJSON } from 'mastodon/api_types/bookmark_categories';
|
||||
import { createBookmarkCategory as createBookmarkCategoryFromJSON } from 'mastodon/models/bookmark_category';
|
||||
import type { BookmarkCategory } from 'mastodon/models/bookmark_category';
|
||||
|
||||
import {
|
||||
BOOKMARK_CATEGORY_FETCH_SUCCESS,
|
||||
BOOKMARK_CATEGORY_FETCH_FAIL,
|
||||
BOOKMARK_CATEGORIES_FETCH_SUCCESS,
|
||||
BOOKMARK_CATEGORY_DELETE_SUCCESS,
|
||||
} from '../actions/bookmark_categories';
|
||||
|
||||
const initialState = ImmutableMap<string, BookmarkCategory | null>();
|
||||
type State = typeof initialState;
|
||||
|
||||
const normalizeBookmarkCategory = (
|
||||
state: State,
|
||||
bookmark_category: ApiBookmarkCategoryJSON,
|
||||
) =>
|
||||
state.set(
|
||||
bookmark_category.id,
|
||||
createBookmarkCategoryFromJSON(bookmark_category),
|
||||
);
|
||||
|
||||
const normalizeBookmarkCategories = (
|
||||
state: State,
|
||||
bookmark_categories: ApiBookmarkCategoryJSON[],
|
||||
) => {
|
||||
bookmark_categories.forEach((bookmark_category) => {
|
||||
state = normalizeBookmarkCategory(state, bookmark_category);
|
||||
});
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
export const bookmarkCategoriesReducer: Reducer<State> = (
|
||||
state = initialState,
|
||||
action,
|
||||
) => {
|
||||
if (
|
||||
createBookmarkCategory.fulfilled.match(action) ||
|
||||
updateBookmarkCategory.fulfilled.match(action)
|
||||
) {
|
||||
return normalizeBookmarkCategory(state, action.payload);
|
||||
} else {
|
||||
switch (action.type) {
|
||||
case BOOKMARK_CATEGORY_FETCH_SUCCESS:
|
||||
return normalizeBookmarkCategory(
|
||||
state,
|
||||
action.bookmarkCategory as ApiBookmarkCategoryJSON,
|
||||
);
|
||||
case BOOKMARK_CATEGORIES_FETCH_SUCCESS:
|
||||
return normalizeBookmarkCategories(
|
||||
state,
|
||||
action.bookmarkCategories as ApiBookmarkCategoryJSON[],
|
||||
);
|
||||
case BOOKMARK_CATEGORY_DELETE_SUCCESS:
|
||||
case BOOKMARK_CATEGORY_FETCH_FAIL:
|
||||
return state.set(action.id as string, null);
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue