2024-02-16 01:49:19 +00:00
|
|
|
'use client';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-10-14 03:42:12 +00:00
|
|
|
import DOMPurify from 'dompurify';
|
2024-02-16 01:49:19 +00:00
|
|
|
import Image from 'next/image';
|
|
|
|
import Link from 'next/link';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { useFormStatus } from 'react-dom';
|
|
|
|
import { Check, Loader2 } from 'lucide-react';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
2024-02-16 01:49:19 +00:00
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
import { defaultImages } from '@/constants/images';
|
|
|
|
import { FormErrors } from './form-errors';
|
2024-02-15 02:30:10 +00:00
|
|
|
|
|
|
|
interface FormPickerProps {
|
|
|
|
id: string;
|
|
|
|
errors?: Record<string, string[] | undefined>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const FormPicker = ({ id, errors }: FormPickerProps) => {
|
|
|
|
const { pending } = useFormStatus();
|
|
|
|
|
|
|
|
const [images, setImages] =
|
|
|
|
useState<Array<Record<string, any>>>(defaultImages);
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const [selectedImageId, setSelectedImageId] = useState(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchImages = async () => {
|
|
|
|
try {
|
2024-02-18 00:21:19 +00:00
|
|
|
const response = await fetch('/api/unsplash');
|
|
|
|
if (response.ok) {
|
|
|
|
const newImages = await response.json();
|
2024-02-15 02:30:10 +00:00
|
|
|
setImages(newImages);
|
|
|
|
} else {
|
2024-02-18 00:21:19 +00:00
|
|
|
console.error('Failed to fetch images');
|
2024-02-15 02:30:10 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
setImages(defaultImages);
|
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchImages();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
return (
|
2024-02-16 01:49:19 +00:00
|
|
|
<div className='flex items-center justify-center p-6'>
|
|
|
|
<Loader2 className='h-6 w-6 animate-spin text-sky-700' />
|
2024-02-15 02:30:10 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-02-16 01:49:19 +00:00
|
|
|
<div className='relative'>
|
|
|
|
<div className='mb-2 grid grid-cols-3 gap-2'>
|
2024-02-15 02:30:10 +00:00
|
|
|
{images.map((image) => (
|
|
|
|
<div
|
|
|
|
key={image.id}
|
|
|
|
className={cn(
|
2024-02-16 01:49:19 +00:00
|
|
|
'group relative aspect-video cursor-pointer bg-muted transition hover:opacity-75',
|
|
|
|
pending && 'cursor-auto opacity-50 hover:opacity-50'
|
2024-02-15 02:30:10 +00:00
|
|
|
)}
|
|
|
|
onClick={() => {
|
|
|
|
if (pending) return;
|
|
|
|
setSelectedImageId(image.id);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<input
|
2024-02-16 01:49:19 +00:00
|
|
|
type='radio'
|
2024-02-15 02:30:10 +00:00
|
|
|
id={id}
|
|
|
|
name={id}
|
2024-02-16 01:49:19 +00:00
|
|
|
className='hidden'
|
2024-02-15 02:30:10 +00:00
|
|
|
checked={selectedImageId === image.id}
|
|
|
|
disabled={pending}
|
2024-02-18 00:21:19 +00:00
|
|
|
value={`${image.id}|${image.urls.thumb}|${image.urls.full}|${image.links.html}|${image.user.name}|${image.links.download_location}`}
|
2024-02-15 02:30:10 +00:00
|
|
|
/>
|
|
|
|
<Image
|
|
|
|
src={image.urls.thumb}
|
2024-02-16 01:49:19 +00:00
|
|
|
alt='Unsplash image'
|
|
|
|
className='rounded-sm object-cover'
|
2024-02-15 02:30:10 +00:00
|
|
|
fill
|
|
|
|
/>
|
|
|
|
{selectedImageId === image.id && (
|
2024-02-16 01:49:19 +00:00
|
|
|
<div className='absolute inset-y-0 flex h-full w-full items-center justify-center bg-black/30'>
|
|
|
|
<Check className='h-4 w-4 text-white' />
|
2024-02-15 02:30:10 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<Link
|
2024-02-21 00:53:12 +00:00
|
|
|
href={
|
2024-10-14 03:42:12 +00:00
|
|
|
DOMPurify.sanitize(image.user.links.html) +
|
|
|
|
'?utm_source=Tasko&utm_medium=referral'
|
2024-02-21 00:53:12 +00:00
|
|
|
}
|
2024-02-16 01:49:19 +00:00
|
|
|
target='_blank'
|
|
|
|
className='absolute bottom-0 w-full truncate bg-black/50 p-1 text-[10px] text-white opacity-0 hover:underline group-hover:opacity-100'
|
2024-02-15 02:30:10 +00:00
|
|
|
>
|
|
|
|
{image.user.name}
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2024-02-16 01:49:19 +00:00
|
|
|
<FormErrors id='image' errors={errors} />
|
2024-02-15 02:30:10 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|