1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-07 11:55:17 +02:00

Updating categories using form

This commit is contained in:
unknown 2021-05-26 13:13:56 +02:00
parent 0f2125e720
commit 216c12a33c
11 changed files with 176 additions and 30 deletions

View file

@ -13,6 +13,7 @@ import {
AddBookmarkAction,
PinCategoryAction,
DeleteCategoryAction,
UpdateCategoryAction,
// Notifications
CreateNotificationAction,
ClearNotificationAction
@ -38,6 +39,7 @@ export enum ActionTypes {
addBookmark = 'ADD_BOOKMARK',
pinCategory = 'PIN_CATEGORY',
deleteCategory = 'DELETE_CATEGORY',
updateCategory = 'UPDATE_CATEGORY',
// Notifications
createNotification = 'CREATE_NOTIFICATION',
clearNotification = 'CLEAR_NOTIFICATION'
@ -58,6 +60,7 @@ export type Action =
AddBookmarkAction |
PinCategoryAction |
DeleteCategoryAction |
UpdateCategoryAction |
// Notifications
CreateNotificationAction |
ClearNotificationAction;

View file

@ -147,4 +147,33 @@ export const deleteCategory = (id: number) => async (dispatch: Dispatch) => {
} catch (err) {
console.log(err);
}
}
/**
* UPDATE CATEGORY
*/
export interface UpdateCategoryAction {
type: ActionTypes.updateCategory,
payload: Category
}
export const updateCategory = (id: number, formData: NewCategory) => async (dispatch: Dispatch) => {
try {
const res = await axios.put<ApiResponse<Category>>(`/api/categories/${id}`, formData);
dispatch<CreateNotificationAction>({
type: ActionTypes.createNotification,
payload: {
title: 'Success',
message: `Category ${formData.name} updated`
}
})
dispatch<UpdateCategoryAction>({
type: ActionTypes.updateCategory,
payload: res.data.data
})
} catch (err) {
console.log(err);
}
}

View file

@ -78,6 +78,20 @@ const deleteCategory = (state: State, action: Action): State => {
}
}
const updateCategory = (state: State, action: Action): State => {
const tmpCategories = [...state.categories];
const categoryInUpdate = tmpCategories.find((category: Category) => category.id === action.payload.id);
if (categoryInUpdate) {
categoryInUpdate.name = action.payload.name;
}
return {
...state,
categories: tmpCategories
}
}
const bookmarkReducer = (state = initialState, action: Action) => {
switch (action.type) {
case ActionTypes.getCategories: return getCategories(state, action);
@ -86,6 +100,7 @@ const bookmarkReducer = (state = initialState, action: Action) => {
case ActionTypes.addBookmark: return addBookmark(state, action);
case ActionTypes.pinCategory: return pinCategory(state, action);
case ActionTypes.deleteCategory: return deleteCategory(state, action);
case ActionTypes.updateCategory: return updateCategory(state, action);
default: return state;
}
}