Skip to content

Instantly share code, notes, and snippets.

@Ernesto-tha-great
Created August 5, 2024 10:28
Show Gist options
  • Select an option

  • Save Ernesto-tha-great/9002c5492ed74257cd9f5c940dfb48be to your computer and use it in GitHub Desktop.

Select an option

Save Ernesto-tha-great/9002c5492ed74257cd9f5c940dfb48be to your computer and use it in GitHub Desktop.
"use client";
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog";
import { toast } from "sonner";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
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 { MoveLeft } from "lucide-react";
interface BetModalProps {
children: React.ReactNode;
onPlaceBet: (betId: number, betForExceed: boolean, amount: string) => void;
betId: number;
}
const PlaceBetModal = ({ children, onPlaceBet, betId }: BetModalProps) => {
const formSchema = z.object({
amount: z.string(),
betForExceed: z.boolean(),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
amount: "",
betForExceed: false,
},
});
const handleSubmit = async (data: z.infer<typeof formSchema>) => {
console.log(data, betId);
try {
onPlaceBet(betId, data.betForExceed, data.amount);
} 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-center">
<AlertDialogCancel className="border-none">
<MoveLeft size={24} />
</AlertDialogCancel>
<h1>Make a bet</h1>
</div>
</AlertDialogTitle>
</AlertDialogHeader>
<div>
<Form {...form}>
<form
onSubmit={form.handleSubmit(handleSubmit)}
className="space-y-8"
>
<FormField
control={form.control}
name="amount"
render={({ field }) => (
<FormItem>
<FormLabel className="">
<h1 className="text-[#32393A]">Amount</h1>
</FormLabel>
<FormControl>
<Input
className="rounded-full"
placeholder="Bet amount (ETH)"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="betForExceed"
render={({ field }) => (
<FormItem>
<FormLabel>
<h1 className="text-[#32393A]">Bet</h1>
</FormLabel>
<Select
onValueChange={(value) =>
field.onChange(value === "true")
}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="will exceed" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="true">true</SelectItem>
<SelectItem value="false">false</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<Button
className="bg-[#007A86] self-center my-8 rounded-full w-full"
size="lg"
type="submit"
>
Submit
</Button>
</form>
</Form>
</div>
<AlertDialogFooter className="mt-4">
<AlertDialogCancel>Cancel</AlertDialogCancel>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
);
};
export default PlaceBetModal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment