-
-
Save Ernesto-tha-great/56588c6396852bad8311323078f3bd1f to your computer and use it in GitHub Desktop.
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
"use client"; | |
import { | |
AlertDialog, | |
AlertDialogCancel, | |
AlertDialogContent, | |
AlertDialogFooter, | |
AlertDialogHeader, | |
AlertDialogTitle, | |
AlertDialogTrigger, | |
} from "@/components/ui/alert-dialog"; | |
import { useEffect } from "react"; | |
import { | |
OkidoTokenAbi, | |
okidoFinance, | |
okidoFinanceAbi, | |
okidoToken, | |
} from "@/constants"; | |
import { toast } from "sonner"; | |
import { | |
Form, | |
FormControl, | |
FormField, | |
FormItem, | |
FormLabel, | |
FormMessage, | |
} from "@/components/ui/form"; | |
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi"; | |
import { z } from "zod"; | |
import { zodResolver } from "@hookform/resolvers/zod"; | |
import { useForm } from "react-hook-form"; | |
import { Input } from "@/components/ui/input"; | |
import { Button } from "@/components/ui/button"; | |
import { XIcon } from "lucide-react"; | |
import { parseUnits } from "viem"; | |
interface InvestModalProps { | |
children: React.ReactNode; | |
} | |
const AddPropertyModal = ({ children }: InvestModalProps) => { | |
const { | |
data: hash, | |
error, | |
isPending, | |
writeContractAsync, | |
} = useWriteContract(); | |
const { isLoading: isConfirming, isSuccess: isConfirmed } = | |
useWaitForTransactionReceipt({ | |
hash, | |
}); | |
useEffect(() => { | |
if (isConfirming) { | |
toast.loading("Transaction Pending"); | |
} | |
if (isConfirmed) { | |
toast.success("Transaction Successful", { | |
action: { | |
label: "View on Etherscan", | |
onClick: () => { | |
window.open(`https://explorer-holesky.morphl2.io/tx/${hash}`); | |
}, | |
}, | |
}); | |
} | |
if (error) { | |
toast.error("Transaction Failed"); | |
} | |
}, [isConfirming, isConfirmed, error, hash]); | |
const formSchema = z.object({ | |
name: z.any(), | |
symbol: z.any(), | |
uri: z.any(), | |
totalShares: z.any(), | |
pricePerShare: z.any(), | |
}); | |
const form = useForm<z.infer<typeof formSchema>>({ | |
resolver: zodResolver(formSchema), | |
defaultValues: { | |
name: "", | |
symbol: "", | |
uri: "", | |
totalShares: 0, | |
pricePerShare: 0, | |
}, | |
}); | |
const AddProperty = async (data: z.infer<typeof formSchema>) => { | |
console.log(data); | |
try { | |
const addPropertyTx = await writeContractAsync({ | |
address: okidoFinance, | |
abi: okidoFinanceAbi, | |
functionName: "createProperty", | |
args: [ | |
data.name, | |
data.symbol, | |
data.uri, | |
data.totalShares, | |
data.pricePerShare, | |
], | |
}); | |
console.log("property transaction hash:", addPropertyTx); | |
} catch (err: any) { | |
toast.error("Transaction Failed: " + err.message); | |
console.log("Transaction Failed: " + err.message); | |
} | |
}; | |
return ( | |
<AlertDialog> | |
<AlertDialogTrigger asChild>{children}</AlertDialogTrigger> | |
<AlertDialogContent> | |
<AlertDialogHeader> | |
<AlertDialogTitle> | |
<div className="flex items-center gap-6 justify-between"> | |
<h1>Add a Property</h1> | |
<AlertDialogCancel className="border-none"> | |
<XIcon size={28} /> | |
</AlertDialogCancel> | |
</div> | |
</AlertDialogTitle> | |
</AlertDialogHeader> | |
<div> | |
<Form {...form}> | |
<form | |
onSubmit={form.handleSubmit(AddProperty)} | |
className="space-y-8 mt-6" | |
> | |
<FormField | |
control={form.control} | |
name="name" | |
render={({ field }) => ( | |
<FormItem> | |
<FormLabel className=""> | |
<h1 className="text-[#32393A]">Property name</h1> | |
</FormLabel> | |
<FormControl> | |
<Input | |
className=" bg-[#14A800]/10 rounded-lg focus:outline-none border-none" | |
placeholder="abc.." | |
{...field} | |
/> | |
</FormControl> | |
<FormMessage /> | |
</FormItem> | |
)} | |
/> | |
<FormField | |
control={form.control} | |
name="symbol" | |
render={({ field }) => ( | |
<FormItem> | |
<FormLabel className=""> | |
<h1 className="text-[#32393A]">Property symbol</h1> | |
</FormLabel> | |
<FormControl> | |
<Input | |
className=" bg-[#14A800]/10 rounded-lg focus:outline-none border-none" | |
placeholder="xyz" | |
{...field} | |
/> | |
</FormControl> | |
<FormMessage /> | |
</FormItem> | |
)} | |
/> | |
<FormField | |
control={form.control} | |
name="uri" | |
render={({ field }) => ( | |
<FormItem> | |
<FormLabel className=""> | |
<h1 className="text-[#32393A]">Link to image</h1> | |
</FormLabel> | |
<FormControl> | |
<Input | |
className=" bg-[#14A800]/10 rounded-lg focus:outline-none border-none" | |
placeholder="https://..." | |
{...field} | |
/> | |
</FormControl> | |
<FormMessage /> | |
</FormItem> | |
)} | |
/> | |
<FormField | |
control={form.control} | |
name="totalShares" | |
render={({ field }) => ( | |
<FormItem> | |
<FormLabel className=""> | |
<h1 className="text-[#32393A]">Total shares</h1> | |
</FormLabel> | |
<FormControl> | |
<Input | |
className=" bg-[#14A800]/10 rounded-lg focus:outline-none border-none" | |
type="number" | |
placeholder="0" | |
{...field} | |
/> | |
</FormControl> | |
<FormMessage /> | |
</FormItem> | |
)} | |
/> | |
<FormField | |
control={form.control} | |
name="pricePerShare" | |
render={({ field }) => ( | |
<FormItem> | |
<FormLabel className=""> | |
<h1 className="text-[#32393A]">Price per share</h1> | |
</FormLabel> | |
<FormControl> | |
<Input | |
className=" bg-[#14A800]/10 rounded-lg focus:outline-none border-none" | |
type="number" | |
placeholder="0" | |
{...field} | |
/> | |
</FormControl> | |
<FormMessage /> | |
</FormItem> | |
)} | |
/> | |
<div className="flex items-center justify-center gap-4"> | |
<AlertDialogCancel className="bg-[#14A800]/10 rounded-full px-20 py-6"> | |
Cancel | |
</AlertDialogCancel> | |
<Button | |
className="bg-[#14A800] w-fit my-8 rounded-full px-20 py-6" | |
size="lg" | |
disabled={isPending} | |
type="submit" | |
> | |
{isPending ? "Loading" : "Submit"} | |
</Button> | |
</div> | |
</form> | |
</Form> | |
</div> | |
</AlertDialogContent> | |
</AlertDialog> | |
); | |
}; | |
export default AddPropertyModal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment