'use client'; import Image from 'next/image'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { useFormStatus } from 'react-dom'; import { Check, Loader2 } from 'lucide-react'; import { cn } from '@/lib/utils'; import { defaultImages } from '@/constants/images'; import { FormErrors } from './form-errors'; interface FormPickerProps { id: string; errors?: Record; } export const FormPicker = ({ id, errors }: FormPickerProps) => { const { pending } = useFormStatus(); const [images, setImages] = useState>>(defaultImages); const [isLoading, setIsLoading] = useState(true); const [selectedImageId, setSelectedImageId] = useState(null); useEffect(() => { const fetchImages = async () => { try { const response = await fetch('/api/unsplash'); if (response.ok) { const newImages = await response.json(); setImages(newImages); } else { console.error('Failed to fetch images'); } } catch (error) { console.log(error); setImages(defaultImages); } finally { setIsLoading(false); } }; fetchImages(); }, []); if (isLoading) { return (
); } return (
{images.map((image) => (
{ if (pending) return; setSelectedImageId(image.id); }} > Unsplash image {selectedImageId === image.id && (
)} {image.user.name}
))}
); };