Moved entityInUpdate to app state. It applies for apps, categories and bookmarks

This commit is contained in:
Paweł Malak 2021-11-22 14:36:00 +01:00
parent d110d9b732
commit dfdd49cf4a
15 changed files with 197 additions and 81 deletions

View file

@ -4,7 +4,7 @@ import classes from './TableActions.module.css';
interface Entity { interface Entity {
id: number; id: number;
name: string; name: string;
isPinned: boolean; isPinned?: boolean;
isPublic: boolean; isPublic: boolean;
} }
@ -12,7 +12,7 @@ interface Props {
entity: Entity; entity: Entity;
deleteHandler: (id: number, name: string) => void; deleteHandler: (id: number, name: string) => void;
updateHandler: (id: number) => void; updateHandler: (id: number) => void;
pinHanlder: (id: number) => void; pinHanlder?: (id: number) => void;
changeVisibilty: (id: number) => void; changeVisibilty: (id: number) => void;
showPin?: boolean; showPin?: boolean;
} }
@ -27,6 +27,8 @@ export const TableActions = (props: Props): JSX.Element => {
showPin = true, showPin = true,
} = props; } = props;
const _pinHandler = pinHanlder || function () {};
return ( return (
<td className={classes.TableActions}> <td className={classes.TableActions}>
{/* DELETE */} {/* DELETE */}
@ -51,7 +53,7 @@ export const TableActions = (props: Props): JSX.Element => {
{showPin && ( {showPin && (
<div <div
className={classes.TableAction} className={classes.TableAction}
onClick={() => pinHanlder(entity.id)} onClick={() => _pinHandler(entity.id)}
tabIndex={0} tabIndex={0}
> >
{entity.isPinned ? ( {entity.isPinned ? (

View file

@ -1,6 +1,6 @@
import { useState, useEffect, ChangeEvent, SyntheticEvent } from 'react'; import { useState, useEffect, ChangeEvent, SyntheticEvent } from 'react';
import { useDispatch } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { App, NewApp } from '../../../interfaces'; import { NewApp } from '../../../interfaces';
import classes from './AppForm.module.css'; import classes from './AppForm.module.css';
@ -8,29 +8,34 @@ import { ModalForm, InputGroup, Button } from '../../UI';
import { inputHandler, newAppTemplate } from '../../../utility'; import { inputHandler, newAppTemplate } from '../../../utility';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { actionCreators } from '../../../store'; import { actionCreators } from '../../../store';
import { State } from '../../../store/reducers';
interface Props { interface Props {
modalHandler: () => void; modalHandler: () => void;
app?: App;
} }
export const AppForm = ({ app, modalHandler }: Props): JSX.Element => { export const AppForm = ({ modalHandler }: Props): JSX.Element => {
const { appInUpdate } = useSelector((state: State) => state.apps);
const dispatch = useDispatch(); const dispatch = useDispatch();
const { addApp, updateApp } = bindActionCreators(actionCreators, dispatch); const { addApp, updateApp, setEditApp } = bindActionCreators(
actionCreators,
dispatch
);
const [useCustomIcon, toggleUseCustomIcon] = useState<boolean>(false); const [useCustomIcon, toggleUseCustomIcon] = useState<boolean>(false);
const [customIcon, setCustomIcon] = useState<File | null>(null); const [customIcon, setCustomIcon] = useState<File | null>(null);
const [formData, setFormData] = useState<NewApp>(newAppTemplate); const [formData, setFormData] = useState<NewApp>(newAppTemplate);
useEffect(() => { useEffect(() => {
if (app) { if (appInUpdate) {
setFormData({ setFormData({
...app, ...appInUpdate,
}); });
} else { } else {
setFormData(newAppTemplate); setFormData(newAppTemplate);
} }
}, [app]); }, [appInUpdate]);
const inputChangeHandler = ( const inputChangeHandler = (
e: ChangeEvent<HTMLInputElement | HTMLSelectElement>, e: ChangeEvent<HTMLInputElement | HTMLSelectElement>,
@ -66,7 +71,7 @@ export const AppForm = ({ app, modalHandler }: Props): JSX.Element => {
return data; return data;
}; };
if (!app) { if (!appInUpdate) {
if (customIcon) { if (customIcon) {
const data = createFormData(); const data = createFormData();
addApp(data); addApp(data);
@ -76,14 +81,15 @@ export const AppForm = ({ app, modalHandler }: Props): JSX.Element => {
} else { } else {
if (customIcon) { if (customIcon) {
const data = createFormData(); const data = createFormData();
updateApp(app.id, data); updateApp(appInUpdate.id, data);
} else { } else {
updateApp(app.id, formData); updateApp(appInUpdate.id, formData);
modalHandler(); modalHandler();
} }
} }
setFormData(newAppTemplate); setFormData(newAppTemplate);
setEditApp(null);
}; };
return ( return (
@ -182,7 +188,7 @@ export const AppForm = ({ app, modalHandler }: Props): JSX.Element => {
</select> </select>
</InputGroup> </InputGroup>
{!app ? ( {!appInUpdate ? (
<Button>Add new application</Button> <Button>Add new application</Button>
) : ( ) : (
<Button>Update application</Button> <Button>Update application</Button>

View file

@ -19,7 +19,6 @@ import { AppForm } from './AppForm/AppForm';
import { AppTable } from './AppTable/AppTable'; import { AppTable } from './AppTable/AppTable';
// Utils // Utils
import { appTemplate } from '../../utility';
import { State } from '../../store/reducers'; import { State } from '../../store/reducers';
import { bindActionCreators } from 'redux'; import { bindActionCreators } from 'redux';
import { actionCreators } from '../../store'; import { actionCreators } from '../../store';
@ -37,7 +36,7 @@ export const Apps = (props: Props): JSX.Element => {
// Get Redux action creators // Get Redux action creators
const dispatch = useDispatch(); const dispatch = useDispatch();
const { getApps } = bindActionCreators(actionCreators, dispatch); const { getApps, setEditApp } = bindActionCreators(actionCreators, dispatch);
// Load apps if array is empty // Load apps if array is empty
useEffect(() => { useEffect(() => {
@ -49,8 +48,6 @@ export const Apps = (props: Props): JSX.Element => {
// Form // Form
const [modalIsOpen, setModalIsOpen] = useState(false); const [modalIsOpen, setModalIsOpen] = useState(false);
const [showTable, setShowTable] = useState(false); const [showTable, setShowTable] = useState(false);
const [isInUpdate, setIsInUpdate] = useState(false);
const [appInUpdate, setAppInUpdate] = useState<App>(appTemplate);
// Observe if user is authenticated -> set default view if not // Observe if user is authenticated -> set default view if not
useEffect(() => { useEffect(() => {
@ -63,28 +60,21 @@ export const Apps = (props: Props): JSX.Element => {
// Form actions // Form actions
const toggleModal = (): void => { const toggleModal = (): void => {
setModalIsOpen(!modalIsOpen); setModalIsOpen(!modalIsOpen);
setIsInUpdate(false);
}; };
const toggleEdit = (): void => { const toggleEdit = (): void => {
setShowTable(!showTable); setShowTable(!showTable);
setIsInUpdate(false);
}; };
const openFormForUpdating = (app: App): void => { const openFormForUpdating = (app: App): void => {
setAppInUpdate(app); setEditApp(app);
setIsInUpdate(true);
setModalIsOpen(true); setModalIsOpen(true);
}; };
return ( return (
<Container> <Container>
<Modal isOpen={modalIsOpen} setIsOpen={setModalIsOpen}> <Modal isOpen={modalIsOpen} setIsOpen={setModalIsOpen}>
{!isInUpdate ? ( <AppForm modalHandler={toggleModal} />
<AppForm modalHandler={toggleModal} />
) : (
<AppForm modalHandler={toggleModal} app={appInUpdate} />
)}
</Modal> </Modal>
<Headline <Headline
@ -94,7 +84,14 @@ export const Apps = (props: Props): JSX.Element => {
{isAuthenticated && ( {isAuthenticated && (
<div className={classes.ActionsContainer}> <div className={classes.ActionsContainer}>
<ActionButton name="Add" icon="mdiPlusBox" handler={toggleModal} /> <ActionButton
name="Add"
icon="mdiPlusBox"
handler={() => {
setEditApp(null);
toggleModal();
}}
/>
<ActionButton name="Edit" icon="mdiPencil" handler={toggleEdit} /> <ActionButton name="Edit" icon="mdiPencil" handler={toggleEdit} />
</div> </div>
)} )}

View file

@ -10,6 +10,10 @@
text-transform: uppercase; text-transform: uppercase;
} }
.BookmarkCard h3:hover {
cursor: pointer;
}
.Bookmarks { .Bookmarks {
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View file

@ -1,14 +1,17 @@
import { Fragment } from 'react'; import { Fragment } from 'react';
import { useSelector } from 'react-redux'; // Redux
import { useDispatch, useSelector } from 'react-redux';
import { State } from '../../../store/reducers'; import { State } from '../../../store/reducers';
import { bindActionCreators } from 'redux';
import { actionCreators } from '../../../store';
// Typescript
import { Bookmark, Category } from '../../../interfaces'; import { Bookmark, Category } from '../../../interfaces';
// Other
import classes from './BookmarkCard.module.css'; import classes from './BookmarkCard.module.css';
import { Icon } from '../../UI'; import { Icon } from '../../UI';
import { iconParser, isImage, isSvg, isUrl, urlParser } from '../../../utility'; import { iconParser, isImage, isSvg, isUrl, urlParser } from '../../../utility';
interface Props { interface Props {
@ -18,9 +21,19 @@ interface Props {
export const BookmarkCard = (props: Props): JSX.Element => { export const BookmarkCard = (props: Props): JSX.Element => {
const { config } = useSelector((state: State) => state.config); const { config } = useSelector((state: State) => state.config);
const dispatch = useDispatch();
const { setEditCategory } = bindActionCreators(actionCreators, dispatch);
return ( return (
<div className={classes.BookmarkCard}> <div className={classes.BookmarkCard}>
<h3>{props.category.name}</h3> <h3
onClick={() => {
setEditCategory(props.category);
}}
>
{props.category.name}
</h3>
<div className={classes.Bookmarks}> <div className={classes.Bookmarks}>
{props.category.bookmarks.map((bookmark: Bookmark) => { {props.category.bookmarks.map((bookmark: Bookmark) => {
const redirectUrl = urlParser(bookmark.url)[1]; const redirectUrl = urlParser(bookmark.url)[1];

View file

@ -19,9 +19,6 @@ import { Container, Headline, ActionButton, Spinner, Modal } from '../UI';
// Components // Components
import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid'; import { BookmarkGrid } from './BookmarkGrid/BookmarkGrid';
import { Form } from './Form/Form'; import { Form } from './Form/Form';
// Utils
import { bookmarkTemplate, categoryTemplate } from '../../utility';
import { Table } from './Table/Table'; import { Table } from './Table/Table';
interface Props { interface Props {
@ -36,13 +33,14 @@ export enum ContentType {
export const Bookmarks = (props: Props): JSX.Element => { export const Bookmarks = (props: Props): JSX.Element => {
// Get Redux state // Get Redux state
const { const {
bookmarks: { loading, categories }, bookmarks: { loading, categories, categoryInEdit },
auth: { isAuthenticated }, auth: { isAuthenticated },
} = useSelector((state: State) => state); } = useSelector((state: State) => state);
// Get Redux action creators // Get Redux action creators
const dispatch = useDispatch(); const dispatch = useDispatch();
const { getCategories } = bindActionCreators(actionCreators, dispatch); const { getCategories, setEditCategory, setEditBookmark } =
bindActionCreators(actionCreators, dispatch);
// Load categories if array is empty // Load categories if array is empty
useEffect(() => { useEffect(() => {
@ -55,10 +53,6 @@ export const Bookmarks = (props: Props): JSX.Element => {
const [modalIsOpen, setModalIsOpen] = useState(false); const [modalIsOpen, setModalIsOpen] = useState(false);
const [formContentType, setFormContentType] = useState(ContentType.category); const [formContentType, setFormContentType] = useState(ContentType.category);
const [isInUpdate, setIsInUpdate] = useState(false); const [isInUpdate, setIsInUpdate] = useState(false);
const [categoryInUpdate, setCategoryInUpdate] =
useState<Category>(categoryTemplate);
const [bookmarkInUpdate, setBookmarkInUpdate] =
useState<Bookmark>(bookmarkTemplate);
// Table // Table
const [showTable, setShowTable] = useState(false); const [showTable, setShowTable] = useState(false);
@ -74,6 +68,13 @@ export const Bookmarks = (props: Props): JSX.Element => {
} }
}, [isAuthenticated]); }, [isAuthenticated]);
useEffect(() => {
if (categoryInEdit && !showTable) {
setTableContentType(ContentType.bookmark);
setShowTable(true);
}
}, [categoryInEdit]);
// Form actions // Form actions
const toggleModal = (): void => { const toggleModal = (): void => {
setModalIsOpen(!modalIsOpen); setModalIsOpen(!modalIsOpen);
@ -94,10 +95,10 @@ export const Bookmarks = (props: Props): JSX.Element => {
if (instanceOfCategory(data)) { if (instanceOfCategory(data)) {
setFormContentType(ContentType.category); setFormContentType(ContentType.category);
setCategoryInUpdate(data); setEditCategory(data);
} else { } else {
setFormContentType(ContentType.bookmark); setFormContentType(ContentType.bookmark);
setBookmarkInUpdate(data); setEditBookmark(data);
} }
toggleModal(); toggleModal();
@ -121,8 +122,6 @@ export const Bookmarks = (props: Props): JSX.Element => {
modalHandler={toggleModal} modalHandler={toggleModal}
contentType={formContentType} contentType={formContentType}
inUpdate={isInUpdate} inUpdate={isInUpdate}
category={categoryInUpdate}
bookmark={bookmarkInUpdate}
/> />
</Modal> </Modal>
@ -145,11 +144,11 @@ export const Bookmarks = (props: Props): JSX.Element => {
icon="mdiPencil" icon="mdiPencil"
handler={() => showTableForEditing(ContentType.category)} handler={() => showTableForEditing(ContentType.category)}
/> />
{/* <ActionButton <ActionButton
name="Edit Bookmarks" name="Edit Bookmarks"
icon="mdiPencil" icon="mdiPencil"
handler={() => showTableForEditing(ContentType.bookmark)} handler={() => showTableForEditing(ContentType.bookmark)}
/> */} />
</div> </div>
)} )}

View file

@ -1,22 +1,26 @@
// Typescript // Typescript
import { Bookmark, Category } from '../../../interfaces';
import { ContentType } from '../Bookmarks'; import { ContentType } from '../Bookmarks';
// Utils // Utils
import { CategoryForm } from './CategoryForm'; import { CategoryForm } from './CategoryForm';
import { BookmarksForm } from './BookmarksForm'; import { BookmarksForm } from './BookmarksForm';
import { Fragment } from 'react'; import { Fragment } from 'react';
import { useSelector } from 'react-redux';
import { State } from '../../../store/reducers';
import { bookmarkTemplate, categoryTemplate } from '../../../utility';
interface Props { interface Props {
modalHandler: () => void; modalHandler: () => void;
contentType: ContentType; contentType: ContentType;
inUpdate?: boolean; inUpdate?: boolean;
category?: Category;
bookmark?: Bookmark;
} }
export const Form = (props: Props): JSX.Element => { export const Form = (props: Props): JSX.Element => {
const { modalHandler, contentType, inUpdate, category, bookmark } = props; const { categoryInEdit, bookmarkInEdit } = useSelector(
(state: State) => state.bookmarks
);
const { modalHandler, contentType, inUpdate } = props;
return ( return (
<Fragment> <Fragment>
@ -33,9 +37,15 @@ export const Form = (props: Props): JSX.Element => {
// form: update // form: update
<Fragment> <Fragment>
{contentType === ContentType.category ? ( {contentType === ContentType.category ? (
<CategoryForm modalHandler={modalHandler} category={category} /> <CategoryForm
modalHandler={modalHandler}
category={categoryInEdit || categoryTemplate}
/>
) : ( ) : (
<BookmarksForm modalHandler={modalHandler} bookmark={bookmark} /> <BookmarksForm
modalHandler={modalHandler}
bookmark={bookmarkInEdit || bookmarkTemplate}
/>
)} )}
</Fragment> </Fragment>
)} )}

View file

@ -7,6 +7,7 @@ import {
GetAppsAction, GetAppsAction,
PinAppAction, PinAppAction,
ReorderAppsAction, ReorderAppsAction,
SetEditAppAction,
SortAppsAction, SortAppsAction,
UpdateAppAction, UpdateAppAction,
} from '../actions/app'; } from '../actions/app';
@ -196,3 +197,11 @@ export const sortApps = () => async (dispatch: Dispatch<SortAppsAction>) => {
console.log(err); console.log(err);
} }
}; };
export const setEditApp =
(app: App | null) => (dispatch: Dispatch<SetEditAppAction>) => {
dispatch({
type: ActionType.setEditApp,
payload: app,
});
};

View file

@ -18,6 +18,8 @@ import {
GetCategoriesAction, GetCategoriesAction,
PinCategoryAction, PinCategoryAction,
ReorderCategoriesAction, ReorderCategoriesAction,
SetEditBookmarkAction,
SetEditCategoryAction,
SortCategoriesAction, SortCategoriesAction,
UpdateBookmarkAction, UpdateBookmarkAction,
UpdateCategoryAction, UpdateCategoryAction,
@ -319,3 +321,21 @@ export const reorderCategories =
console.log(err); 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,
});
};

View file

@ -23,6 +23,7 @@ export enum ActionType {
updateApp = 'UPDATE_APP', updateApp = 'UPDATE_APP',
reorderApps = 'REORDER_APPS', reorderApps = 'REORDER_APPS',
sortApps = 'SORT_APPS', sortApps = 'SORT_APPS',
setEditApp = 'SET_EDIT_APP',
// CATEGORES // CATEGORES
getCategories = 'GET_CATEGORIES', getCategories = 'GET_CATEGORIES',
getCategoriesSuccess = 'GET_CATEGORIES_SUCCESS', getCategoriesSuccess = 'GET_CATEGORIES_SUCCESS',
@ -33,10 +34,12 @@ export enum ActionType {
updateCategory = 'UPDATE_CATEGORY', updateCategory = 'UPDATE_CATEGORY',
sortCategories = 'SORT_CATEGORIES', sortCategories = 'SORT_CATEGORIES',
reorderCategories = 'REORDER_CATEGORIES', reorderCategories = 'REORDER_CATEGORIES',
setEditCategory = 'SET_EDIT_CATEGORY',
// BOOKMARKS // BOOKMARKS
addBookmark = 'ADD_BOOKMARK', addBookmark = 'ADD_BOOKMARK',
deleteBookmark = 'DELETE_BOOKMARK', deleteBookmark = 'DELETE_BOOKMARK',
updateBookmark = 'UPDATE_BOOKMARK', updateBookmark = 'UPDATE_BOOKMARK',
setEditBookmark = 'SET_EDIT_BOOKMARK',
// AUTH // AUTH
login = 'LOGIN', login = 'LOGIN',
logout = 'LOGOUT', logout = 'LOGOUT',

View file

@ -36,3 +36,8 @@ export interface SortAppsAction {
type: ActionType.sortApps; type: ActionType.sortApps;
payload: string; payload: string;
} }
export interface SetEditAppAction {
type: ActionType.setEditApp;
payload: App | null;
}

View file

@ -56,3 +56,13 @@ export interface ReorderCategoriesAction {
type: ActionType.reorderCategories; type: ActionType.reorderCategories;
payload: Category[]; payload: Category[];
} }
export interface SetEditCategoryAction {
type: ActionType.setEditCategory;
payload: Category | null;
}
export interface SetEditBookmarkAction {
type: ActionType.setEditBookmark;
payload: Bookmark | null;
}

View file

@ -24,6 +24,7 @@ import {
UpdateAppAction, UpdateAppAction,
ReorderAppsAction, ReorderAppsAction,
SortAppsAction, SortAppsAction,
SetEditAppAction,
} from './app'; } from './app';
import { import {
@ -37,6 +38,8 @@ import {
AddBookmarkAction, AddBookmarkAction,
DeleteBookmarkAction, DeleteBookmarkAction,
UpdateBookmarkAction, UpdateBookmarkAction,
SetEditCategoryAction,
SetEditBookmarkAction,
} from './bookmark'; } from './bookmark';
import { import {
@ -67,6 +70,7 @@ export type Action =
| UpdateAppAction | UpdateAppAction
| ReorderAppsAction | ReorderAppsAction
| SortAppsAction | SortAppsAction
| SetEditAppAction
// Categories // Categories
| GetCategoriesAction<any> | GetCategoriesAction<any>
| AddCategoryAction | AddCategoryAction
@ -75,10 +79,12 @@ export type Action =
| UpdateCategoryAction | UpdateCategoryAction
| SortCategoriesAction | SortCategoriesAction
| ReorderCategoriesAction | ReorderCategoriesAction
| SetEditCategoryAction
// Bookmarks // Bookmarks
| AddBookmarkAction | AddBookmarkAction
| DeleteBookmarkAction | DeleteBookmarkAction
| UpdateBookmarkAction | UpdateBookmarkAction
| SetEditBookmarkAction
// Auth // Auth
| LoginAction | LoginAction
| LogoutAction | LogoutAction

View file

@ -7,12 +7,14 @@ interface AppsState {
loading: boolean; loading: boolean;
apps: App[]; apps: App[];
errors: string | undefined; errors: string | undefined;
appInUpdate: App | null;
} }
const initialState: AppsState = { const initialState: AppsState = {
loading: true, loading: true,
apps: [], apps: [],
errors: undefined, errors: undefined,
appInUpdate: null,
}; };
export const appsReducer = ( export const appsReducer = (
@ -86,6 +88,12 @@ export const appsReducer = (
apps: sortData<App>(state.apps, action.payload), apps: sortData<App>(state.apps, action.payload),
}; };
case ActionType.setEditApp:
return {
...state,
appInUpdate: action.payload,
};
default: default:
return state; return state;
} }

View file

@ -1,4 +1,4 @@
import { Category } from '../../interfaces'; import { Bookmark, Category } from '../../interfaces';
import { sortData } from '../../utility'; import { sortData } from '../../utility';
import { ActionType } from '../action-types'; import { ActionType } from '../action-types';
import { Action } from '../actions'; import { Action } from '../actions';
@ -7,12 +7,16 @@ interface BookmarksState {
loading: boolean; loading: boolean;
errors: string | undefined; errors: string | undefined;
categories: Category[]; categories: Category[];
categoryInEdit: Category | null;
bookmarkInEdit: Bookmark | null;
} }
const initialState: BookmarksState = { const initialState: BookmarksState = {
loading: true, loading: true,
errors: undefined, errors: undefined,
categories: [], categories: [],
categoryInEdit: null,
bookmarkInEdit: null,
}; };
export const bookmarksReducer = ( export const bookmarksReducer = (
@ -45,19 +49,19 @@ export const bookmarksReducer = (
(category) => category.id === action.payload.categoryId (category) => category.id === action.payload.categoryId
); );
const categoryWithNewBookmark = {
...state.categories[categoryIdx],
bookmarks: [...state.categories[categoryIdx].bookmarks, action.payload],
};
return { return {
...state, ...state,
categories: [ categories: [
...state.categories.slice(0, categoryIdx), ...state.categories.slice(0, categoryIdx),
{ categoryWithNewBookmark,
...state.categories[categoryIdx],
bookmarks: [
...state.categories[categoryIdx].bookmarks,
action.payload,
],
},
...state.categories.slice(categoryIdx + 1), ...state.categories.slice(categoryIdx + 1),
], ],
categoryInEdit: categoryWithNewBookmark,
}; };
case ActionType.pinCategory: case ActionType.pinCategory:
@ -112,47 +116,54 @@ export const bookmarksReducer = (
(category) => category.id === action.payload.categoryId (category) => category.id === action.payload.categoryId
); );
const targetCategory = {
...state.categories[categoryInUpdateIdx],
bookmarks: state.categories[categoryInUpdateIdx].bookmarks.filter(
(bookmark) => bookmark.id !== action.payload.bookmarkId
),
};
return { return {
...state, ...state,
categories: [ categories: [
...state.categories.slice(0, categoryInUpdateIdx), ...state.categories.slice(0, categoryInUpdateIdx),
{ targetCategory,
...state.categories[categoryInUpdateIdx],
bookmarks: state.categories[categoryInUpdateIdx].bookmarks.filter(
(bookmark) => bookmark.id !== action.payload.bookmarkId
),
},
...state.categories.slice(categoryInUpdateIdx + 1), ...state.categories.slice(categoryInUpdateIdx + 1),
], ],
categoryInEdit: targetCategory,
}; };
case ActionType.updateBookmark: case ActionType.updateBookmark:
const parentCategoryIdx = state.categories.findIndex( const parentCategoryIdx = state.categories.findIndex(
(category) => category.id === action.payload.categoryId (category) => category.id === action.payload.categoryId
); );
const updatedBookmarkIdx = state.categories[ const updatedBookmarkIdx = state.categories[
parentCategoryIdx parentCategoryIdx
].bookmarks.findIndex((bookmark) => bookmark.id === action.payload.id); ].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 { return {
...state, ...state,
categories: [ categories: [
...state.categories.slice(0, parentCategoryIdx), ...state.categories.slice(0, parentCategoryIdx),
{ categoryWithUpdatedBookmark,
...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), ...state.categories.slice(parentCategoryIdx + 1),
], ],
categoryInEdit: categoryWithUpdatedBookmark,
}; };
case ActionType.sortCategories: case ActionType.sortCategories:
@ -166,6 +177,19 @@ export const bookmarksReducer = (
...state, ...state,
categories: action.payload, categories: action.payload,
}; };
case ActionType.setEditCategory:
return {
...state,
categoryInEdit: action.payload,
};
case ActionType.setEditBookmark:
return {
...state,
bookmarkInEdit: action.payload,
};
default: default:
return state; return state;
} }