Skip to content

Commit

Permalink
fix: made transfer page standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
VariableVic committed Nov 25, 2024
1 parent db0f7c7 commit 135629f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/app/[countryCode]/(main)/order/[id]/transfer/[token]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Button, Heading, Text } from "@medusajs/ui"
import LocalizedClientLink from "@modules/common/components/localized-client-link"
import { Heading, Text } from "@medusajs/ui"
import TransferActions from "@modules/order/components/transfer-actions"
import TransferImage from "@modules/order/components/transfer-image"

export default async function TransferPage({
Expand Down Expand Up @@ -31,20 +31,7 @@ export default async function TransferPage({
further action is required.
</Text>
<div className="w-full h-px bg-zinc-200" />
<div className="flex gap-x-4">
<Button size="large" asChild>
<LocalizedClientLink href={`/order/${id}/transfer/${token}/accept`}>
Approve transfer
</LocalizedClientLink>
</Button>
<Button size="large" variant="secondary" asChild>
<LocalizedClientLink
href={`/order/${id}/transfer/${token}/decline`}
>
Decline transfer
</LocalizedClientLink>
</Button>
</div>
<TransferActions id={id} token={token} />
</div>
</div>
)
Expand Down
81 changes: 81 additions & 0 deletions src/modules/order/components/transfer-actions/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"use client"

import { acceptTransferRequest, declineTransferRequest } from "@lib/data/orders"
import { Button, Text } from "@medusajs/ui"
import { useState } from "react"

type TransferStatus = "pending" | "success" | "error"

const TransferActions = ({ id, token }: { id: string; token: string }) => {
const [errorMessage, setErrorMessage] = useState<string | null>(null)
const [status, setStatus] = useState<{
accept: TransferStatus | null
decline: TransferStatus | null
} | null>({
accept: null,
decline: null,
})

const acceptTransfer = async () => {
setStatus({ accept: "pending", decline: null })
setErrorMessage(null)

const { success, error } = await acceptTransferRequest(id, token)

if (error) setErrorMessage(error)
setStatus({ accept: success ? "success" : "error", decline: null })
}

const declineTransfer = async () => {
setStatus({ accept: null, decline: "pending" })
setErrorMessage(null)

const { success, error } = await declineTransferRequest(id, token)

if (error) setErrorMessage(error)
setStatus({ accept: null, decline: success ? "success" : "error" })
}

return (
<div className="flex flex-col gap-y-4">
{status?.accept === "success" && (
<Text className="text-emerald-500">
Order transferred successfully!
</Text>
)}
{status?.decline === "success" && (
<Text className="text-emerald-500">
Order transfer declined successfully!
</Text>
)}
{status?.accept !== "success" && status?.decline !== "success" && (
<div className="flex gap-x-4">
<Button
size="large"
onClick={acceptTransfer}
isLoading={status?.accept === "pending"}
disabled={
status?.accept === "pending" || status?.decline === "pending"
}
>
Accept transfer
</Button>
<Button
size="large"
variant="secondary"
onClick={declineTransfer}
isLoading={status?.decline === "pending"}
disabled={
status?.accept === "pending" || status?.decline === "pending"
}
>
Decline transfer
</Button>
</div>
)}
{errorMessage && <Text className="text-red-500">{errorMessage}</Text>}
</div>
)
}

export default TransferActions

0 comments on commit 135629f

Please sign in to comment.