Skip to content

Commit

Permalink
Merge pull request #183 from ppochaco/fix/157-point
Browse files Browse the repository at this point in the history
Fix: 포인트 결제하기 로직 수정
  • Loading branch information
ppochaco authored Nov 10, 2024
2 parents 35e0f07 + c606de1 commit 137ae0a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
25 changes: 25 additions & 0 deletions src/api/services/point/purchase.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ import { useQuery } from '@tanstack/react-query'
import { authorizationInstance } from '@/api/instance'
import { appendParamsToUrl } from '@/api/utils/common/appendParamsToUrl'

type PurchasePointResponseParams = {
point: number
}

type PurchasePointRequest = {
nextRedirectPcUrl: string
}

const purchasePoint = async ({ point }: PurchasePointResponseParams) => {
const response = await authorizationInstance.get<PurchasePointRequest>(
appendParamsToUrl('/api/point/purchase', { point })
)

return response.data.nextRedirectPcUrl
}

export const usePurchasePoint = ({ point }: PurchasePointResponseParams) => {
return useQuery({
queryKey: ['point', 'purchase', 'redirect'],
queryFn: () => purchasePoint({ point }),
refetchOnWindowFocus: false,
enabled: false,
})
}

type PurchasePointApproveResponseParams = {
pg_token: string
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Modal/ConfirmModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ButtonHTMLAttributes, ReactElement, ReactNode } from 'react'
import { ReactElement, ReactNode } from 'react'

import {
Box,
Button,
ButtonProps,
Modal,
ModalContent,
ModalFooter,
Expand Down Expand Up @@ -75,8 +76,7 @@ export const ConfirmModal = ({
)
}

interface ConfirmModalButtonProps
extends ButtonHTMLAttributes<HTMLButtonElement> {}
interface ConfirmModalButtonProps extends ButtonProps {}

export const ConfirmModalButton = ({
children,
Expand Down
34 changes: 16 additions & 18 deletions src/pages/PointPage/PurchasePointCard/PointModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@ import { BiCheckCircle } from 'react-icons/bi'

import { Button, useDisclosure } from '@chakra-ui/react'

import { appendParamsToUrl } from '@/api/utils/common/appendParamsToUrl'
import { usePurchasePoint } from '@/api/services/point/purchase.api'
import {
ConfirmModal,
ConfirmModalButton,
} from '@/components/Modal/ConfirmModal'
import { useAuthTokenStore } from '@/stores/auth-token'

export const PointModal = () => {
const { isOpen, onOpen, onClose } = useDisclosure()
const authToken = useAuthTokenStore((state) => state.authToken)
const { data: redirectUrl, refetch: purchasePoint } = usePurchasePoint({
point: 100,
})

const purchasePoint = () => {
fetch(
appendParamsToUrl(`${import.meta.env.VITE_BASE_URL}/api/point/purchase`, {
point: 100,
}),
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
},
}
)
const handleRedirectPurchase = () => {
window.location.href = `${redirectUrl}`
}

return (
<div>
<Button colorScheme="primary" borderRadius={10} onClick={onOpen}>
<Button
colorScheme="primary"
borderRadius={10}
onClick={() => {
onOpen()
purchasePoint()
}}
>
+ 100P
</Button>
<ConfirmModal
Expand All @@ -43,8 +40,9 @@ export const PointModal = () => {
<ConfirmModalButton
onClick={() => {
onClose()
purchasePoint()
handleRedirectPurchase()
}}
isDisabled={!redirectUrl}
>
확인
</ConfirmModalButton>
Expand Down

0 comments on commit 137ae0a

Please sign in to comment.