import type {Route} from "../../../.react-router/types/app/pages/dashboard_sub/+types/DashboardRegister"; import {flexLeftWeight, iconMStyle, marginRound, maxWidth} from "~/styles"; import { Button, Flex, Group, Image, Input, Divider, PasswordInput, Select, Stack, Text, TextInput, Space } from "@mantine/core"; import {useForm} from "@mantine/form"; import {useState} from "react"; import AccountIcon from "mdi-react/AccountIcon"; import LockIcon from "mdi-react/LockIcon"; import { useNavigate } from "react-router"; import {apiRegisterPatient} from "~/utils/hms_api"; import {dateToTimestamp, showErrorMessage, showInfoMessage} from "~/utils/utils"; import {DateInput} from "@mantine/dates"; import {DashboardPageType} from "~/utils/hms_enums.ts"; import CalendarRangeIcon from "mdi-react/CalendarRangeIcon"; import GenderMaleFemaleIcon from "mdi-react/GenderMaleFemaleIcon"; import CellphoneIcon from "mdi-react/CellphoneIcon"; import EmailIcon from "mdi-react/EmailIcon"; import HomeIcon from "mdi-react/HomeIcon"; export function meta({}: Route.MetaArgs) { return [ { title: "Patient Registration" }, { name: "description", content: "Patient registration page." }, ]; } export default function Component() { const form = useForm({ initialValues: { userName: '', password: '', password2: '', name: '', title: '', birth_date: new Date(2000, 1, 1), gender: '', phone: '+44 ', email: '', address: '', postcode: '' }, validate: { userName: (value) => { if (value.length < 6) return "Username must be at least 6 characters long."; if (/^\d+$/.test(value)) return "The username cannot be a pure number"; return null; }, password: (value) => (/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/.test(value) ? null : "The password must be at least 8 characters long and contain both letters and numbers."), password2: (value, others) => (value !== others.password ? "The two passwords do not match" : null), name: (value) => (value.length < 2 ? "Name is too short" : null), title: (value) => (value.length < 2 ? "Title is too short" : null), birth_date: (value) => (value > new Date() ? "Date of birth cannot be in the future" : null), gender: (value) => (value.length <= 0 ? "Please select your biological sex" : null), phone: (value) => (/^\+\d{1,3} \d{7,}$/.test(value) ? null : "Invalid phone number format"), email: (value) => (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? null : "Invalid email format"), address: (value) => (value.length < 5 ? "Address is too short" : null), postcode: (value) => (/^[A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2}$/.test(value) ? null : "Invalid postcode format"), }, }); const [isLoggingIn, setIsLoggingIn] = useState(false); const navigate = useNavigate(); const onClickRegister = (values: { userName: string, password: string, password2: string, name: string, title: string, birth_date: Date, gender: string, phone: string, email: string, address: string, postcode: string, }) => { setIsLoggingIn(true) const regData = { username: values.userName, password: values.password, name: values.name, title: values.title.endsWith(".") ? values.title : values.title + ".", birth_date: dateToTimestamp(values.birth_date), gender: values.gender, phone: values.phone, email: values.email, address: values.address, postcode: values.postcode, } // console.log(regData) apiRegisterPatient(regData) .then((res) => { if (!res.success) { showErrorMessage(res.message, "Registration failed") return } showInfoMessage("", "Registration successful", 3000) navigate(DashboardPageType.LoginPage) }) .catch((e) => showErrorMessage(e.toString(), "Registration error")) .finally(() => setIsLoggingIn(false)) } return ( Patient Register
onClickRegister(values))}> } {...form.getInputProps('userName')} /> } {...form.getInputProps('password')} /> } {...form.getInputProps('password2')} /> } {...form.getInputProps('name')} /> } {...form.getInputProps('title')} /> } {...form.getInputProps('birth_date')} />