All files / src/hooks useUserProfile.ts

100% Statements 68/68
63.15% Branches 12/19
100% Functions 3/3
100% Lines 68/68

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 691x 1x 1x 1x 35x 35x 35x 35x 35x 35x 35x 14x 12x 12x 12x 10x 10x 10x 10x 10x 12x 1x 12x 12x 12x 14x 14x 14x 12x 12x 35x 35x 35x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 3x 3x 3x 3x 3x 5x 1x 1x 1x 5x 1x 1x 5x 5x 5x 35x 35x 35x 35x 35x 35x 35x 35x 35x 35x  
import { useState, useEffect } from "react";
import { clientLogger } from "@/lib/logging/clientLogger";
import { UserProfile, UseUserProfileReturn, UserMessage } from "@/types/user";
 
export function useUserProfile(isAuthenticated: boolean): UseUserProfileReturn {
  const [userProfile, setUserProfile] = useState<UserProfile | null>(null);
  const [loading, setLoading] = useState(true);
  const [savingProfile, setSavingProfile] = useState(false);
  const [profileMessage, setProfileMessage] = useState<UserMessage | null>(null);
 
  useEffect(() => {
    const fetchProfile = async () => {
      try {
        const response = await fetch("/api/user/profile");
        if (response.ok) {
          const result = await response.json();
          // Extract data from API response wrapper
          const data = result?.data ?? result;
          setUserProfile(data);
        }
      } catch (error) {
        clientLogger.error("Failed to fetch profile", error instanceof Error ? error : new Error(String(error)));
      } finally {
        setLoading(false);
      }
    };
 
    if (isAuthenticated) {
      fetchProfile();
    }
  }, [isAuthenticated]);
 
  const updateProfile = async (name: string, phone: string | null) => {
    setSavingProfile(true);
    setProfileMessage(null);
 
    try {
      const response = await fetch("/api/user/profile", {
        method: "PATCH",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ name, phone })});
 
      if (response.ok) {
        const result = await response.json();
        // Extract data from API response wrapper
        const updatedUser = result?.data ?? result;
        setUserProfile(updatedUser);
        setProfileMessage({ type: "success", text: "Profile updated successfully!" });
      } else {
        const error = await response.json();
        setProfileMessage({ type: "error", text: error.error || "Failed to update profile" });
      }
    } catch (error) {
      setProfileMessage({ type: "error", text: "An error occurred while updating profile" });
      clientLogger.error("Failed to update profile", error instanceof Error ? error : new Error(String(error)));
    } finally {
      setSavingProfile(false);
    }
  };
 
  return {
    userProfile,
    loading,
    savingProfile,
    profileMessage,
    updateProfile,
    setProfileMessage};
}