All files / src/features/setting/components profile-section.tsx

100% Statements 7/7
87.5% Branches 21/24
100% Functions 2/2
100% Lines 7/7

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 156 157 158 159 160 161                                    1x 28x   28x                                     1x                             28x       28x         28x                                                                                                                                                                                                
import { useActionState } from 'react';
import { useFormStatus } from 'react-dom';
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from '@/components/collapse';
import { Button } from '@/components/button';
import { Input } from '@/components/input';
import { Select } from '@/components/select';
import { UploadAvatar } from '@/components/upload-avatar';
import { FormMessage } from '@/components/form-message';
import { Heading } from '@/components/heading';
import { Text } from '@/components/text';
import { useProfileSettings } from '../hooks/use-profile-settings';
import { PROFILE_SECTION } from '@/constants/setting';
 
// Submit button component using useFormStatus hook
const SubmitButton = () => {
  const { pending } = useFormStatus();
 
  return (
    <div className="flex justify-center mt-6 mb-6">
      <Button
        type="submit"
        className="bg-gradient-vertical hover:opacity-90 text-white px-6 py-2"
        disabled={pending}
      >
        {pending
          ? PROFILE_SECTION.BUTTONS.SAVE.LOADING_TEXT
          : PROFILE_SECTION.BUTTONS.SAVE.TEXT}
      </Button>
    </div>
  );
};
 
interface ProfileSectionProps {
  onProfileDataLoaded?: (profileData: any) => void;
}
 
export const ProfileSection = ({
  onProfileDataLoaded,
}: ProfileSectionProps) => {
  const {
    control,
    error,
    success,
    avatarUrl,
    newAvatarFile,
    isLoading,
    isSubmitting,
    handleAvatarChange,
    dismissError,
    dismissSuccess,
    profileAction,
  } = useProfileSettings({
    onProfileDataLoaded,
  });
 
  const [actionState, formAction] = useActionState(profileAction, {
    success: false,
    error: null,
  });
 
  return (
    <Collapsible defaultOpen className="border-b border-gray-200">
      <CollapsibleTrigger className="w-full text-left border-0 p-6">
        <Heading as="h3" size="h5">
          {PROFILE_SECTION.TITLE}
        </Heading>
      </CollapsibleTrigger>
      <CollapsibleContent className="border-0 p-0">
        <div className="w-[362px] mx-auto py-6">
          {/* Error Message */}
          {(error || actionState.error) && (
            <FormMessage
              type="error"
              message={error || actionState.error || 'An error occurred'}
              onDismiss={dismissError}
            />
          )}
 
          {/* Success Message */}
          {(success || actionState.success) && (
            <FormMessage
              type="success"
              message={success || 'Profile updated successfully!'}
              onDismiss={dismissSuccess}
            />
          )}
 
          {/* Avatar Upload */}
          <div className="flex flex-col items-center space-y-4 py-6">
            <UploadAvatar
              value={newAvatarFile || avatarUrl || undefined}
              onValueChange={handleAvatarChange}
              size="lg"
              disabled={isLoading || isSubmitting}
            />
            <Text size="sm">{PROFILE_SECTION.FIELDS.AVATAR.NOTE}</Text>
          </div>
 
          {/* Profile Form */}
          <form action={formAction} className="flex flex-col gap-6">
            <Input
              name="name"
              control={control as any}
              id="name"
              label={PROFILE_SECTION.FIELDS.NAME.LABEL}
              placeholder={PROFILE_SECTION.FIELDS.NAME.PLACEHOLDER}
              className="h-[50px]"
              disabled={isLoading || isSubmitting}
            />
 
            <>
              <Input
                name="email"
                control={control as any}
                id="email"
                type="email"
                label={PROFILE_SECTION.FIELDS.EMAIL.LABEL}
                required
                placeholder={PROFILE_SECTION.FIELDS.EMAIL.PLACEHOLDER}
                className="h-[50px]"
                disabled={true}
              />
              <Text size="xs" variant="muted" className="mt-1">
                {PROFILE_SECTION.FIELDS.EMAIL.ERROR_MESSAGE}
              </Text>
            </>
 
            <Input
              name="phone"
              control={control as any}
              id="phone"
              label={PROFILE_SECTION.FIELDS.PHONE.LABEL}
              required
              placeholder={PROFILE_SECTION.FIELDS.PHONE.PLACEHOLDER}
              className="h-[50px]"
              disabled={isLoading || isSubmitting}
            />
 
            <Select
              name="nationality"
              control={control as any}
              id="nationality"
              label={PROFILE_SECTION.FIELDS.NATIONALITY.LABEL}
              placeholder={PROFILE_SECTION.FIELDS.NATIONALITY.PLACEHOLDER}
              options={PROFILE_SECTION.FIELDS.NATIONALITY.OPTIONS}
              disabled={isLoading || isSubmitting}
            />
 
            {/* Submit Button using useFormStatus */}
            <SubmitButton />
          </form>
        </div>
      </CollapsibleContent>
    </Collapsible>
  );
};