All files / src/pages/admin pro-company-profile.tsx

97.77% Statements 88/90
84.9% Branches 90/106
100% Functions 17/17
98.87% Lines 88/89

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328                                                                307x 307x     307x 307x 307x 307x                         307x   307x 307x 307x   307x     307x 307x 307x 307x 307x     307x 307x 307x 307x 307x 307x 307x 307x     307x 307x 307x 307x 307x 307x     307x 307x 174x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x 56x       307x 7x 7x   7x   7x 7x   14x 14x   7x                                               6x 6x   1x     1x     7x       307x 62x             245x                                                                                                                                                 6x 2x             6x 1x             5x 2x                     6x     4x 4x 4x       1x 1x                       5x     4x 4x 4x       1x 1x                         5x     3x 4x 4x       1x 1x                
import { useState, useEffect, type FormEvent } from "react";
import { useParams } from "@tanstack/react-router";
import { useQueryClient } from "@tanstack/react-query";
 
import { Button } from "../../components/ui/button";
import { Breadcrumb } from "../../components/ui/breadcrumb";
import {
	adminApi,
	ApiError,
	GENERIC_ERROR_MESSAGE,
} from "../../lib/api";
import type { ProLeadership, ProCertification, ProTestimonial } from "../../lib/api";
import { notify } from "../../lib/notify";
import { Save } from "lucide-react";
import { BusinessInformationForm } from "../../components/company-profile/BusinessInformationForm";
import { SocialLinksForm } from "../../components/company-profile/SocialLinksForm";
import { LeadershipSection } from "../../components/company-profile/LeadershipSection";
import { CertificationsSection } from "../../components/company-profile/CertificationsSection";
import { TestimonialsSection } from "../../components/company-profile/TestimonialsSection";
import { LeadershipModal } from "../../components/company-profile/LeadershipModal";
import { CertificationModal } from "../../components/company-profile/CertificationModal";
import { TestimonialModal } from "../../components/company-profile/TestimonialModal";
import {
	useAdminProCompanyProfile,
	useAdminPro,
} from "../../hooks/queries/useAdminQueries";
import { useServiceCategories } from "../../hooks/queries/useTaxonomyQueries";
import { useProjects } from "../../hooks/queries/useProjectQueries";
import { queryKeys } from "../../lib/query-keys";
import { useProCompanyProfileHandlers } from "./use-pro-company-profile.hook";
 
export function AdminProCompanyProfilePage() {
	const { proId } = useParams({ strict: false });
	const queryClient = useQueryClient();
 
	// React Query hooks
	const { data: profileData, isLoading: profileLoading } = useAdminProCompanyProfile(proId ?? null);
	const { data: proData, isLoading: proLoading } = useAdminPro(proId ?? null);
	const { data: serviceCategories = [] } = useServiceCategories();
	const { data: projectsList = [] } = useProjects(proId ?? null);
 
	const {
		addLeader,
		updateLeader,
		deleteLeader,
		addCertification,
		updateCertification,
		deleteCertification,
		addTestimonial,
		updateTestimonial,
		deleteTestimonial,
		dialog: confirmDialog,
	} = useProCompanyProfileHandlers(proId);
 
	const profile = profileData ?? null;
	const proName = proData?.businessName ?? "Pro";
	const isLoading = profileLoading || proLoading;
 
	const [isSaving, setIsSaving] = useState(false);
 
	// Form state - Business Info
	const [tagline, setTagline] = useState("");
	const [about, setAbout] = useState("");
	const [establishedYear, setEstablishedYear] = useState("");
	const [teamSizeCategory, setTeamSizeCategory] = useState("");
	const [areasServed, setAreasServed] = useState("");
 
	// Form state - Social Links
	const [instagramHandle, setInstagramHandle] = useState("");
	const [facebookUrl, setFacebookUrl] = useState("");
	const [youtubeUrl, setYoutubeUrl] = useState("");
	const [googleBusinessUrl, setGoogleBusinessUrl] = useState("");
	const [websiteUrl, setWebsiteUrl] = useState("");
	const [linkedinUrl, setLinkedinUrl] = useState("");
	const [twitterUrl, setTwitterUrl] = useState("");
	const [pinterestUrl, setPinterestUrl] = useState("");
 
	// Modal state
	const [showLeadershipModal, setShowLeadershipModal] = useState(false);
	const [showCertificationModal, setShowCertificationModal] = useState(false);
	const [showTestimonialModal, setShowTestimonialModal] = useState(false);
	const [editingLeader, setEditingLeader] = useState<ProLeadership | null>(null);
	const [editingCert, setEditingCert] = useState<ProCertification | null>(null);
	const [editingTestimonial, setEditingTestimonial] = useState<ProTestimonial | null>(null);
 
	// Sync form state when profile data first loads (avoid re-initializing after user edits)
	const [formInitialized, setFormInitialized] = useState(false);
	useEffect(() => {
		if (profile && !formInitialized) {
			setTagline(profile.tagline || "");
			setAbout(profile.about || "");
			setEstablishedYear(profile.establishedYear?.toString() || "");
			setTeamSizeCategory(profile.teamSizeCategory || "");
			setAreasServed(profile.areasServed?.join(", ") || "");
			setInstagramHandle(profile.socialLinks.instagramHandle || "");
			setFacebookUrl(profile.socialLinks.facebookUrl || "");
			setYoutubeUrl(profile.socialLinks.youtubeUrl || "");
			setGoogleBusinessUrl(profile.socialLinks.googleBusinessUrl || "");
			setWebsiteUrl(profile.socialLinks.websiteUrl || "");
			setLinkedinUrl(profile.socialLinks.linkedinUrl || "");
			setTwitterUrl(profile.socialLinks.twitterUrl || "");
			setPinterestUrl(profile.socialLinks.pinterestUrl || "");
			setFormInitialized(true);
		}
	}, [profile, formInitialized]);
 
	const handleSaveBusinessInfo = async (e: FormEvent) => {
		e.preventDefault();
		Iif (!proId) return;
 
		setIsSaving(true);
 
		try {
			const areasArray = areasServed
				.split(",")
				.map((a) => a.trim())
				.filter((a) => a.length > 0);
 
			await adminApi.updateCompanyProfile(proId, {
				tagline: tagline || null,
				about: about || null,
				establishedYear: establishedYear
					? Number.parseInt(establishedYear, 10)
					: null,
				teamSizeCategory: (teamSizeCategory || null) as
					| "solo"
					| "2-5"
					| "6-10"
					| "11-25"
					| "25+"
					| null,
				areasServed: areasArray.length > 0 ? areasArray : null,
				instagramHandle: instagramHandle || null,
				facebookUrl: facebookUrl || null,
				youtubeUrl: youtubeUrl || null,
				googleBusinessUrl: googleBusinessUrl || null,
				websiteUrl: websiteUrl || null,
				linkedinUrl: linkedinUrl || null,
				twitterUrl: twitterUrl || null,
				pinterestUrl: pinterestUrl || null,
			});
 
			queryClient.invalidateQueries({ queryKey: queryKeys.admin.pros.companyProfile(proId) });
			notify.success("Profile updated successfully!");
		} catch (err) {
			Iif (err instanceof ApiError) {
				notify.error(err.message);
			} else {
				notify.error(GENERIC_ERROR_MESSAGE);
			}
		} finally {
			setIsSaving(false);
		}
	};
 
	if (isLoading) {
		return (
			<div className="flex items-center justify-center h-64">
				<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary-600" />
			</div>
		);
	}
 
	return (
		<>
			<div className="max-w-4xl mx-auto space-y-6">
				{/* Breadcrumb */}
				<Breadcrumb
					items={[
						{ label: "Pros", href: "/admin/pros" },
						{ label: proName, href: `/admin/pros/${proId}` },
						{ label: "Company Profile" },
					]}
				/>
 
				{/* Header */}
				<div>
					<h1 className="text-xl sm:text-2xl font-bold text-foreground-default">
						Company Profile
					</h1>
					<p className="mt-1 text-sm sm:text-base text-foreground-muted">
						Managing company profile for{" "}
						<span className="font-medium text-foreground-default">
							{proName}
						</span>
					</p>
				</div>
 
				{/* Business Info & Social Links Form */}
				<form onSubmit={handleSaveBusinessInfo} className="space-y-6">
					{/* Business Information */}
					<BusinessInformationForm
						tagline={tagline}
						setTagline={setTagline}
						about={about}
						setAbout={setAbout}
						establishedYear={establishedYear}
						setEstablishedYear={setEstablishedYear}
						teamSizeCategory={teamSizeCategory}
						setTeamSizeCategory={setTeamSizeCategory}
						areasServed={areasServed}
						setAreasServed={setAreasServed}
					/>
 
					{/* Social Links */}
					<SocialLinksForm
						instagramHandle={instagramHandle}
						setInstagramHandle={setInstagramHandle}
						facebookUrl={facebookUrl}
						setFacebookUrl={setFacebookUrl}
						youtubeUrl={youtubeUrl}
						setYoutubeUrl={setYoutubeUrl}
						googleBusinessUrl={googleBusinessUrl}
						setGoogleBusinessUrl={setGoogleBusinessUrl}
						websiteUrl={websiteUrl}
						setWebsiteUrl={setWebsiteUrl}
						linkedinUrl={linkedinUrl}
						setLinkedinUrl={setLinkedinUrl}
						twitterUrl={twitterUrl}
						setTwitterUrl={setTwitterUrl}
						pinterestUrl={pinterestUrl}
						setPinterestUrl={setPinterestUrl}
					/>
 
					{/* Save Button */}
					<div className="flex justify-end">
						<Button type="submit" isLoading={isSaving}>
							<Save className="h-4 w-4 mr-2" />
							Save Changes
						</Button>
					</div>
				</form>
 
				{/* Leadership Team */}
				<LeadershipSection
					leadershipTeam={profile?.leadershipTeam || []}
					onAdd={() => setShowLeadershipModal(true)}
					onEdit={(member) => setEditingLeader(member)}
					onDelete={deleteLeader}
				/>
 
				{/* Certifications & Awards */}
				<CertificationsSection
					certifications={profile?.certifications || []}
					onAdd={() => setShowCertificationModal(true)}
					onEdit={(cert) => setEditingCert(cert)}
					onDelete={deleteCertification}
				/>
 
				{/* Testimonials */}
				<TestimonialsSection
					testimonials={profile?.testimonials || []}
					onAdd={() => setShowTestimonialModal(true)}
					onEdit={(testimonial) => setEditingTestimonial(testimonial)}
					onDelete={deleteTestimonial}
				/>
			</div>
 
			{/* Leadership Modal */}
			{(showLeadershipModal || editingLeader) && proId && (
				<LeadershipModal
					member={editingLeader}
					proId={proId}
					onSave={async (data) => {
						const success = editingLeader
							? await updateLeader(editingLeader.id, data)
							: await addLeader(data);
						Eif (success) {
							setShowLeadershipModal(false);
							setEditingLeader(null);
						}
					}}
					onClose={() => {
						setShowLeadershipModal(false);
						setEditingLeader(null);
					}}
				/>
			)}
 
			{/* Certification Modal */}
			{(showCertificationModal || editingCert) && (
				<CertificationModal
					certification={editingCert}
					// biome-ignore lint/style/noNonNullAssertion: proId is guaranteed by route params
					proId={proId!}
					onSave={async (data) => {
						const success = editingCert
							? await updateCertification(editingCert.id, data)
							: await addCertification(data);
						Eif (success) {
							setShowCertificationModal(false);
							setEditingCert(null);
						}
					}}
					onClose={() => {
						setShowCertificationModal(false);
						setEditingCert(null);
					}}
				/>
			)}
 
			{/* Testimonial Modal */}
			{(showTestimonialModal || editingTestimonial) && (
				<TestimonialModal
					testimonial={editingTestimonial}
					serviceCategories={serviceCategories}
					projects={projectsList}
					proId={proId ?? ""}
					onSave={async (data) => {
						const success = editingTestimonial
							? await updateTestimonial(editingTestimonial.id, data)
							: await addTestimonial(data);
						if (success) {
							setShowTestimonialModal(false);
							setEditingTestimonial(null);
						}
					}}
					onClose={() => {
						setShowTestimonialModal(false);
						setEditingTestimonial(null);
					}}
				/>
			)}
			{confirmDialog}
		</>
	);
}