-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db0f7c7
commit 135629f
Showing
2 changed files
with
84 additions
and
16 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
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 |