diff --git a/app/dashboard/career/[id]/client-page.tsx b/app/dashboard/career/[id]/client-page.tsx new file mode 100644 index 0000000..c7e4ff5 --- /dev/null +++ b/app/dashboard/career/[id]/client-page.tsx @@ -0,0 +1,353 @@ +"use client" + +import type React from "react" + +import { useEffect, useState } from "react" +import { useRouter } from "next/navigation" +import Link from "next/link" +import { ArrowLeft } from "lucide-react" + +import { AnimatedList } from "@/components/animated-list" +import { AnimatedSection } from "@/components/animated-section" +import { DashboardHeader } from "@/components/dashboard-header" +import { FileUpload } from "@/components/file-upload" +import { Button } from "@/components/ui/button" +import { + Card, + CardContent, + CardDescription, + CardFooter, + CardHeader, + CardTitle, +} from "@/components/ui/card" +import { Input } from "@/components/ui/input" +import { Label } from "@/components/ui/label" +import { Textarea } from "@/components/ui/textarea" +import { useToast } from "@/components/ui/use-toast" + +interface EditCareerPageClientProps { + id?: string +} + +interface CareerEntry { + id: string + position: string + company: string + startDate: string + endDate: string + description: string + location?: string | null + logoUrl?: string | null +} + +export default function EditCareerPageClient({ id }: EditCareerPageClientProps) { + const router = useRouter() + const { toast } = useToast() + const [isLoading, setIsLoading] = useState(true) + const [isSubmitting, setIsSubmitting] = useState(false) + const [careerEntry, setCareerEntry] = useState(null) + const [isPresentPosition, setIsPresentPosition] = useState(false) + const [logoUrl, setLogoUrl] = useState("") + + useEffect(() => { + if (id) { + return + } + + console.error("Missing career id in route params") + toast({ + title: "Invalid route", + description: "The career entry identifier is missing from the URL.", + variant: "destructive", + }) + setIsLoading(false) + }, [id, toast]) + + useEffect(() => { + if (!id) { + return + } + + async function fetchCareerEntry() { + try { + const response = await fetch(`/api/career/${id}`) + + if (!response.ok) { + throw new Error("Failed to fetch career entry") + } + + const data: { career: CareerEntry } = await response.json() + setCareerEntry(data.career) + setIsPresentPosition(data.career.endDate === "Present") + setLogoUrl(data.career.logoUrl || "") + } catch (error) { + console.error("Error fetching career entry:", error) + toast({ + title: "Error", + description: "Failed to load career entry details", + variant: "destructive", + }) + } finally { + setIsLoading(false) + } + } + + fetchCareerEntry() + }, [id, toast]) + + async function handleSubmit(event: React.FormEvent) { + event.preventDefault() + setIsSubmitting(true) + + if (!id) { + toast({ + title: "Missing career entry", + description: "We couldn't determine which career entry should be updated.", + variant: "destructive", + }) + setIsSubmitting(false) + return + } + + const formData = new FormData(event.currentTarget) + const position = formData.get("position") as string + const company = formData.get("company") as string + const startDate = formData.get("startDate") as string + const endDate = isPresentPosition ? "Present" : (formData.get("endDate") as string) + const description = formData.get("description") as string + const location = formData.get("location") as string + + try { + const response = await fetch(`/api/career/${id}`, { + method: "PUT", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + position, + company, + startDate, + endDate, + description, + location, + logoUrl, + }), + }) + + if (!response.ok) { + const error = await response.json() + throw new Error(error.error || "Failed to update career entry") + } + + toast({ + title: "Career entry updated", + description: "Your career entry has been updated successfully.", + }) + + router.push("/dashboard/career") + router.refresh() + } catch (error) { + console.error("Career entry update error:", error) + toast({ + title: "Update failed", + description: error instanceof Error ? error.message : "Something went wrong", + variant: "destructive", + }) + } finally { + setIsSubmitting(false) + } + } + + async function handleDelete() { + if (!confirm("Are you sure you want to delete this career entry?")) { + return + } + + if (!id) { + toast({ + title: "Missing career entry", + description: "We couldn't determine which career entry should be deleted.", + variant: "destructive", + }) + return + } + + try { + const response = await fetch(`/api/career/${id}`, { + method: "DELETE", + }) + + if (!response.ok) { + const error = await response.json() + throw new Error(error.error || "Failed to delete career entry") + } + + toast({ + title: "Career entry deleted", + description: "Your career entry has been deleted successfully.", + }) + + router.push("/dashboard/career") + router.refresh() + } catch (error) { + console.error("Career entry deletion error:", error) + toast({ + title: "Deletion failed", + description: error instanceof Error ? error.message : "Something went wrong", + variant: "destructive", + }) + } + } + + if (isLoading) { + return ( + + +
+

Loading...

+
+
+ ) + } + + if (!careerEntry) { + return ( + + +
+

The requested career entry could not be found.

+ + + +
+
+ ) + } + + const formatDateForInput = (dateString: string | null) => { + if (!dateString || dateString === "Present") return "" + const date = new Date(dateString) + return date.toISOString().split("T")[0] + } + + return ( + + + + + + + + + Position Details + Update the details of your career position. + +
+ + +
+ + +
+
+ + +
+
+ + +
+