forked from pagers-org/Menu-Management-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
65 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { Tcategory, TmenuAction } from '../types/store.js'; | ||
import { Tstate } from '../types/store.js'; | ||
import { createMenu } from '../api/menu.js'; | ||
|
||
/* 액션 타입 정의 */ | ||
const CREATE_MENU = 'CREATE_MENU' as const; | ||
|
@@ -47,24 +48,18 @@ export const setCurrentTab = (categoryId: string) => ({ | |
}); | ||
|
||
// 리듀서는 새로운 상태를 생성하는 함수. | ||
export default function reducer(state: Tstate, action: TmenuAction) { | ||
export default async function reducer(state: Tstate, action: TmenuAction) { | ||
This comment has been minimized.
Sorry, something went wrong.
SINHOLEE
|
||
const { type, payload } = action; | ||
const { categoryId = '', menuId = '', menuName = '' } = payload; | ||
const { menus, categories } = state; | ||
|
||
switch (type) { | ||
case CREATE_MENU: { | ||
const categoryMenus = menus.filter(menu => { | ||
return menu.categoryId === categoryId; | ||
}); | ||
// TODO: 중복 가능성 의심, UUID 적용 | ||
const id = `${categoryId}-menu-id-${categoryMenus.length}`; | ||
const newMenu = { id, categoryId, menuName, inStock: true }; | ||
const newMenuList = [...menus, newMenu]; | ||
return { ...state, menus: newMenuList }; | ||
await createMenu({ category: categoryId, name: menuName }); | ||
break; | ||
} | ||
case EDIT_MENU: { | ||
const newMenuList = menus.map(menu => { | ||
const newMenuList = menus.map((menu) => { | ||
if (menu.id === menuId) { | ||
menu.menuName = menuName; | ||
} | ||
|
@@ -73,11 +68,11 @@ export default function reducer(state: Tstate, action: TmenuAction) { | |
return { ...state, menus: newMenuList }; | ||
} | ||
case REMOVE_MENU: { | ||
const newMenuList = menus.filter(menu => menu.id !== menuId); | ||
const newMenuList = menus.filter((menu) => menu.id !== menuId); | ||
return { ...state, menus: newMenuList }; | ||
} | ||
case SOLD_OUT_MENU: { | ||
const newMenuList = menus.map(menu => { | ||
const newMenuList = menus.map((menu) => { | ||
if (menu.id === menuId) { | ||
menu.inStock = false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
export type Tparams = { | ||
category: string; | ||
category?: string; | ||
menuId?: string; | ||
name?: string; | ||
}; | ||
|
||
export type TmenuResponse = { | ||
menuId: string; | ||
name: string; | ||
isSoldOut: boolean; | ||
}; | ||
|
||
export type TrequestConfig = { | ||
url: string; | ||
method: string; | ||
data?: { name?: string }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,33 @@ | ||
import { SERVER_URL } from '../utils/constants/env.js'; | ||
import { TmenuResponse, TrequestConfig } from '../types/api.js'; | ||
import { Tmenu } from '../types/store.js'; | ||
|
||
type Tconfig = { | ||
url: string; | ||
method: string; | ||
data?: { name?: string }; | ||
}; | ||
|
||
export default async (config: Tconfig) => { | ||
export default async (config: TrequestConfig) => { | ||
const { url, method, data } = config; | ||
|
||
const requestUrl = SERVER_URL + url; | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
try { | ||
const response = await fetch(requestUrl, { | ||
method, | ||
headers, | ||
body: JSON.stringify(data), | ||
}); | ||
|
||
if (response.status !== 200) throw Error('요청 에러'); | ||
if (method !== 'GET') return; | ||
|
||
const response = await fetch(requestUrl, { | ||
method, | ||
body: JSON.stringify(data), | ||
}); | ||
console.log('요청정보? >>>> ', response); | ||
const resData = await response.json(); | ||
const resData = await response.json(); | ||
|
||
if (response.status === 200) { | ||
return resData; | ||
} else { | ||
throw new Error('서버요청 에러!!'); | ||
return resData.map((data: TmenuResponse) => ({ | ||
id: data.menuId, | ||
menuName: data.name, | ||
inStock: !data.isSoldOut, | ||
})) as Tmenu[]; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; |
오우 그냥 아예 순수 ui가 되어버렸네요.