|
| 1 | +import { useMutation } from "@tanstack/react-query"; |
| 2 | +import { useAccount, useSignTypedData } from "wagmi"; |
| 3 | +import { useStepProcessDialogContext } from "@/components/global/step-process-dialog"; |
| 4 | +import { hypercertApiSigningDomain } from "@/configs/constants"; |
| 5 | +import { HYPERCERTS_API_URL_REST } from "@/configs/hypercerts"; |
| 6 | +import { revalidatePathServerAction } from "@/app/actions/revalidatePathServerAction"; |
| 7 | + |
| 8 | +export interface BlueprintDeleteRequest { |
| 9 | + signature: string; |
| 10 | + chain_id: number; |
| 11 | + admin_address: string; |
| 12 | +} |
| 13 | + |
| 14 | +export const useDeleteBlueprint = () => { |
| 15 | + const { address, chainId } = useAccount(); |
| 16 | + const { signTypedDataAsync } = useSignTypedData(); |
| 17 | + const { |
| 18 | + setDialogStep: setStep, |
| 19 | + setSteps, |
| 20 | + setOpen, |
| 21 | + setExtraContent, |
| 22 | + } = useStepProcessDialogContext(); |
| 23 | + |
| 24 | + return useMutation({ |
| 25 | + mutationKey: ["deleteBlueprint"], |
| 26 | + mutationFn: async ({ blueprintId }: { blueprintId: number }) => { |
| 27 | + if (!address) { |
| 28 | + throw new Error("No address found"); |
| 29 | + } |
| 30 | + |
| 31 | + if (!chainId) { |
| 32 | + throw new Error("No chainId found"); |
| 33 | + } |
| 34 | + |
| 35 | + setSteps([ |
| 36 | + { |
| 37 | + id: "Awaiting signature", |
| 38 | + description: "Awaiting signature", |
| 39 | + }, |
| 40 | + { |
| 41 | + id: "Deleting blueprint", |
| 42 | + description: "Deleting blueprint", |
| 43 | + }, |
| 44 | + ]); |
| 45 | + setOpen(true); |
| 46 | + |
| 47 | + await setStep("Awaiting signature"); |
| 48 | + let signature: string; |
| 49 | + |
| 50 | + try { |
| 51 | + signature = await signTypedDataAsync({ |
| 52 | + account: address, |
| 53 | + domain: hypercertApiSigningDomain(chainId), |
| 54 | + types: { |
| 55 | + Blueprint: [{ name: "id", type: "uint256" }], |
| 56 | + BlueprintDeleteRequest: [{ name: "blueprint", type: "Blueprint" }], |
| 57 | + }, |
| 58 | + primaryType: "BlueprintDeleteRequest", |
| 59 | + message: { |
| 60 | + blueprint: { id: BigInt(blueprintId) }, |
| 61 | + }, |
| 62 | + }); |
| 63 | + if (!signature) { |
| 64 | + throw new Error("No signature found"); |
| 65 | + } |
| 66 | + } catch (error) { |
| 67 | + await setStep( |
| 68 | + "Awaiting signature", |
| 69 | + "error", |
| 70 | + error instanceof Error ? error.message : "Error signing message", |
| 71 | + ); |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + await setStep("Deleting blueprint"); |
| 76 | + |
| 77 | + try { |
| 78 | + const body: BlueprintDeleteRequest = { |
| 79 | + signature: signature as `0x${string}`, |
| 80 | + chain_id: chainId, |
| 81 | + admin_address: address as `0x${string}`, |
| 82 | + }; |
| 83 | + await fetch(`${HYPERCERTS_API_URL_REST}/blueprints/${blueprintId}`, { |
| 84 | + method: "DELETE", |
| 85 | + body: JSON.stringify(body), |
| 86 | + headers: { |
| 87 | + "Content-Type": "application/json", |
| 88 | + }, |
| 89 | + }).then((res) => { |
| 90 | + if (!res.ok) { |
| 91 | + throw new Error("Error deleting blueprint"); |
| 92 | + } |
| 93 | + }); |
| 94 | + setExtraContent( |
| 95 | + <div className="flex flex-col spacy-y-2"> |
| 96 | + <p className="text-sm font-medium"> |
| 97 | + Blueprint deleted successfully |
| 98 | + </p> |
| 99 | + </div>, |
| 100 | + ); |
| 101 | + await setStep("Deleting blueprint", "completed"); |
| 102 | + await revalidatePathServerAction([ |
| 103 | + "/blueprints", |
| 104 | + `/profile/${address}`, |
| 105 | + { path: `/`, type: "layout" }, |
| 106 | + ]); |
| 107 | + setTimeout(() => { |
| 108 | + setOpen(false); |
| 109 | + }, 2000); |
| 110 | + } catch (error) { |
| 111 | + await setStep( |
| 112 | + "Deleting blueprint", |
| 113 | + "error", |
| 114 | + error instanceof Error ? error.message : "Error deleting blueprint", |
| 115 | + ); |
| 116 | + } |
| 117 | + }, |
| 118 | + }); |
| 119 | +}; |
0 commit comments