-
Notifications
You must be signed in to change notification settings - Fork 0
Create Week 9 Mission1, 2, 3 #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ง ๊ณ ์ ๋ง์ผ์ จ์ต๋๋ค! ๐๐ป๐๐ป๐๐ป
์ด๋ฒ ์ฃผ ์ํฌ๋ถ์ ํตํด Redux Toolkit
๊ณผ Zustand
๋ ๊ฐ์ง ์ํ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ชจ๋ ๋ค๋ค๋ณด์์ต๋๋ค. ์์ผ๋ก ํ๋ก์ ํธ ๋ณต์ก๋๋ ๊ท๋ชจ์ ๋ฐ๋ผ ์ ์ ํ ์ํ๊ด๋ฆฌ ๋๊ตฌ๋ฅผ ์ ํํ ์ ์๋ ์๋ชฉ์ ๊ฐ์ถ๊ฒ ๋์
จ์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค! ๐๐ป๐๐ป๐๐ป
const initialState: CartState = { | ||
items: cartItems, | ||
totalAmount: cartItems.length, | ||
totalPrice: cartItems.reduce((total, item) => total + Number(item.price), 0), | ||
}; | ||
|
||
const cartSlice = createSlice({ | ||
name: 'cart', | ||
initialState, | ||
reducers: { | ||
increaseAmount: (state, action: PayloadAction<string>) => { | ||
const item = state.items.find(item => item.id === action.payload); | ||
if (item) { | ||
item.amount += 1; | ||
cartSlice.caseReducers.calculateTotals(state); | ||
} | ||
}, | ||
decreaseAmount: (state, action: PayloadAction<string>) => { | ||
const item = state.items.find(item => item.id === action.payload); | ||
if (item && item.amount > 1) { | ||
item.amount -= 1; | ||
cartSlice.caseReducers.calculateTotals(state); | ||
} | ||
}, | ||
removeItem: (state, action: PayloadAction<string>) => { | ||
state.items = state.items.filter(item => item.id !== action.payload); | ||
cartSlice.caseReducers.calculateTotals(state); | ||
}, | ||
clearCart: (state) => { | ||
state.items = []; | ||
state.totalAmount = 0; | ||
state.totalPrice = 0; | ||
}, | ||
calculateTotals: (state) => { | ||
state.totalAmount = state.items.reduce((total, item) => total + item.amount, 0); | ||
state.totalPrice = state.items.reduce((total, item) => total + (Number(item.price) * item.amount), 0); | ||
} | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ cartSlice์์ calculateTotals
๋ฅผ caseReducers
๋ก ํธ์ถํ์ฌ ๊ตฌํํ์
จ๋๋ฐ, ์ด๋ ์ข์ ์ ๊ทผ์ด์ง๋ง initialState์์ ์ดํฉ ๊ณ์ฐ ๋ก์ง์ด ์ค๋ณต๋๊ณ ์์ต๋๋ค.
initialState์์๋ calculateTotals
๋ก์ง์ ์ฌ์ฌ์ฉํ๋๋ก ๋ฆฌํฉํ ๋งํ๋ฉด ์ฝ๋ ์ค๋ณต์ ์ ๊ฑฐํ ์ ์๊ณ , ๊ณ์ฐ ๋ก์ง์ด ํ ๊ณณ์ ์ง์ค๋์ด ์ ์ง๋ณด์์ฑ์ด ํฅ์๋ ๊ฒ ๊ฐ์ต๋๋ค!
import { configureStore } from '@reduxjs/toolkit'; | ||
import cartReducer from '../slice/cartSlice'; | ||
import modalReducer from '../slice/modalSlice'; | ||
|
||
export const store = configureStore({ | ||
reducer: { | ||
cart: cartReducer, | ||
modal: modalReducer, | ||
}, | ||
}); | ||
|
||
export type RootState = ReturnType<typeof store.getState>; | ||
export type AppDispatch = typeof store.dispatch; No newline at end of file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ ์ฅ๋ฐ๊ตฌ๋ ๋ฐ์ดํฐ๊ฐ ์๋ก๊ณ ์นจ ์ ์ด๊ธฐํ๋๊ณ ์๋๋ฐ, Redux Toolkit์ redux-persist๋ Zustand์ persist ๋ฏธ๋ค์จ์ด๋ฅผ ์ ์ฉํ์ฌ, ์๋ก๊ณ ์นจ ํ์๋ ์ฅ๋ฐ๊ตฌ๋ ์ํ๊ฐ ์ ์ง๋๋๋ก ๐Challenge ๋ฏธ์ ์ ๋์ ํด ๋ณด์๋ฉด ์ข๊ฒ ์ต๋๋ค!
React.useEffect(() => { | ||
calculateTotals(); | ||
}, [items, calculateTotals]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ useCartStore์์ ๊ฐ ์ก์
ํจ์๋ค์ด calculateTotals
๋ฅผ ์๋์ผ๋ก ํธ์ถํ์ง ์์ CartPage์์ useEffect
๋ฅผ ํตํด ์๋์ผ๋ก ์ดํฉ์ ๊ณ์ฐํ๊ณ ์์ต๋๋ค. ์ด๋ ์ํ ์
๋ฐ์ดํธ์ ์ดํฉ ๊ณ์ฐ์ด ๋ถ๋ฆฌ๋์ด ์ผ๊ด์ฑ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก, increaseAmount
, decreaseAmount
, removeItem
, clearCart
์ก์
๋ค์ด ์คํ๋ ๋๋ง๋ค ์๋์ผ๋ก calculateTotals
๋ฅผ ํธ์ถํ๋๋ก ์์ ํ๋ฉด ๋์ฑ ์์ ์ ์ธ ์ํ ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ต๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
10์ฃผ์ฐจ ์ํฌ๋ถ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ต๋๋ค.
๊ทธ๋์ ๋ชจ๋ ๊ณ ์ ๋ง์ผ์
จ์ต๋๋ค! ๐๐ป๐๐ป๐๐ป ์งง์ง ์์ ๊ธฐ๊ฐ๋์ ์๋ฏธ ์๋ ์๊ฐ์ด ๋์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
์ด์ ๊ธฐ๋ณธ ์ํฌ๋ถ์ ๋ชจ๋ ์์ฃผํ์ จ์ง๋ง, 11-12์ฃผ์ฐจ ์ํฌ๋ถ๋ ๋์ ํด๋ณด์๊ธธ ์ถ์ฒ๋๋ฆฝ๋๋ค! 11์ฃผ์ฐจ์์๋ Vercel์ ํ์ฉํ ๋ฐฐํฌ์ CI/CD ๊ตฌ์ฑ์ ๋ค๋ฃจ๊ณ , 12์ฃผ์ฐจ์์๋ WebSocket์ ์ด์ฉํ ์ค์๊ฐ ํต์ ๊ณผ Cypress๋ฅผ ํ์ฉํ E2E ํ ์คํธ๋ฅผ ๋ค๋ฃน๋๋ค.
์์ผ๋ก๋ ๊พธ์คํ ํ์ตํ๊ณ ์ฑ์ฅํ์๊ธธ ์์ํ๋ฉฐ, ๋ฐ๋ชจ๋ฐ์ด๊น์ง ํ์ดํ ์ ๋๋ค! ๐๐ป๐๐ป๐๐ป
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ ์ปดํฌ๋ํธ๋ค์ด ์ ์ ํ ๋ถ๋ฆฌ๋์ด ์ฌ์ฌ์ฉ์ฑ์ด ์ข๊ฒ ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. ํนํ MovieFilter, Input, SelectBox, LanguageSelector ๋ฑ์ ์ธ๋ถํ๋ ์ปดํฌ๋ํธ ๋ถ๋ฆฌ๊ฐ ์ธ์์ ์ ๋๋ค!
import HomePage from "./pages/HomePage" | ||
import MovieModal from "./components/MovieModal" | ||
|
||
function App() { | ||
|
||
return ( | ||
<> | ||
<HomePage /> | ||
<MovieModal /> | ||
</> | ||
) | ||
} | ||
|
||
export default App |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ App.tsx์์ MovieModal์ ์ต์์์์ ํญ์ ๋ ๋๋งํ๊ณ ์๋๋ฐ, ์ด๋ ๋ชจ๋ฌ์ด ๋ซํ์์ ๋๋ DOM์ ์กด์ฌํ๋ฉฐ ๋ถํ์ํ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ ๋นํจ์จ์ ์ ๋๋ค.
์๋ฅผ ๋ค์ด, {isOpen && <MovieModal />}
ํํ๋ก ์กฐ๊ฑด๋ถ ๋ ๋๋งํ๋ฉด, ์ค์ ๋ก ๋ชจ๋ฌ์ด ์ด๋ ค์์ ๋๋ง ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ๋์ด ์ฑ๋ฅ์ด ๋ ๊ฐ์ ๋ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค!
๋๋, ๋จ์ผ ์ฑ ์ ์์น์ ๋ฐ๋ผ ๋ชจ๋ฌ์ ์ฌ์ฉํ๋ ๊ณณ์์ ์ง์ ๋ชจ๋ฌ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ ๊ฒ๋ ๊ณ ๋ คํด๋ณผ ์ ์๊ฒ ์ต๋๋ค.
๐ ๋ฏธ์ ๋ฒํธ
9์ฃผ์ฐจ ๋ฏธ์ 1, 2, 3
๐ ๊ตฌํ ์ฌํญ
๐ ์คํฌ๋ฆฐ์ท
bandicam.2025-05-26.14-26-03-925.mp4
โ ์ฒดํฌ๋ฆฌ์คํธ
๐ค ์ง๋ฌธ ์ฌํญ