"use client"; import uniqid from "uniqid"; import React, { useState } from 'react'; import { useSupabaseClient } from '@supabase/auth-helpers-react'; import { FieldValues, SubmitHandler, useForm } from 'react-hook-form'; import { toast } from "react-hot-toast"; import { useRouter } from "next/navigation"; import useUploadModal from '@/hooks/useUploadModal'; import { useUser } from "@/hooks/useUser"; import Modal from './Modal'; import Input from './Input'; import Button from './Button'; const UploadModal = () => { const [isLoading, setIsLoading] = useState(false); const uploadModal = useUploadModal(); const supabaseClient = useSupabaseClient(); const { user } = useUser(); const router = useRouter(); const { register, handleSubmit, reset, } = useForm({ defaultValues: { author: '', title: '', song: null, image: null, } }); const onChange = (open: boolean) => { if (!open) { reset(); uploadModal.onClose(); } } const onSubmit: SubmitHandler = async (values) => { try { setIsLoading(true); const imageFile = values.image?.[0]; const songFile = values.song?.[0]; if (!imageFile || !songFile || !user) { toast.error('Missing fields') return; } const uniqueID = uniqid(); // Upload song const { data: songData, error: songError } = await supabaseClient .storage .from('songs') .upload(`song-${values.title}-${uniqueID}`, songFile, { cacheControl: '3600', upsert: false }); if (songError) { setIsLoading(false); return toast.error('Failed song upload'); } // Upload image const { data: imageData, error: imageError } = await supabaseClient .storage .from('images') .upload(`image-${values.title}-${uniqueID}`, imageFile, { cacheControl: '3600', upsert: false }); if (imageError) { setIsLoading(false); return toast.error('Failed image upload'); } // Create record const { error: supabaseError } = await supabaseClient .from('songs') .insert({ user_id: user.id, title: values.title, author: values.author, image_path: imageData.path, song_path: songData.path }); if (supabaseError) { return toast.error(supabaseError.message); } router.refresh(); setIsLoading(false); toast.success('Song created!'); reset(); uploadModal.onClose(); } catch (error) { toast.error('Something went wrong'); } finally { setIsLoading(false); } } return (
Select a song file
Select an image
); } export default UploadModal;