-
Notifications
You must be signed in to change notification settings - Fork 0
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
9주차미션-제이 #19
Merged
The head ref may contain hidden characters: "\uC81C\uC7749\uC8FC\uCC28"
Merged
9주차미션-제이 #19
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
40b563f
실습
OrangeKim04 a4b1bad
미션1: cartSlice 생성
OrangeKim04 69dc0c8
미션1: main에 Provider를주고 store연결
OrangeKim04 14cfb8d
미션1: Item컴포넌트 생성
OrangeKim04 7b3ae4c
미션1: 전체화면 구성
OrangeKim04 96f636e
미션1: 아이콘 스타일 수정
OrangeKim04 1a9a534
모달 포탈과 스타일 구현
OrangeKim04 e6387c8
미션2: 모달 슬라이스
OrangeKim04 e25b7d8
미션2: store에 모달슬라이스 추가
OrangeKim04 e136386
미션2: 모달 적용
OrangeKim04 3a2bc7e
body에 margin 없앰
OrangeKim04 675773f
body에 margin 없앰
OrangeKim04 d69809b
미션3: zustand 활용
OrangeKim04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {createSlice} from '@reduxjs/toolkit'; | ||
import MusicData from '../constants/cartItems'; | ||
|
||
const initialState = { | ||
items: MusicData, | ||
totalCount: 0, | ||
totalPrice: 0, | ||
} | ||
|
||
export const cartSlice = createSlice({ | ||
name : 'cartfunction', | ||
initialState, | ||
reducers:{ | ||
// 음반수량 증가 | ||
increase: (state, action) => { | ||
state.items = state.items.map(e => e.id === action.payload ? {...e, amount: e.amount+1}: e); | ||
}, | ||
// 음반수량 감소 | ||
decrease: (state, action) => { | ||
state.items = state.items.map(e => e.id === action.payload ? {...e, amount: e.amount-1}: e); | ||
}, | ||
// 음반수량이 1보다 작아질 때, 자동 제거 | ||
removeItem: (state, action) => { | ||
state.items = state.items.filter(e => e.id !== action.payload); | ||
}, | ||
// 장바구니 초기화 | ||
clearCart: (state) => { | ||
state.items = []; | ||
state.totalCount = 0; | ||
state.totalPrice = 0; | ||
}, | ||
// 전체 수량 계산 | ||
calculateTotals: (state) => { | ||
const { totalCount, totalPrice } = state.items.reduce( | ||
(totals, item) => { | ||
totals.totalCount += item.amount; | ||
totals.totalPrice += item.amount * item.price; // 가격 계산 | ||
return totals; | ||
}, | ||
{ totalCount: 0, totalPrice: 0 } // 초기값 | ||
); | ||
|
||
state.totalCount = totalCount; // 총 수량 업데이트 | ||
state.totalPrice = totalPrice; // 총 가격 업데이트 | ||
|
||
console.log(`총 수량: ${totalCount}, 총 가격: ${totalPrice}`); | ||
}, | ||
|
||
} | ||
}) | ||
|
||
export const {increase, decrease, removeItem, clearCart, calculateTotals} = cartSlice.actions; | ||
//store에서 add, remove, complte 액션을 내보낸다. | ||
export default cartSlice.reducer; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {configureStore} from '@reduxjs/toolkit' | ||
import cartSlice from './cartSlice' | ||
import modalSlice from '../components/modal/modalSlice' | ||
|
||
export default configureStore({ | ||
reducer : { | ||
cart : cartSlice, | ||
modal: modalSlice | ||
} | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
전개연산자로 복사를 한 뒤에 변경사항만 변경하는 방식이 굉장히 좋은 것 같아요!