Frontend registration

This commit is contained in:
Leons Aleksandrovs
2025-07-05 22:29:51 +03:00
parent 66ed77e758
commit 6b20ea1a3c
+27 -8
View File
@@ -2,7 +2,10 @@ import { Button } from "@/components/ui/button";
import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { useAppForm } from "@/hooks/formHook"; import { useAppForm } from "@/hooks/formHook";
import Guest from "@/layouts/Guest"; import Guest from "@/layouts/Guest";
import { createFileRoute, Link } from "@tanstack/react-router"; import requests from "@/lib/requests";
import { createFileRoute, Link, redirect, useNavigate } from "@tanstack/react-router";
import { useState } from "react";
import toast from "react-hot-toast";
import * as z from "zod/v4"; import * as z from "zod/v4";
export const Route = createFileRoute("/register")({ export const Route = createFileRoute("/register")({
@@ -10,18 +13,23 @@ export const Route = createFileRoute("/register")({
}); });
const registerSchema = z const registerSchema = z
// Basic validation
.object({ .object({
email: z.string().email(), email: z.string().email(),
name: z.string().min(2, "Name is too short"), name: z.string().min(2, "Name is too short").max(50, "Name is too long (50 char max)"),
password: z.string().min(8, "Password is too short"), password: z.string().min(8, "Password is too short"),
repeatPassword: z.string().min(8, "Password is too short"), repeatPassword: z.string().min(8, "Password is too short"),
}) })
// Custom validation
.refine((data) => data.password === data.repeatPassword, { .refine((data) => data.password === data.repeatPassword, {
message: "Passwords don't match", message: "Passwords don't match",
path: ["repeatPassword"], path: ["repeatPassword"],
}); });
function RouteComponent() { function RouteComponent() {
const loading = useState(false);
const navigate = useNavigate();
const form = useAppForm({ const form = useAppForm({
defaultValues: { defaultValues: {
email: "", email: "",
@@ -33,14 +41,25 @@ function RouteComponent() {
onBlur: registerSchema, onBlur: registerSchema,
}, },
onSubmit: ({ value }) => { onSubmit: ({ value }) => {
console.log(value); requests.post<{ message: string }>("/register", {
data: value,
before() {
// use state to true loading
loading[1](true);
},
success(data) {
toast.success(data.message);
form.reset();
// Wait a bit before redirecting
setTimeout(() => {
navigate({ to: "/login" });
}, 1000);
},
});
}, },
}); });
// useEffect(() => {
// requests.post<{ message: string }>("/ping", { data: { hello: "world" } });
// }, []);
return ( return (
<Guest className="h-screen w-screen grid place-items-center"> <Guest className="h-screen w-screen grid place-items-center">
<Card className="w-full max-w-[400px]"> <Card className="w-full max-w-[400px]">
@@ -94,7 +113,7 @@ function RouteComponent() {
)} )}
/> />
<Button onClick={form.handleSubmit} className="w-full"> <Button disabled={loading[0]} onClick={form.handleSubmit} className="w-full">
Register Register
</Button> </Button>
</CardContent> </CardContent>