|
| 1 | +import AddChannel from '@/components/forms/AddChannel'; |
| 2 | +import Modal from '@/components/home/Modal'; |
| 3 | +import { |
| 4 | + CreateChannelInput, |
| 5 | + createChannelSchema, |
| 6 | +} from '@/types/client/channel'; |
| 7 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 8 | +import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'; |
| 9 | +import { useForm } from 'react-hook-form'; |
| 10 | +import { useSupabaseClient, useUser } from '@supabase/auth-helpers-react'; |
| 11 | +import { createChannel } from '@/services/channels.service'; |
| 12 | +import { PostgrestError } from '@supabase/supabase-js'; |
| 13 | +import { |
| 14 | + useGetUserPermsForServer, |
| 15 | + useUserPerms, |
| 16 | + useUserServerPerms, |
| 17 | +} from '@/lib/store'; |
| 18 | + |
| 19 | +export default function AddChannelModal({ |
| 20 | + showModal, |
| 21 | + setShowModal, |
| 22 | + serverId, |
| 23 | +}: { |
| 24 | + showModal: boolean; |
| 25 | + setShowModal: Dispatch<SetStateAction<boolean>>; |
| 26 | + serverId: number; |
| 27 | +}) { |
| 28 | + const addChannelRef = useRef<HTMLDialogElement>(null); |
| 29 | + |
| 30 | + const [serverError, setServerError] = useState<string>(''); |
| 31 | + const [showDesc, setSetShowDesc] = useState<boolean>(false); |
| 32 | + const [channelType, setChannelType] = useState<'media' | 'text'>('text'); |
| 33 | + |
| 34 | + const supabase = useSupabaseClient(); |
| 35 | + |
| 36 | + const user = useUser(); |
| 37 | + |
| 38 | + const getUserServerPerms = useGetUserPermsForServer(); |
| 39 | + |
| 40 | + const { |
| 41 | + register, |
| 42 | + handleSubmit, |
| 43 | + reset, |
| 44 | + control, |
| 45 | + formState: { errors }, |
| 46 | + } = useForm<CreateChannelInput>({ |
| 47 | + resolver: zodResolver(createChannelSchema), |
| 48 | + mode: 'onSubmit', |
| 49 | + defaultValues: { |
| 50 | + name: '', |
| 51 | + description: '', |
| 52 | + isMedia: false, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + if (getUserServerPerms) { |
| 58 | + if (user) { |
| 59 | + getUserServerPerms(supabase, serverId, user.id); |
| 60 | + } |
| 61 | + } |
| 62 | + }, [user, getUserServerPerms, supabase, serverId]); |
| 63 | + |
| 64 | + const onSubmit = async (formData: CreateChannelInput) => { |
| 65 | + const { data, error } = await createChannel( |
| 66 | + supabase, |
| 67 | + serverId, |
| 68 | + formData.name, |
| 69 | + formData.description, |
| 70 | + formData.isMedia |
| 71 | + ); |
| 72 | + |
| 73 | + if (error) { |
| 74 | + if ((error as PostgrestError).message) { |
| 75 | + setServerError((error as PostgrestError).message); |
| 76 | + } |
| 77 | + else { |
| 78 | + setServerError(error as string); |
| 79 | + } |
| 80 | + |
| 81 | + setTimeout(() => { |
| 82 | + setServerError(''); |
| 83 | + }, 7000); |
| 84 | + return; |
| 85 | + } |
| 86 | + else { |
| 87 | + addChannelRef.current?.close(); |
| 88 | + setChannelType('text'); |
| 89 | + setSetShowDesc(false); |
| 90 | + reset(); |
| 91 | + setShowModal(false); |
| 92 | + } |
| 93 | + }; |
| 94 | + |
| 95 | + return ( |
| 96 | + <Modal |
| 97 | + modalRef={addChannelRef} |
| 98 | + showModal={showModal} |
| 99 | + title={'Create a new Channel'} |
| 100 | + onKeyDown={(e) => { |
| 101 | + if (e.key === 'Enter') { |
| 102 | + handleSubmit(onSubmit)(); |
| 103 | + } |
| 104 | + }} |
| 105 | + buttons={ |
| 106 | + <> |
| 107 | + <div |
| 108 | + className="hover:underline hover:cursor-pointer" |
| 109 | + onClick={() => { |
| 110 | + setShowModal(false); |
| 111 | + setSetShowDesc(false); |
| 112 | + setChannelType('text'); |
| 113 | + reset(); |
| 114 | + addChannelRef.current?.close(); |
| 115 | + }} |
| 116 | + > |
| 117 | + Cancel |
| 118 | + </div> |
| 119 | + <div |
| 120 | + className="bg-frost-500 py-2 px-5 rounded-lg hover:cursor-pointer hover:bg-frost-700" |
| 121 | + onClick={() => { |
| 122 | + handleSubmit(onSubmit)(); |
| 123 | + }} |
| 124 | + > |
| 125 | + Submit |
| 126 | + </div> |
| 127 | + </> |
| 128 | + } |
| 129 | + > |
| 130 | + <AddChannel |
| 131 | + register={register} |
| 132 | + errors={errors} |
| 133 | + serverError={serverError} |
| 134 | + showDesc={showDesc} |
| 135 | + setShowDesc={setSetShowDesc} |
| 136 | + control={control} |
| 137 | + channelType={channelType} |
| 138 | + setChannelType={setChannelType} |
| 139 | + /> |
| 140 | + </Modal> |
| 141 | + ); |
| 142 | +} |
0 commit comments