mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-07-01 15:23:58 +00:00
wip
This commit is contained in:
@ -5,7 +5,6 @@ import { Button } from "@/components/ui/button";
|
||||
|
||||
import { signin } from "@/actions/auth-actions";
|
||||
import { Loader } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { useActionState } from "react";
|
||||
import { PhoneInput } from "../ui/phone-input";
|
||||
|
||||
@ -17,8 +16,7 @@ export default function LoginForm() {
|
||||
|
||||
return (
|
||||
<form className="bg-white overflow-clip dark:bg-transparent dark:border-2 w-full max-w-xs mx-auto rounded-lg shadow mt-4" action={formAction}>
|
||||
<h4 className="text-center rounded m-2 text-xl font-bold dark:text-white text-gray-600 dark:bg-gray-900 bg-gray-200 py-4">Login</h4>
|
||||
<div className="py-2 px-10">
|
||||
<div className="py-6 px-10">
|
||||
<div className="grid gap-4">
|
||||
<PhoneInput
|
||||
id="phone-number"
|
||||
@ -35,12 +33,12 @@ export default function LoginForm() {
|
||||
{isPending ? <Loader className="animate-spin" /> : "Request OTP"}
|
||||
</Button>
|
||||
</div>
|
||||
<div className="my-4 text-center text-sm">
|
||||
{/* <div className="mt-2 text-center text-sm">
|
||||
Don't have an account?{" "}
|
||||
<Link href="signup" className="underline">
|
||||
Sign up
|
||||
</Link>
|
||||
</div>
|
||||
</div> */}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
|
@ -1,167 +1,241 @@
|
||||
"use client";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { signUpFormSchema } from "@/lib/schemas";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import Link from "next/link";
|
||||
|
||||
import { signup } from "@/actions/auth-actions";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Loader } from "lucide-react";
|
||||
import { useActionState } from "react";
|
||||
import { useSearchParams } from "next/navigation";
|
||||
import { Atoll, Island, Prisma } from "@prisma/client";
|
||||
import * as React from "react"
|
||||
|
||||
import { useForm } from "react-hook-form";
|
||||
import { toast } from "sonner";
|
||||
import type { z } from "zod";
|
||||
import { DatePicker } from "../ui/datepicker";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectLabel,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
|
||||
|
||||
export default function SignUpForm() {
|
||||
const params = useSearchParams();
|
||||
console.log(params);
|
||||
const phone_number = params.get("phone_number");
|
||||
const form = useForm<z.infer<typeof signUpFormSchema>>({
|
||||
resolver: zodResolver(signUpFormSchema),
|
||||
defaultValues: {
|
||||
phoneNumber: phone_number ?? "",
|
||||
name: "",
|
||||
id_card: "",
|
||||
house_name: "",
|
||||
island: "F.Dharanboodhoo",
|
||||
},
|
||||
});
|
||||
|
||||
function onSubmit(values: z.infer<typeof signUpFormSchema>) {
|
||||
console.log(values);
|
||||
type AtollWithIslands = Prisma.AtollGetPayload<{
|
||||
include: {
|
||||
islands: true;
|
||||
}
|
||||
}>
|
||||
|
||||
export default function SignUpForm({ atolls }: { atolls: AtollWithIslands[] }) {
|
||||
const [atoll, setAtoll] = React.useState<AtollWithIslands>()
|
||||
const [island, setIsland] = React.useState<string>()
|
||||
|
||||
|
||||
|
||||
const [actionState, action, isPending] = useActionState(signup, {
|
||||
message: "",
|
||||
});
|
||||
const params = useSearchParams();
|
||||
const phoneNumberFromUrl = params.get("phone_number");
|
||||
const NUMBER_WITHOUT_DASH = phoneNumberFromUrl?.split("-").join("")
|
||||
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmit)}
|
||||
className="max-w-xs w-full bg-white dark:bg-transparent dark:border-2 shadow rounded-lg mx-auto"
|
||||
>
|
||||
<h4 className="text-center rounded m-2 text-xl font-bold text-gray-600 bg-gray-200 py-4">
|
||||
Sign up
|
||||
</h4>
|
||||
<div className="py-2 px-4 my-2">
|
||||
<FormField
|
||||
control={form.control}
|
||||
<form
|
||||
action={action}
|
||||
className="max-w-xs mt-4 w-full bg-white dark:bg-transparent dark:border-2 shadow rounded-lg mx-auto"
|
||||
>
|
||||
<div className="py-2 px-4 my-2 space-y-2">
|
||||
<div>
|
||||
<label htmlFor="name" className="text-sm">Name</label>
|
||||
|
||||
<Input
|
||||
className={cn(
|
||||
'text-base',
|
||||
actionState.errors?.fieldErrors.name && 'border-2 border-red-500',
|
||||
)}
|
||||
name="name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="eg: Shihaam" type="text" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
type="text"
|
||||
defaultValue={(actionState.payload?.get("name") || "") as string}
|
||||
placeholder="Full Name"
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
{actionState.errors?.fieldErrors.name && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors?.fieldErrors.name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="id_card" className="text-sm">ID Card</label>
|
||||
<Input
|
||||
name="id_card"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>ID Card</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="Axxxxxx" type="" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
type="text"
|
||||
maxLength={7}
|
||||
defaultValue={(actionState.payload?.get("id_card") || "") as string}
|
||||
className={cn(
|
||||
'text-base',
|
||||
actionState.errors?.fieldErrors?.id_card && 'border-2 border-red-500',
|
||||
)}
|
||||
placeholder="ID Card"
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="island"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Island</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="F.Dharanboodhoo"
|
||||
disabled
|
||||
type=""
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
{actionState?.errors?.fieldErrors?.id_card?.[0] && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors.fieldErrors.id_card[0]}
|
||||
</span>
|
||||
)}
|
||||
{actionState.db_error === "id_card" && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.message}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
{/* <label htmlFor="island_name" className="text-sm">Island Name</label>
|
||||
<Input
|
||||
name="island_name"
|
||||
type="text"
|
||||
defaultValue={"F.Dharaboondhoo"}
|
||||
className={cn(
|
||||
'text-base cursor-not-allowed',
|
||||
actionState.errors?.fieldErrors?.island_name && 'border-2 border-red-500',
|
||||
)}
|
||||
readOnly
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
{actionState?.errors?.fieldErrors.island_name && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState?.errors?.fieldErrors.island_name}
|
||||
</span>
|
||||
)} */}
|
||||
|
||||
<div>
|
||||
<label htmlFor="atoll" className="text-sm">Atoll</label>
|
||||
<Select onValueChange={(v) => {
|
||||
setAtoll(atolls.find((atoll) => atoll.id === v))
|
||||
setIsland("")
|
||||
}} name="atoll_id" value={atoll?.id}>
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select atoll" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectLabel>Atolls</SelectLabel>
|
||||
{atolls.map((atoll) => (
|
||||
<SelectItem key={atoll.id} value={atoll.id}>
|
||||
{atoll.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
{actionState.errors?.fieldErrors?.atoll_id && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors?.fieldErrors?.atoll_id}
|
||||
</span>
|
||||
)}
|
||||
</Select>
|
||||
</div>
|
||||
<div>
|
||||
<label htmlFor="island" className="text-sm">Island</label>
|
||||
<Select
|
||||
name="island_id">
|
||||
<SelectTrigger className="w-full">
|
||||
<SelectValue placeholder="Select island" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectGroup>
|
||||
<SelectLabel>Islands</SelectLabel>
|
||||
{atoll?.islands.map((island) => (
|
||||
<SelectItem key={island.id} value={island.id}>
|
||||
{island.name}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
{actionState.errors?.fieldErrors?.island_id && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors?.fieldErrors?.island_id}
|
||||
</span>
|
||||
)}
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="house_name" className="text-sm">House Name</label>
|
||||
<Input
|
||||
className={cn(
|
||||
'text-base',
|
||||
actionState.errors?.fieldErrors?.house_name && 'border-2 border-red-500',
|
||||
)}
|
||||
name="house_name"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>House Name</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="eg: Skyvilla" type="text" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
defaultValue={(actionState.payload?.get("house_name") || "") as string}
|
||||
type="text"
|
||||
placeholder="House Name"
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
{actionState.errors?.fieldErrors?.house_name && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors?.fieldErrors?.house_name}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label htmlFor="dob" className="text-sm">Date of Birth</label>
|
||||
<Input
|
||||
className={cn(
|
||||
'text-base',
|
||||
actionState.errors?.fieldErrors?.dob && 'border-2 border-red-500',
|
||||
)}
|
||||
name="dob"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex flex-col mt-2">
|
||||
<FormLabel>Date of birth</FormLabel>
|
||||
|
||||
<DatePicker
|
||||
date={field.value}
|
||||
setDate={field.onChange}
|
||||
/>
|
||||
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
defaultValue={(actionState.payload?.get("dob") || "") as string}
|
||||
type="date"
|
||||
placeholder="Date of birth"
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="phoneNumber"
|
||||
render={({ field }) => (
|
||||
<FormItem className="flex flex-col items-start mt-2">
|
||||
<FormLabel>Phone number</FormLabel>
|
||||
<FormControl className="w-full">
|
||||
<div className="flex rounded-lg shadow-sm shadow-black/5">
|
||||
<div className="relative">
|
||||
<span
|
||||
className="peer inline-flex h-full appearance-none items-center rounded-none rounded-s-lg border border-input bg-background pe-4 ps-3 text-sm text-muted-foreground transition-shadow hover:bg-accent hover:text-accent-foreground focus:z-10 focus-visible:border-ring focus-visible:text-foreground focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring/20 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
|
||||
aria-label="Protocol"
|
||||
>
|
||||
<option value="https://">+960</option>
|
||||
</span>
|
||||
</div>
|
||||
<Input
|
||||
className="-ms-px rounded-s-none shadow-none focus-visible:z-10"
|
||||
// disabled={Boolean(phone_number ?? "")}
|
||||
placeholder="9xxxxxx / 7xxxxxx"
|
||||
{...field}
|
||||
/>
|
||||
</div>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<Button className="mt-4 w-full" type="submit">
|
||||
Submit
|
||||
</Button>
|
||||
</div>
|
||||
<div className="mb-4 text-center text-sm">
|
||||
Already have an account?{" "}
|
||||
<Link href="login" className="underline">
|
||||
login
|
||||
</Link>
|
||||
{actionState.errors?.fieldErrors?.dob && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors?.fieldErrors?.dob}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</Form>
|
||||
<div>
|
||||
<label htmlFor="phone_number" className="text-sm">Phone Number</label>
|
||||
<Input
|
||||
id="phone-number"
|
||||
name="phone_number"
|
||||
maxLength={8}
|
||||
className={cn(!phoneNumberFromUrl && actionState.errors?.fieldErrors?.phone_number && "border-2 border-red-500 rounded-md",)}
|
||||
defaultValue={NUMBER_WITHOUT_DASH ?? ""}
|
||||
readOnly={Boolean(phoneNumberFromUrl)}
|
||||
placeholder={phoneNumberFromUrl ?? "Phone number"}
|
||||
/>
|
||||
</div>
|
||||
{actionState?.errors?.fieldErrors?.phone_number?.[0] && (
|
||||
<span className="text-sm inline-block text-red-500">
|
||||
{actionState.errors.fieldErrors.phone_number[0]}
|
||||
</span>
|
||||
)}
|
||||
<Button disabled={isPending} className="mt-4 w-full" type="submit">
|
||||
{isPending ? <Loader className="animate-spin" /> : "Submit"}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="mb-4 text-center text-sm">
|
||||
Already have an account?{" "}
|
||||
<Link href="login" className="underline">
|
||||
login
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -15,14 +15,13 @@ import { z } from "zod";
|
||||
const OTPSchema = z.object({
|
||||
pin: z.string().min(6, {
|
||||
message: "Your one-time password must be 6 characters.",
|
||||
|
||||
}),
|
||||
});
|
||||
|
||||
export default function VerifyOTPForm() {
|
||||
export default function VerifyOTPForm({ phone_number }: { phone_number: string }) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const router = useRouter();
|
||||
|
||||
console.log("verification in OTP form", phone_number)
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
@ -35,13 +34,13 @@ export default function VerifyOTPForm() {
|
||||
});
|
||||
|
||||
const onSubmit: SubmitHandler<z.infer<typeof OTPSchema>> = (data) => {
|
||||
console.log(data);
|
||||
|
||||
startTransition(async () => {
|
||||
const isVerified = await authClient.phoneNumber.verify({
|
||||
phoneNumber: "+9607780588",
|
||||
phoneNumber: phone_number,
|
||||
code: data.pin,
|
||||
});
|
||||
console.log({ isVerified });
|
||||
if (!isVerified.error) {
|
||||
router.push("/devices");
|
||||
} else {
|
||||
@ -53,9 +52,8 @@ export default function VerifyOTPForm() {
|
||||
return (
|
||||
<form
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
className="bg-white dark:bg-gray-900 w-full max-w-xs rounded-lg shadow my-4 "
|
||||
className="bg-white dark:bg-gray-900 w-full max-w-xs rounded-lg shadow my-4"
|
||||
>
|
||||
<h4 className="text-center rounded m-2 text-xl font-bold text-gray-600 bg-gray-200 py-4">Verify OTP</h4>
|
||||
<div className="grid pb-4 pt-2 gap-4 px-10">
|
||||
<div className="">
|
||||
<Label htmlFor="otp-number" className="text-gray-500">
|
||||
|
Reference in New Issue
Block a user