1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-06 19:25:17 +02:00

App state: refactored reducers and actions for apps, categories and bookmarks

This commit is contained in:
Paweł Malak 2021-11-09 13:19:53 +01:00
parent 7e89ab0204
commit adc017c48d
10 changed files with 893 additions and 1 deletions

View file

@ -0,0 +1,92 @@
import { ActionType } from '../action-types';
import { Action } from '../actions/index';
import { App } from '../../interfaces';
import { sortData } from '../../utility';
interface AppsState {
loading: boolean;
apps: App[];
errors: string | undefined;
}
const initialState: AppsState = {
loading: true,
apps: [],
errors: undefined,
};
export const appsReducer = (
state: AppsState = initialState,
action: Action
): AppsState => {
switch (action.type) {
case ActionType.getApps:
return {
...state,
loading: true,
errors: undefined,
};
case ActionType.getAppsSuccess:
return {
...state,
loading: false,
apps: action.payload || [],
};
case ActionType.pinApp:
const pinnedAppIdx = state.apps.findIndex(
(app) => app.id === action.payload.id
);
return {
...state,
apps: [
...state.apps.slice(0, pinnedAppIdx),
action.payload,
...state.apps.slice(pinnedAppIdx + 1),
],
};
case ActionType.addAppSuccess:
return {
...state,
apps: [...state.apps, action.payload],
};
case ActionType.deleteApp:
return {
...state,
apps: [...state.apps].filter((app) => app.id !== action.payload),
};
case ActionType.updateApp:
const updatedAppIdx = state.apps.findIndex(
(app) => app.id === action.payload.id
);
return {
...state,
apps: [
...state.apps.slice(0, updatedAppIdx),
action.payload,
...state.apps.slice(updatedAppIdx + 1),
],
};
case ActionType.reorderApps:
return {
...state,
apps: action.payload,
};
case ActionType.sortApps:
return {
...state,
apps: sortData<App>(state.apps, action.payload),
};
default:
return state;
}
};

View file

@ -0,0 +1,166 @@
import { Category } from '../../interfaces';
import { sortData } from '../../utility';
import { ActionType } from '../action-types';
import { Action } from '../actions';
interface BookmarksState {
loading: boolean;
errors: string | undefined;
categories: Category[];
}
const initialState: BookmarksState = {
loading: true,
errors: undefined,
categories: [],
};
export const bookmarksReducer = (
state: BookmarksState = initialState,
action: Action
): BookmarksState => {
switch (action.type) {
case ActionType.getCategories:
return {
...state,
loading: true,
errors: undefined,
};
case ActionType.getCategoriesSuccess:
return {
...state,
loading: false,
categories: action.payload,
};
case ActionType.addCategory:
return {
...state,
categories: [...state.categories, { ...action.payload, bookmarks: [] }],
};
case ActionType.addBookmark:
const categoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.categoryId
);
return {
...state,
categories: [
...state.categories.slice(0, categoryIdx),
{
...state.categories[categoryIdx],
bookmarks: [
...state.categories[categoryIdx].bookmarks,
action.payload,
],
},
...state.categories.slice(categoryIdx + 1),
],
};
case ActionType.pinCategory:
const pinnedCategoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.id
);
return {
...state,
categories: [
...state.categories.slice(0, pinnedCategoryIdx),
action.payload,
...state.categories.slice(pinnedCategoryIdx + 1),
],
};
case ActionType.deleteCategory:
const deletedCategoryIdx = state.categories.findIndex(
(category) => category.id === action.payload
);
return {
...state,
categories: [
...state.categories.slice(0, deletedCategoryIdx),
...state.categories.slice(deletedCategoryIdx + 1),
],
};
case ActionType.updateCategory:
const updatedCategoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.id
);
return {
...state,
categories: [
...state.categories.slice(0, updatedCategoryIdx),
action.payload,
...state.categories.slice(updatedCategoryIdx + 1),
],
};
case ActionType.deleteBookmark:
const categoryInUpdateIdx = state.categories.findIndex(
(category) => category.id === action.payload.categoryId
);
return {
...state,
categories: [
...state.categories.slice(0, categoryInUpdateIdx),
{
...state.categories[categoryInUpdateIdx],
bookmarks: state.categories[categoryInUpdateIdx].bookmarks.filter(
(bookmark) => bookmark.id !== action.payload.bookmarkId
),
},
...state.categories.slice(categoryInUpdateIdx + 1),
],
};
case ActionType.updateBookmark:
const parentCategoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.categoryId
);
const updatedBookmarkIdx = state.categories[
parentCategoryIdx
].bookmarks.findIndex((bookmark) => bookmark.id === action.payload.id);
return {
...state,
categories: [
...state.categories.slice(0, parentCategoryIdx),
{
...state.categories[parentCategoryIdx],
bookmarks: [
...state.categories[parentCategoryIdx].bookmarks.slice(
0,
updatedBookmarkIdx
),
action.payload,
...state.categories[parentCategoryIdx].bookmarks.slice(
updatedBookmarkIdx + 1
),
],
},
...state.categories.slice(parentCategoryIdx + 1),
],
};
case ActionType.sortCategories:
return {
...state,
categories: sortData<Category>(state.categories, action.payload),
};
case ActionType.reorderCategories:
return {
...state,
categories: action.payload,
};
default:
return state;
}
};

View file

@ -3,11 +3,15 @@ import { combineReducers } from 'redux';
import { themeReducer } from './theme';
import { configReducer } from './config';
import { notificationReducer } from './notification';
import { appsReducer } from './app';
import { bookmarksReducer } from './bookmark';
export const reducers = combineReducers({
theme: themeReducer,
config: configReducer,
notification: notificationReducer,
apps: appsReducer,
bookmarks: bookmarksReducer,
});
export type State = ReturnType<typeof reducers>;