-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useCash Hook, cashSlice reducer 생성 현금의 생성,읽기, 추가 기능 구현 프로필 탭에서 화면 구현 Issues #70
- Loading branch information
김병현
authored and
김병현
committed
Sep 11, 2023
1 parent
ad74d4b
commit 58173c6
Showing
5 changed files
with
133 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// hooks/useCash.ts | ||
import { useQuery, useMutation } from 'react-query'; | ||
import axios from 'axios'; | ||
|
||
export const useCreateCash = () => { | ||
return useMutation((initialAmount: number) => axios.post('http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/cash', { cash: initialAmount })); | ||
} | ||
|
||
export const useGetCash = (cashId: string | null) => { | ||
const queryFn = () => { | ||
if (!cashId) { | ||
throw new Error("Cash ID is not provided."); | ||
} | ||
return axios.get(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/cash/${cashId}`); | ||
}; | ||
|
||
const queryResult = useQuery(['cash', cashId], queryFn, { | ||
enabled: !!cashId, | ||
}); | ||
|
||
if (!cashId) { | ||
return { | ||
...queryResult, | ||
data: null | ||
}; | ||
} | ||
|
||
return queryResult; | ||
} | ||
|
||
|
||
export const useUpdateCash = () => { | ||
return useMutation((data: { cashId: number, cashAmount: number }) => axios.post(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/cash/${data.cashId}`, { cash: data.cashAmount })); | ||
} |
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,21 @@ | ||
// store/cashSlice.ts | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const cashSlice = createSlice({ | ||
name: 'cash', | ||
initialState: { | ||
cashId: null, | ||
cashAmount: null, | ||
}, | ||
reducers: { | ||
setCashId: (state, action) => { | ||
state.cashId = action.payload; | ||
}, | ||
setCashAmount: (state, action) => { | ||
state.cashAmount = action.payload; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setCashId, setCashAmount } = cashSlice.actions; | ||
export default cashSlice.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