All files / src/components/features/account/MyAccount ProfileForm.tsx

100% Statements 155/155
96.77% Branches 30/31
100% Functions 4/4
100% Lines 155/155

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1561x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 124x 124x 124x 124x 124x 124x 152x 144x 144x 8x 124x 124x 124x 152x 144x 144x 144x 8x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 32x 28x 28x 28x 28x 32x 124x 124x 124x 4x 4x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x  
"use client";
 
import React, { useEffect } from "react";
import { z } from "zod";
import { UserProfile } from "@/types/user";
import { Button } from "@/components/ui";
import { FormInput, FormSelect } from "@/components/ui";
import { FormRow } from "@/components/ui/FormField";
import { useForm } from "@/lib/forms";
import { validators } from "@/lib/forms/validators";
 
// Profile form validation schema
const profileSchema = z.object({
  firstName: validators.name,
  lastName: validators.name,
  phone: validators.phone,
  country: z.string().min(1, "Country is required"),
});
 
type ProfileFormValues = z.infer<typeof profileSchema>;
 
interface ProfileFormProps {
  userProfile: UserProfile | null;
  savingProfile: boolean;
  onSubmit: (name: string, phone: string | null) => Promise<void>;
}
 
export default function ProfileForm({
  userProfile,
  savingProfile,
  onSubmit,
}: ProfileFormProps) {
  const getInitialFirstName = () => {
    if (userProfile?.name) {
      return userProfile.name.split(" ")[0] || "";
    }
    return "";
  };
 
  const getInitialLastName = () => {
    if (userProfile?.name) {
      const nameParts = userProfile.name.split(" ");
      return nameParts.slice(1).join(" ") || "";
    }
    return "";
  };
 
  const {
    errors,
    touched,
    isSubmitting,
    handleSubmit,
    getFieldProps,
    setValue,
  } = useForm<typeof profileSchema>({
    schema: profileSchema,
    initialValues: {
      firstName: getInitialFirstName(),
      lastName: getInitialLastName(),
      phone: userProfile?.phone || "",
      country: "Australia",
    },
  });
 
  // Update form when userProfile changes
  useEffect(() => {
    if (userProfile) {
      setValue("firstName", getInitialFirstName());
      setValue("lastName", getInitialLastName());
      setValue("phone", userProfile.phone || "");
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [userProfile?.name, userProfile?.phone]);
 
  const onFormSubmit = async (formValues: ProfileFormValues) => {
    const fullName = `${formValues.firstName} ${formValues.lastName}`.trim();
    await onSubmit(fullName, formValues.phone || null);
  };
 
  // Custom className to match existing design system
  const inputClassName =
    "rounded-md border-gray-3 dark:border-gray-600 bg-gray-1 dark:bg-gray-700 placeholder:text-dark-5 dark:placeholder:text-gray-400 dark:text-gray-200 py-2.5 px-5 focus:shadow-input";
 
  return (
    <form onSubmit={handleSubmit(onFormSubmit)}>
      <div className="border-b border-gray-3 dark:border-gray-700 pb-9.5 mb-9.5">
        <h3 className="font-medium text-xl text-dark dark:text-gray-100 mb-5.5">
          Personal Information
        </h3>
 
        <FormRow gap={6} className="gap-5.5">
          <FormInput
            label="First Name"
            type="text"
            placeholder="First Name"
            {...getFieldProps("firstName")}
            error={errors.firstName}
            touched={touched.firstName}
            required
            className={inputClassName}
          />
 
          <FormInput
            label="Last Name"
            type="text"
            placeholder="Last Name"
            {...getFieldProps("lastName")}
            error={errors.lastName}
            touched={touched.lastName}
            required
            className={inputClassName}
          />
        </FormRow>
 
        <div className="mt-5.5">
          <FormInput
            label="Phone Number"
            type="tel"
            placeholder="Phone Number"
            {...getFieldProps("phone")}
            error={errors.phone}
            touched={touched.phone}
            className={inputClassName}
          />
        </div>
 
        <div className="mt-5.5">
          <FormSelect
            label="Country/Region"
            options={[
              { value: "Australia", label: "Australia" },
              { value: "America", label: "America" },
              { value: "England", label: "England" },
            ]}
            {...getFieldProps("country")}
            error={errors.country}
            touched={touched.country}
            required
            className={inputClassName}
          />
        </div>
 
        <Button
          type="submit"
          variant="primary"
          size="md"
          loading={savingProfile || isSubmitting}
          className="mt-6"
        >
          Save Changes
        </Button>
      </div>
    </form>
  );
}