"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { api } from "@/lib/api"; import { useAuth } from "@/lib/auth"; import { useToast } from "@/lib/toast"; import { useI18n } from "@/lib/i18n"; import type { GigItem } from "@/lib/types"; import RingNext from "@/components/RingNext"; import { useRingSteps } from "@/lib/rings"; import { clearApplyDraft, clearSavedGigs, loadApplyDrafts, loadSavedGigs, saveApplyDraft, toggleSavedGig, type SavedGig, } from "@/lib/savedGigs"; export default function GigsClient({ gigs }: { gigs: GigItem[] }) { const { token } = useAuth(); const router = useRouter(); const { toast } = useToast(); const { t } = useI18n(); const rings = useRingSteps(); const [msg, setMsg] = useState>({}); const [applied, setApplied] = useState>({}); const [busyId, setBusyId] = useState(null); const [saved, setSaved] = useState([]); useEffect(() => { setSaved(loadSavedGigs()); setMsg(loadApplyDrafts()); }, []); const onToggleSave = (g: GigItem) => { const next = toggleSavedGig({ id: g.id, title: g.title, budget: g.budget, deadline: g.deadline, }); const now = next.some((x) => x.id === g.id); setSaved(next); toast(now ? t.gigs.saved : t.gigs.unsaved, "success"); }; const apply = async (id: string) => { if (!token) { router.push("/login?next=/gigs"); return; } const message = msg[id]?.trim(); if (!message) { toast(t.gigs.applyNeed, "info"); return; } setBusyId(id); try { await api.applyGig(token, id, message); setApplied((a) => ({ ...a, [id]: true })); clearApplyDraft(id); setMsg((m) => { const next = { ...m }; delete next[id]; return next; }); toast(t.gigs.applyOk, "success", { href: "/notifications", label: t.nav.notifications, }); } catch { toast(t.gigs.applyFail, "error"); } finally { setBusyId(null); } }; return (
{t.gigs.tag}

{t.gigs.title}

{t.gigs.subtitle}

{t.gigs.postTitle}
{saved.length > 0 && (

{t.gigs.savedList} · {saved.length}

)} {gigs.length === 0 ? (

{t.gigs.empty}

{t.gigs.emptyHint}

{t.gigs.postTitle} {t.nav.digital}
) : (
{!token && (

{t.nav.login} · {t.gigs.loginFirst}

)} {gigs.map((g) => { const isSaved = saved.some((s) => s.id === g.id); return (

{g.title}

{g.description}

{g.poster} · {g.budget} · {g.deadline}

{applied[g.id] ? (

{t.gigs.applied}

) : !token ? ( {t.gigs.apply} ) : ( <>