From 135629f181ee528c61d022f165e1634925f16ce6 Mon Sep 17 00:00:00 2001 From: Victor Gerbrands Date: Mon, 25 Nov 2024 13:16:39 +0100 Subject: [PATCH] fix: made transfer page standalone --- .../order/[id]/transfer/[token]/page.tsx | 19 +---- .../components/transfer-actions/index.tsx | 81 +++++++++++++++++++ 2 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 src/modules/order/components/transfer-actions/index.tsx diff --git a/src/app/[countryCode]/(main)/order/[id]/transfer/[token]/page.tsx b/src/app/[countryCode]/(main)/order/[id]/transfer/[token]/page.tsx index 1bf02e90..29776a74 100644 --- a/src/app/[countryCode]/(main)/order/[id]/transfer/[token]/page.tsx +++ b/src/app/[countryCode]/(main)/order/[id]/transfer/[token]/page.tsx @@ -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({ @@ -31,20 +31,7 @@ export default async function TransferPage({ further action is required.
-
- - -
+
) diff --git a/src/modules/order/components/transfer-actions/index.tsx b/src/modules/order/components/transfer-actions/index.tsx new file mode 100644 index 00000000..e6148a84 --- /dev/null +++ b/src/modules/order/components/transfer-actions/index.tsx @@ -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(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 ( +
+ {status?.accept === "success" && ( + + Order transferred successfully! + + )} + {status?.decline === "success" && ( + + Order transfer declined successfully! + + )} + {status?.accept !== "success" && status?.decline !== "success" && ( +
+ + +
+ )} + {errorMessage && {errorMessage}} +
+ ) +} + +export default TransferActions