import { createLazyFileRoute } from "@tanstack/react-router"; import { TbArrowNarrowRight } from "react-icons/tb"; import { useForm, Control, FieldError } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Button } from "@/shadcn/components/ui/button.tsx"; import { Form, FormControl, FormField, FormItem, FormLabel, } from "@/shadcn/components/ui/form.tsx"; import { Input } from "@/shadcn/components/ui/input.tsx"; import { useEffect, useState } from "react"; import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger } from "@/shadcn/components/ui/dropdown-menu"; import { HiOutlineGlobeAlt } from "react-icons/hi"; import { IoIosArrowUp } from "react-icons/io"; /// Define validation schema using zod const formSchema = z .object({ password: z.string().min(1, "Password is required"), confirm_password: z.string().min(1, "Password confirmation is required"), }) .refine((data) => data.password === data.confirm_password, { message: "Passwords do not match", path: ["confirm_password"], // Set error on confirm_password field }); type FormSchema = z.infer; // Interface for props of CustomFormField interface CustomFormFieldProps { name: keyof FormSchema; label: string; control: Control; type?: string; placeholder?: string; error?: FieldError; } // Component for form fields with bold labels const CustomFormField: React.FC = ({ name, label, control, type = "text", placeholder, error, }) => ( ( {label} {error &&

{error.message}

}
)} /> ); interface DropdownProps { onSelect: (selectedOption: string) => void; defaultOption?: string; listOption?: string[]; } const CustomDropdownMenu: React.FC = ({ onSelect, defaultOption = "", listOption = [], }) => { const [selectedOption, setSelectedOption] = useState(defaultOption); const handleSelect = (option: string) => { setSelectedOption(option); onSelect(option); }; return ( {listOption.map((option, index) => ( {option} ))} ); }; // Define a route for the registration form export const Route = createLazyFileRoute("/forgot-password/verify")({ component: () => (
), }); // Main component of the reset password form export function ResetPasswordForm() { const [token, setToken] = useState(null); // Set up form with react-hook-form and zod const form = useForm({ // Integrate schema with form resolver: zodResolver(formSchema), defaultValues: { password: "", confirm_password: "", }, }); // Use effect to get token from URL when component mounts useEffect(() => { const urlParams = new URLSearchParams(window.location.search); const tokenFromURL = urlParams.get("token"); setToken(tokenFromURL); }, []); // Function to handle form submission const onSubmit = async (values: FormSchema) => { try { if (!token) { alert("Token not found in URL"); return; } // Create URL with token as query parameter const urlWithToken = import.meta.env.VITE_BACKEND_BASE_URL + `/forgot-password/verify?token=${token}`; const response = await fetch(urlWithToken, { method: "PATCH", headers: { "Content-Type": "application/json", }, body: JSON.stringify(values), }); // Check status response if (response.ok) { // Periksa apakah respons memiliki konten const responseText = await response.text(); let data = {}; try { data = responseText ? JSON.parse(responseText) : {}; // Parsing respons hanya jika ada teks } catch (jsonError) { console.error("Error parsing JSON response:", jsonError); alert("Failed to parse server response"); } alert("Password reset successfully"); return data; } else { // Tangani kasus jika respons tidak OK const errorText = await response.text(); console.error("Server error:", errorText); alert("Failed to reset password"); } } catch (error) { console.error("Submission error:", error); alert("Failed to reset password"); } }; const handleSelect = (selectedOption: string) => { console.log('Selected option:', selectedOption); // Do something with selected option }; return (
{/* Top */}
Amati
{/* Center */}

Change Password

Enter your new password

); }