Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #47

Merged
Merged

fix #47

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 110 additions & 103 deletions components/AuthComponent/SignupForm.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
"use client";

import React, { useState } from "react";
import React from "react";
import { useRouter } from "next/navigation";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
Expand All @@ -24,33 +30,29 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { UserType } from "@/types/typeInterfaces";
import { useAuthStore } from "@/store/AuthStore/useAuthStore";
import AuthBottom from "./AuthBottom";
import LoadingButton from "../Buttons/LoadingButton";
import { signupSchema } from "@/validations/validation";
type SignupFormValues = z.infer<typeof signupSchema>;

export default function SignupForm() {
const { isSigningUp, signup, signupError } = useAuthStore();
const router = useRouter();
const [formData, setFormData] = useState<UserType>({
fullName: "",
email: "",
password: "",
leetcodeUsername: "",
gender: "",
});

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setFormData({ ...formData, [e.target.name]: e.target.value });
};

const handleSelectChange = (value: string) => {
setFormData({ ...formData, gender: value });
};
const form = useForm<SignupFormValues>({
resolver: zodResolver(signupSchema),
defaultValues: {
fullName: "",
email: "",
password: "",
leetcodeUsername: "",
gender: "",
},
});

const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
signup(formData, router);
const onSubmit = (data: SignupFormValues) => {
signup(data, router);
};

return (
Expand All @@ -65,93 +67,98 @@ export default function SignupForm() {
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="fullName">Full Name</Label>
<Input
id="fullName"
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="fullName"
type="text"
placeholder="Your Name"
value={formData.fullName}
onChange={handleChange}
required
render={({ field }) => (
<FormItem>
<FormLabel>Full Name</FormLabel>
<FormControl>
<Input placeholder="Your Name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
<FormField
control={form.control}
name="email"
type="email"
placeholder="[email protected]"
value={formData.email}
onChange={handleChange}
required
render={({ field }) => (
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input placeholder="[email protected]" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<Input
id="password"
<FormField
control={form.control}
name="password"
type="password"
minLength={6}
placeholder="••••••"
value={formData.password}
onChange={handleChange}
required
render={({ field }) => (
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input type="password" placeholder="••••••" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<div className="space-y-2">
<Label htmlFor="leetcodeUsername">LeetCode Username</Label>
<Input
id="leetcodeUsername"
<FormField
control={form.control}
name="leetcodeUsername"
type="text"
placeholder="Your LeetCode Username"
value={formData.leetcodeUsername}
onChange={handleChange}
required
render={({ field }) => (
<FormItem>
<FormLabel>LeetCode Username</FormLabel>
<FormControl>
<Input placeholder="Your LeetCode Username" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>

<div className="space-y-2">
<Label htmlFor="gender">Gender</Label>
<Select
value={formData.gender}
onValueChange={handleSelectChange}
>
<SelectTrigger className="w-full">
<SelectValue placeholder="Select Gender" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Options</SelectLabel>
<SelectItem value="male">Male</SelectItem>
<SelectItem value="female">Female</SelectItem>
<SelectItem value="other">Other</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</div>

{signupError && (
<Alert variant="destructive">
<AlertDescription>{signupError}</AlertDescription>
</Alert>
)}

<LoadingButton
loading={isSigningUp}
loadingTitle="Registering"
title="Register"
type="submit"
/>
</form>
<FormField
control={form.control}
name="gender"
render={({ field }) => (
<FormItem>
<FormLabel>Gender</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select Gender" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="male">Male</SelectItem>
<SelectItem value="female">Female</SelectItem>
<SelectItem value="other">Other</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
{signupError && (
<Alert variant="destructive">
<AlertDescription>{signupError}</AlertDescription>
</Alert>
)}
<LoadingButton
loading={isSigningUp}
loadingTitle="Registering"
title="Register"
type="submit"
/>
</form>
</Form>
</CardContent>
<CardFooter className="flex justify-center">
<AuthBottom
Expand Down
Loading
Loading