File size: 4,235 Bytes
f5071ca |
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 |
"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<FieldValues>({
defaultValues: {
author: '',
title: '',
song: null,
image: null,
}
});
const onChange = (open: boolean) => {
if (!open) {
reset();
uploadModal.onClose();
}
}
const onSubmit: SubmitHandler<FieldValues> = 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 (
<Modal
title="Add a song"
description="Upload an mp3 file"
isOpen={uploadModal.isOpen}
onChange={onChange}
>
<form
onSubmit={handleSubmit(onSubmit)}
className="flex flex-col gap-y-4"
>
<Input
id="title"
disabled={isLoading}
{...register('title', { required: true })}
placeholder="Song title"
/>
<Input
id="author"
disabled={isLoading}
{...register('author', { required: true })}
placeholder="Song author"
/>
<div>
<div className="pb-1">
Select a song file
</div>
<Input
placeholder="test"
disabled={isLoading}
type="file"
accept=".mp3"
id="song"
{...register('song', { required: true })}
/>
</div>
<div>
<div className="pb-1">
Select an image
</div>
<Input
placeholder="test"
disabled={isLoading}
type="file"
accept="image/*"
id="image"
{...register('image', { required: true })}
/>
</div>
<Button disabled={isLoading} type="submit">
Create
</Button>
</form>
</Modal>
);
}
export default UploadModal; |