mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-07 03:35:18 +02:00
Moved entityInUpdate to app state. It applies for apps, categories and bookmarks
This commit is contained in:
parent
d110d9b732
commit
dfdd49cf4a
15 changed files with 197 additions and 81 deletions
|
@ -7,6 +7,7 @@ import {
|
|||
GetAppsAction,
|
||||
PinAppAction,
|
||||
ReorderAppsAction,
|
||||
SetEditAppAction,
|
||||
SortAppsAction,
|
||||
UpdateAppAction,
|
||||
} from '../actions/app';
|
||||
|
@ -196,3 +197,11 @@ export const sortApps = () => async (dispatch: Dispatch<SortAppsAction>) => {
|
|||
console.log(err);
|
||||
}
|
||||
};
|
||||
|
||||
export const setEditApp =
|
||||
(app: App | null) => (dispatch: Dispatch<SetEditAppAction>) => {
|
||||
dispatch({
|
||||
type: ActionType.setEditApp,
|
||||
payload: app,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -18,6 +18,8 @@ import {
|
|||
GetCategoriesAction,
|
||||
PinCategoryAction,
|
||||
ReorderCategoriesAction,
|
||||
SetEditBookmarkAction,
|
||||
SetEditCategoryAction,
|
||||
SortCategoriesAction,
|
||||
UpdateBookmarkAction,
|
||||
UpdateCategoryAction,
|
||||
|
@ -319,3 +321,21 @@ export const reorderCategories =
|
|||
console.log(err);
|
||||
}
|
||||
};
|
||||
|
||||
export const setEditCategory =
|
||||
(category: Category | null) =>
|
||||
(dispatch: Dispatch<SetEditCategoryAction>) => {
|
||||
dispatch({
|
||||
type: ActionType.setEditCategory,
|
||||
payload: category,
|
||||
});
|
||||
};
|
||||
|
||||
export const setEditBookmark =
|
||||
(bookmark: Bookmark | null) =>
|
||||
(dispatch: Dispatch<SetEditBookmarkAction>) => {
|
||||
dispatch({
|
||||
type: ActionType.setEditBookmark,
|
||||
payload: bookmark,
|
||||
});
|
||||
};
|
||||
|
|
|
@ -23,6 +23,7 @@ export enum ActionType {
|
|||
updateApp = 'UPDATE_APP',
|
||||
reorderApps = 'REORDER_APPS',
|
||||
sortApps = 'SORT_APPS',
|
||||
setEditApp = 'SET_EDIT_APP',
|
||||
// CATEGORES
|
||||
getCategories = 'GET_CATEGORIES',
|
||||
getCategoriesSuccess = 'GET_CATEGORIES_SUCCESS',
|
||||
|
@ -33,10 +34,12 @@ export enum ActionType {
|
|||
updateCategory = 'UPDATE_CATEGORY',
|
||||
sortCategories = 'SORT_CATEGORIES',
|
||||
reorderCategories = 'REORDER_CATEGORIES',
|
||||
setEditCategory = 'SET_EDIT_CATEGORY',
|
||||
// BOOKMARKS
|
||||
addBookmark = 'ADD_BOOKMARK',
|
||||
deleteBookmark = 'DELETE_BOOKMARK',
|
||||
updateBookmark = 'UPDATE_BOOKMARK',
|
||||
setEditBookmark = 'SET_EDIT_BOOKMARK',
|
||||
// AUTH
|
||||
login = 'LOGIN',
|
||||
logout = 'LOGOUT',
|
||||
|
|
|
@ -36,3 +36,8 @@ export interface SortAppsAction {
|
|||
type: ActionType.sortApps;
|
||||
payload: string;
|
||||
}
|
||||
|
||||
export interface SetEditAppAction {
|
||||
type: ActionType.setEditApp;
|
||||
payload: App | null;
|
||||
}
|
||||
|
|
|
@ -56,3 +56,13 @@ export interface ReorderCategoriesAction {
|
|||
type: ActionType.reorderCategories;
|
||||
payload: Category[];
|
||||
}
|
||||
|
||||
export interface SetEditCategoryAction {
|
||||
type: ActionType.setEditCategory;
|
||||
payload: Category | null;
|
||||
}
|
||||
|
||||
export interface SetEditBookmarkAction {
|
||||
type: ActionType.setEditBookmark;
|
||||
payload: Bookmark | null;
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import {
|
|||
UpdateAppAction,
|
||||
ReorderAppsAction,
|
||||
SortAppsAction,
|
||||
SetEditAppAction,
|
||||
} from './app';
|
||||
|
||||
import {
|
||||
|
@ -37,6 +38,8 @@ import {
|
|||
AddBookmarkAction,
|
||||
DeleteBookmarkAction,
|
||||
UpdateBookmarkAction,
|
||||
SetEditCategoryAction,
|
||||
SetEditBookmarkAction,
|
||||
} from './bookmark';
|
||||
|
||||
import {
|
||||
|
@ -67,6 +70,7 @@ export type Action =
|
|||
| UpdateAppAction
|
||||
| ReorderAppsAction
|
||||
| SortAppsAction
|
||||
| SetEditAppAction
|
||||
// Categories
|
||||
| GetCategoriesAction<any>
|
||||
| AddCategoryAction
|
||||
|
@ -75,10 +79,12 @@ export type Action =
|
|||
| UpdateCategoryAction
|
||||
| SortCategoriesAction
|
||||
| ReorderCategoriesAction
|
||||
| SetEditCategoryAction
|
||||
// Bookmarks
|
||||
| AddBookmarkAction
|
||||
| DeleteBookmarkAction
|
||||
| UpdateBookmarkAction
|
||||
| SetEditBookmarkAction
|
||||
// Auth
|
||||
| LoginAction
|
||||
| LogoutAction
|
||||
|
|
|
@ -7,12 +7,14 @@ interface AppsState {
|
|||
loading: boolean;
|
||||
apps: App[];
|
||||
errors: string | undefined;
|
||||
appInUpdate: App | null;
|
||||
}
|
||||
|
||||
const initialState: AppsState = {
|
||||
loading: true,
|
||||
apps: [],
|
||||
errors: undefined,
|
||||
appInUpdate: null,
|
||||
};
|
||||
|
||||
export const appsReducer = (
|
||||
|
@ -86,6 +88,12 @@ export const appsReducer = (
|
|||
apps: sortData<App>(state.apps, action.payload),
|
||||
};
|
||||
|
||||
case ActionType.setEditApp:
|
||||
return {
|
||||
...state,
|
||||
appInUpdate: action.payload,
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Category } from '../../interfaces';
|
||||
import { Bookmark, Category } from '../../interfaces';
|
||||
import { sortData } from '../../utility';
|
||||
import { ActionType } from '../action-types';
|
||||
import { Action } from '../actions';
|
||||
|
@ -7,12 +7,16 @@ interface BookmarksState {
|
|||
loading: boolean;
|
||||
errors: string | undefined;
|
||||
categories: Category[];
|
||||
categoryInEdit: Category | null;
|
||||
bookmarkInEdit: Bookmark | null;
|
||||
}
|
||||
|
||||
const initialState: BookmarksState = {
|
||||
loading: true,
|
||||
errors: undefined,
|
||||
categories: [],
|
||||
categoryInEdit: null,
|
||||
bookmarkInEdit: null,
|
||||
};
|
||||
|
||||
export const bookmarksReducer = (
|
||||
|
@ -45,19 +49,19 @@ export const bookmarksReducer = (
|
|||
(category) => category.id === action.payload.categoryId
|
||||
);
|
||||
|
||||
const categoryWithNewBookmark = {
|
||||
...state.categories[categoryIdx],
|
||||
bookmarks: [...state.categories[categoryIdx].bookmarks, action.payload],
|
||||
};
|
||||
|
||||
return {
|
||||
...state,
|
||||
categories: [
|
||||
...state.categories.slice(0, categoryIdx),
|
||||
{
|
||||
...state.categories[categoryIdx],
|
||||
bookmarks: [
|
||||
...state.categories[categoryIdx].bookmarks,
|
||||
action.payload,
|
||||
],
|
||||
},
|
||||
categoryWithNewBookmark,
|
||||
...state.categories.slice(categoryIdx + 1),
|
||||
],
|
||||
categoryInEdit: categoryWithNewBookmark,
|
||||
};
|
||||
|
||||
case ActionType.pinCategory:
|
||||
|
@ -112,47 +116,54 @@ export const bookmarksReducer = (
|
|||
(category) => category.id === action.payload.categoryId
|
||||
);
|
||||
|
||||
const targetCategory = {
|
||||
...state.categories[categoryInUpdateIdx],
|
||||
bookmarks: state.categories[categoryInUpdateIdx].bookmarks.filter(
|
||||
(bookmark) => bookmark.id !== action.payload.bookmarkId
|
||||
),
|
||||
};
|
||||
|
||||
return {
|
||||
...state,
|
||||
categories: [
|
||||
...state.categories.slice(0, categoryInUpdateIdx),
|
||||
{
|
||||
...state.categories[categoryInUpdateIdx],
|
||||
bookmarks: state.categories[categoryInUpdateIdx].bookmarks.filter(
|
||||
(bookmark) => bookmark.id !== action.payload.bookmarkId
|
||||
),
|
||||
},
|
||||
targetCategory,
|
||||
...state.categories.slice(categoryInUpdateIdx + 1),
|
||||
],
|
||||
categoryInEdit: targetCategory,
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
const categoryWithUpdatedBookmark = {
|
||||
...state.categories[parentCategoryIdx],
|
||||
bookmarks: [
|
||||
...state.categories[parentCategoryIdx].bookmarks.slice(
|
||||
0,
|
||||
updatedBookmarkIdx
|
||||
),
|
||||
action.payload,
|
||||
...state.categories[parentCategoryIdx].bookmarks.slice(
|
||||
updatedBookmarkIdx + 1
|
||||
),
|
||||
],
|
||||
};
|
||||
|
||||
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
|
||||
),
|
||||
],
|
||||
},
|
||||
categoryWithUpdatedBookmark,
|
||||
...state.categories.slice(parentCategoryIdx + 1),
|
||||
],
|
||||
categoryInEdit: categoryWithUpdatedBookmark,
|
||||
};
|
||||
|
||||
case ActionType.sortCategories:
|
||||
|
@ -166,6 +177,19 @@ export const bookmarksReducer = (
|
|||
...state,
|
||||
categories: action.payload,
|
||||
};
|
||||
|
||||
case ActionType.setEditCategory:
|
||||
return {
|
||||
...state,
|
||||
categoryInEdit: action.payload,
|
||||
};
|
||||
|
||||
case ActionType.setEditBookmark:
|
||||
return {
|
||||
...state,
|
||||
bookmarkInEdit: action.payload,
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue