All files / src/hooks useCompanyProfile.ts

100% Statements 27/27
87.5% Branches 7/8
100% Functions 4/4
100% Lines 25/25

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        21x 21x 21x   21x 10x 2x 2x     8x 8x 8x 8x   6x 5x     1x 1x   7x       21x 3x 2x   2x 1x   1x       21x 10x     21x              
import { useState, useEffect, useCallback } from "react";
import { companyProfileApi, type CompanyProfile } from "../lib/api";
 
export function useCompanyProfile(proId: string | null) {
	const [isLoading, setIsLoading] = useState(true);
	const [profile, setProfile] = useState<CompanyProfile | null>(null);
	const [error, setError] = useState("");
 
	const loadProfile = useCallback(async () => {
		if (!proId) {
			setIsLoading(false);
			return;
		}
 
		try {
			setIsLoading(true);
			setError("");
			const profileRes = await companyProfileApi.getCompanyProfile(proId);
 
			if (profileRes.data) {
				setProfile(profileRes.data);
			}
		} catch (err) {
			console.error("Failed to load profile:", err);
			setError("Failed to load company profile");
		} finally {
			setIsLoading(false);
		}
	}, [proId]);
 
	const refreshProfile = useCallback(async () => {
		if (!proId) return;
		try {
			const profileRes =
				await companyProfileApi.getCompanyProfile(proId);
			Eif (profileRes.data) setProfile(profileRes.data);
		} catch (err) {
			console.error("Failed to refresh profile:", err);
		}
	}, [proId]);
 
	useEffect(() => {
		loadProfile();
	}, [loadProfile]);
 
	return {
		isLoading,
		profile,
		error,
		refreshProfile,
	};
}