All files / app/(dashboard)/experience ExperienceManager.tsx

78.34% Statements 123/157
83.33% Branches 15/18
75% Functions 6/8
78.34% Lines 123/157

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 2171x                                           1x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x   37x 37x 37x   37x 3x   3x                                             3x   3x 3x 3x 3x 3x   3x                 3x 3x 3x 3x 3x   3x 3x 3x   37x 5x 1x 1x   4x 4x   4x   5x           5x 4x 4x 4x 4x 4x 4x   4x 5x   37x         37x 5x 5x 5x   37x 37x 37x 37x 37x 37x 37x   37x 35x 35x   35x   37x     37x 37x 37x 37x       37x 2x 2x 2x 2x 2x       37x 35x 35x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x 68x   68x 68x 68x 68x 68x 68x 68x 35x 35x   2x 2x 2x 2x 2x   2x 2x 2x 2x   2x   2x 2x       37x 37x 37x 1x 1x 1x 37x 37x 37x 37x   37x  
'use client';
 
import { useState } from 'react';
import { useRouter } from 'next/navigation';
import { Plus, Briefcase, Pencil, Trash2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { ExperienceCard } from '@/components/experience/ExperienceCard';
import { ExperienceForm } from '@/components/experience/ExperienceForm';
import { LimitReached } from '@/components/subscription/LimitReached';
import { useToast } from '@/hooks/use-toast';
import { createClient } from '@/lib/supabase/client';
import { canAddMore, getTierLimits, type SubscriptionTier } from '@/lib/subscription/tiers';
import type { WorkExperience } from '@/types/database';
import type { WorkExperienceFormData } from '@/lib/validations/experience';
 
interface ExperienceManagerProps {
  profileId: string;
  experiences: WorkExperience[];
  tier: string;
}
 
export function ExperienceManager({
  profileId,
  experiences: initialExperiences,
  tier,
}: ExperienceManagerProps) {
  const router = useRouter();
  const { toast } = useToast();
  const [experiences, setExperiences] = useState(initialExperiences);
  const [isFormOpen, setIsFormOpen] = useState(false);
  const [editingExperience, setEditingExperience] = useState<WorkExperience | null>(null);
  const [isDeleting, setIsDeleting] = useState<string | null>(null);
 
  const subscriptionTier = tier as SubscriptionTier;
  const limits = getTierLimits(subscriptionTier);
  const canAdd = canAddMore(subscriptionTier, 'workExperiences', experiences.length);
 
  async function handleSubmit(data: WorkExperienceFormData) {
    const supabase = createClient();
 
    if (editingExperience) {
      // Update existing experience
      const { error } = await supabase
        .from('work_experiences')
        .update({
          ...data,
          updated_at: new Date().toISOString(),
        })
        .eq('id', editingExperience.id);
 
      if (error) {
        toast({
          title: 'Error',
          description: 'Failed to update experience',
          variant: 'destructive',
        });
        throw error;
      }
 
      toast({
        title: 'Experience updated',
        description: 'Your work experience has been updated.',
      });
    } else {
      // Create new experience
      const { error } = await supabase.from('work_experiences').insert({
        profile_id: profileId,
        ...data,
        display_order: experiences.length,
      });
 
      if (error) {
        toast({
          title: 'Error',
          description: 'Failed to add experience',
          variant: 'destructive',
        });
        throw error;
      }
 
      toast({
        title: 'Experience added',
        description: 'Your work experience has been added.',
      });
    }
 
    setEditingExperience(null);
    router.refresh();
  }
 
  async function handleDelete(id: string) {
    if (!confirm('Are you sure you want to delete this experience?')) {
      return;
    }
 
    setIsDeleting(id);
    const supabase = createClient();
 
    const { error } = await supabase.from('work_experiences').delete().eq('id', id);
 
    if (error) {
      toast({
        title: 'Error',
        description: 'Failed to delete experience',
        variant: 'destructive',
      });
    } else {
      setExperiences(experiences.filter((e) => e.id !== id));
      toast({
        title: 'Experience deleted',
        description: 'Your work experience has been removed.',
      });
    }
 
    setIsDeleting(null);
  }
 
  function openEditForm(experience: WorkExperience) {
    setEditingExperience(experience);
    setIsFormOpen(true);
  }
 
  function openAddForm() {
    setEditingExperience(null);
    setIsFormOpen(true);
  }
 
  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-3xl font-bold">Work Experience</h1>
          <p className="text-muted-foreground">Showcase your professional journey</p>
        </div>
 
        {canAdd && (
          <Button onClick={openAddForm}>
            <Plus className="mr-2 h-4 w-4" />
            Add Experience
          </Button>
        )}
      </div>
 
      {/* Limit Info */}
      {limits.maxWorkExperiences !== Infinity && (
        <p className="text-sm text-muted-foreground">
          {experiences.length} / {limits.maxWorkExperiences} experiences used
        </p>
      )}
 
      {/* Limit Reached Warning */}
      {!canAdd && (
        <LimitReached
          itemType="work experiences"
          currentTier={subscriptionTier}
          limit={limits.maxWorkExperiences}
        />
      )}
 
      {/* Experiences List */}
      {experiences.length > 0 ? (
        <div className="space-y-4">
          {experiences.map((experience) => (
            <Card key={experience.id} className="group relative">
              <div className="absolute right-4 top-4 z-10 flex gap-2 opacity-0 transition-opacity group-hover:opacity-100">
                <Button variant="outline" size="sm" onClick={() => openEditForm(experience)}>
                  <Pencil className="h-4 w-4" />
                </Button>
                <Button
                  variant="outline"
                  size="sm"
                  onClick={() => handleDelete(experience.id)}
                  disabled={isDeleting === experience.id}
                  className="text-destructive hover:text-destructive"
                >
                  <Trash2 className="h-4 w-4" />
                </Button>
              </div>
              <CardContent className="pt-6">
                <ExperienceCard experience={experience} />
              </CardContent>
            </Card>
          ))}
        </div>
      ) : (
        <Card>
          <CardContent className="flex flex-col items-center justify-center py-12">
            <Briefcase className="h-12 w-12 text-muted-foreground/50" />
            <h3 className="mt-4 text-lg font-semibold">No work experience yet</h3>
            <p className="mt-2 max-w-sm text-center text-sm text-muted-foreground">
              Add your work history to showcase your professional experience to potential clients.
            </p>
            {canAdd && (
              <Button onClick={openAddForm} className="mt-4">
                <Plus className="mr-2 h-4 w-4" />
                Add Your First Experience
              </Button>
            )}
          </CardContent>
        </Card>
      )}
 
      {/* Form Dialog */}
      <ExperienceForm
        open={isFormOpen}
        onOpenChange={(open) => {
          setIsFormOpen(open);
          if (!open) setEditingExperience(null);
        }}
        experience={editingExperience}
        onSubmit={handleSubmit}
      />
    </div>
  );
}