All files / components/experience ExperienceForm.tsx

91.6% Statements 251/274
74.19% Branches 23/31
52.94% Functions 9/17
91.6% Lines 251/274

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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 3631x                                                                                   1x 84x 84x 84x   84x 84x 84x 84x 84x 84x 84x 84x 84x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 74x 74x 74x 74x 74x 74x 74x 74x 84x   84x 84x 84x   84x                   84x 2x 2x 2x 2x 2x   84x             84x 1x 1x 1x 1x 1x   84x             84x 84x 84x 84x 84x 84x 84x   84x   84x 84x 84x 84x 84x     84x   84x 84x 84x 84x 84x 84x 84x 84x     84x 84x     84x 84x 84x 84x 84x 84x       84x 84x 84x 84x 84x 420x 420x 420x 84x 84x 84x 84x   84x 84x 84x 84x 84x       84x 84x 84x 84x 84x 252x 252x 252x 84x 84x 84x 84x 84x     84x 84x 84x 84x 84x   84x 84x 84x 84x 84x 84x 84x 84x 84x     84x 84x 84x 84x 84x     84x   84x 84x 84x 84x 84x     84x 84x 84x 84x 84x 1x 1x 1x 84x 84x 84x     84x 84x 84x 84x 84x 84x 84x 84x 84x     84x 84x 84x 84x 84x 84x 84x 84x 32x 2x 2x 2x 32x 84x 84x 84x 84x 84x 84x 12x 12x 22x 22x 22x   22x 22x 22x 22x 22x   22x 22x 22x 12x 12x   84x     84x 84x 84x 84x 84x 84x 84x 84x 11x 1x 1x 1x 11x 84x 84x 84x 84x 84x 84x 11x 11x 21x 21x 21x   21x 21x 21x 21x 21x   21x 21x 21x 11x 11x   84x     84x 84x 84x 84x 84x 84x 84x 84x     84x 84x   84x 84x 84x 84x 84x 84x 84x 84x   84x  
'use client';
 
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { X, Plus } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import { Switch } from '@/components/ui/switch';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';
import {
  workExperienceSchema,
  type WorkExperienceFormData,
  employmentTypes,
  employmentTypeLabels,
  locationTypes,
  locationTypeLabels,
} from '@/lib/validations/experience';
import type { WorkExperience } from '@/types/database';
 
interface ExperienceFormProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  experience?: WorkExperience | null;
  onSubmit: (data: WorkExperienceFormData) => Promise<void>;
}
 
export function ExperienceForm({ open, onOpenChange, experience, onSubmit }: ExperienceFormProps) {
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [achievementInput, setAchievementInput] = useState('');
  const [skillInput, setSkillInput] = useState('');
 
  const {
    register,
    handleSubmit,
    watch,
    setValue,
    formState: { errors },
  } = useForm<WorkExperienceFormData>({
    resolver: zodResolver(workExperienceSchema),
    defaultValues: experience
      ? {
          company_name: experience.company_name,
          company_logo_url: experience.company_logo_url,
          company_website: experience.company_website,
          role_title: experience.role_title,
          employment_type: experience.employment_type,
          location: experience.location,
          location_type: experience.location_type,
          start_date: experience.start_date,
          end_date: experience.end_date,
          is_current: experience.is_current,
          description: experience.description,
          achievements: experience.achievements || [],
          skills: experience.skills || [],
          is_visible: experience.is_visible,
        }
      : {
          employment_type: 'full_time',
          location_type: 'remote',
          is_current: false,
          achievements: [],
          skills: [],
          is_visible: true,
        },
  });
 
  const isCurrent = watch('is_current');
  const achievements = watch('achievements') || [];
  const skills = watch('skills') || [];
 
  async function handleFormSubmit(data: WorkExperienceFormData) {
    setIsSubmitting(true);
    try {
      await onSubmit(data);
      onOpenChange(false);
    } finally {
      setIsSubmitting(false);
    }
  }
 
  function addAchievement() {
    if (achievementInput.trim()) {
      setValue('achievements', [...achievements, achievementInput.trim()]);
      setAchievementInput('');
    }
  }
 
  function removeAchievement(index: number) {
    setValue(
      'achievements',
      achievements.filter((_, i) => i !== index)
    );
  }
 
  function addSkill() {
    if (skillInput.trim() && !skills.includes(skillInput.trim())) {
      setValue('skills', [...skills, skillInput.trim()]);
      setSkillInput('');
    }
  }
 
  function removeSkill(skill: string) {
    setValue(
      'skills',
      skills.filter((s) => s !== skill)
    );
  }
 
  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-h-[90vh] max-w-2xl overflow-y-auto">
        <DialogHeader>
          <DialogTitle>{experience ? 'Edit Experience' : 'Add Work Experience'}</DialogTitle>
          <DialogDescription>Add details about your work experience</DialogDescription>
        </DialogHeader>
 
        <form onSubmit={handleSubmit(handleFormSubmit)} className="space-y-6">
          {/* Company & Role */}
          <div className="grid gap-4 sm:grid-cols-2">
            <div className="space-y-2">
              <Label htmlFor="company_name">Company Name *</Label>
              <Input id="company_name" {...register('company_name')} placeholder="Company name" />
              {errors.company_name && (
                <p className="text-xs text-destructive">{errors.company_name.message}</p>
              )}
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="role_title">Role Title *</Label>
              <Input
                id="role_title"
                {...register('role_title')}
                placeholder="e.g., Software Engineer"
              />
              {errors.role_title && (
                <p className="text-xs text-destructive">{errors.role_title.message}</p>
              )}
            </div>
          </div>
 
          {/* Employment Type & Location Type */}
          <div className="grid gap-4 sm:grid-cols-2">
            <div className="space-y-2">
              <Label>Employment Type</Label>
              <Select
                defaultValue={watch('employment_type')}
                onValueChange={(v) =>
                  setValue('employment_type', v as (typeof employmentTypes)[number])
                }
              >
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {employmentTypes.map((type) => (
                    <SelectItem key={type} value={type}>
                      {employmentTypeLabels[type]}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
 
            <div className="space-y-2">
              <Label>Work Location</Label>
              <Select
                defaultValue={watch('location_type')}
                onValueChange={(v) =>
                  setValue('location_type', v as (typeof locationTypes)[number])
                }
              >
                <SelectTrigger>
                  <SelectValue />
                </SelectTrigger>
                <SelectContent>
                  {locationTypes.map((type) => (
                    <SelectItem key={type} value={type}>
                      {locationTypeLabels[type]}
                    </SelectItem>
                  ))}
                </SelectContent>
              </Select>
            </div>
          </div>
 
          {/* Location & Website */}
          <div className="grid gap-4 sm:grid-cols-2">
            <div className="space-y-2">
              <Label htmlFor="location">Location</Label>
              <Input id="location" {...register('location')} placeholder="e.g., New York, NY" />
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="company_website">Company Website</Label>
              <Input
                id="company_website"
                {...register('company_website')}
                placeholder="https://company.com"
              />
            </div>
          </div>
 
          {/* Dates */}
          <div className="grid gap-4 sm:grid-cols-2">
            <div className="space-y-2">
              <Label htmlFor="start_date">Start Date *</Label>
              <Input id="start_date" type="date" {...register('start_date')} />
              {errors.start_date && (
                <p className="text-xs text-destructive">{errors.start_date.message}</p>
              )}
            </div>
 
            <div className="space-y-2">
              <Label htmlFor="end_date">End Date</Label>
              <Input id="end_date" type="date" {...register('end_date')} disabled={isCurrent} />
            </div>
          </div>
 
          {/* Current Role Toggle */}
          <div className="flex items-center gap-2">
            <Switch
              id="is_current"
              checked={isCurrent}
              onCheckedChange={(checked) => {
                setValue('is_current', checked);
                if (checked) setValue('end_date', null);
              }}
            />
            <Label htmlFor="is_current">I currently work here</Label>
          </div>
 
          {/* Description */}
          <div className="space-y-2">
            <Label htmlFor="description">Description</Label>
            <Textarea
              id="description"
              {...register('description')}
              placeholder="Describe your role and responsibilities..."
              rows={4}
            />
          </div>
 
          {/* Achievements */}
          <div className="space-y-2">
            <Label>Key Achievements</Label>
            <div className="flex gap-2">
              <Input
                value={achievementInput}
                onChange={(e) => setAchievementInput(e.target.value)}
                placeholder="Add an achievement..."
                onKeyDown={(e) => {
                  if (e.key === 'Enter') {
                    e.preventDefault();
                    addAchievement();
                  }
                }}
              />
              <Button type="button" variant="outline" onClick={addAchievement}>
                <Plus className="h-4 w-4" />
              </Button>
            </div>
            {achievements.length > 0 && (
              <ul className="mt-2 space-y-1">
                {achievements.map((achievement, i) => (
                  <li
                    key={i}
                    className="flex items-center gap-2 rounded bg-muted px-3 py-1 text-sm"
                  >
                    <span className="flex-1">{achievement}</span>
                    <button
                      type="button"
                      onClick={() => removeAchievement(i)}
                      className="text-muted-foreground hover:text-destructive"
                    >
                      <X className="h-3.5 w-3.5" />
                    </button>
                  </li>
                ))}
              </ul>
            )}
          </div>
 
          {/* Skills */}
          <div className="space-y-2">
            <Label>Skills Used</Label>
            <div className="flex gap-2">
              <Input
                value={skillInput}
                onChange={(e) => setSkillInput(e.target.value)}
                placeholder="Add a skill..."
                onKeyDown={(e) => {
                  if (e.key === 'Enter') {
                    e.preventDefault();
                    addSkill();
                  }
                }}
              />
              <Button type="button" variant="outline" onClick={addSkill}>
                <Plus className="h-4 w-4" />
              </Button>
            </div>
            {skills.length > 0 && (
              <div className="mt-2 flex flex-wrap gap-2">
                {skills.map((skill) => (
                  <span
                    key={skill}
                    className="inline-flex items-center gap-1 rounded-full bg-muted px-3 py-1 text-sm"
                  >
                    {skill}
                    <button
                      type="button"
                      onClick={() => removeSkill(skill)}
                      className="text-muted-foreground hover:text-destructive"
                    >
                      <X className="h-3 w-3" />
                    </button>
                  </span>
                ))}
              </div>
            )}
          </div>
 
          {/* Visibility */}
          <div className="flex items-center gap-2">
            <Switch
              id="is_visible"
              checked={watch('is_visible')}
              onCheckedChange={(checked) => setValue('is_visible', checked)}
            />
            <Label htmlFor="is_visible">Show on public profile</Label>
          </div>
 
          {/* Actions */}
          <div className="flex justify-end gap-3">
            <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
              Cancel
            </Button>
            <Button type="submit" disabled={isSubmitting}>
              {isSubmitting ? 'Saving...' : experience ? 'Save Changes' : 'Add Experience'}
            </Button>
          </div>
        </form>
      </DialogContent>
    </Dialog>
  );
}