2022-03-05 18:22:30 +01:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useState } from 'react'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { Form, useForm } from 'react-component-form'
|
2022-03-05 18:22:30 +01:00
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
2022-08-28 18:26:56 +02:00
|
|
|
import classNames from 'clsx'
|
2022-04-09 11:09:51 +02:00
|
|
|
import axios from 'axios'
|
2022-08-28 18:26:56 +02:00
|
|
|
import type { HandleUseFormCallback } from 'react-component-form'
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
import { FormState } from '../../design/FormState'
|
|
|
|
import { useGuildMember } from '../../../contexts/GuildMember'
|
|
|
|
import { Input } from '../../design/Input'
|
|
|
|
import { Button } from '../../design/Button'
|
|
|
|
import { useAuthentication } from '../../../tools/authentication'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type {
|
2022-03-05 18:22:30 +01:00
|
|
|
Channel,
|
|
|
|
ChannelWithDefaultChannelId
|
|
|
|
} from '../../../models/Channel'
|
2022-08-31 21:44:33 +02:00
|
|
|
import { channelSchema } from '../../../models/Channel'
|
2022-04-09 13:48:48 +02:00
|
|
|
import { ConfirmPopup } from '../ConfirmPopup'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { useFormTranslation } from '../../../hooks/useFormTranslation'
|
|
|
|
|
|
|
|
const schema = {
|
|
|
|
name: channelSchema.name
|
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
export interface ChannelSettingsProps {
|
|
|
|
channel: Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ChannelSettings: React.FC<ChannelSettingsProps> = (props) => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const router = useRouter()
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
const { guild } = useGuildMember()
|
|
|
|
|
|
|
|
const { channel } = props
|
|
|
|
|
|
|
|
const [inputValues, setInputValues] = useState({
|
|
|
|
name: channel.name
|
|
|
|
})
|
|
|
|
|
2022-04-09 13:48:48 +02:00
|
|
|
const [confirmation, setConfirmation] = useState(false)
|
|
|
|
|
|
|
|
const handleConfirmation = (): void => {
|
|
|
|
return setConfirmation(!confirmation)
|
|
|
|
}
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
const {
|
2022-08-28 18:26:56 +02:00
|
|
|
handleUseForm,
|
2022-03-05 18:22:30 +01:00
|
|
|
fetchState,
|
|
|
|
message,
|
|
|
|
errors,
|
|
|
|
setFetchState,
|
2022-08-28 18:26:56 +02:00
|
|
|
setMessage
|
|
|
|
} = useForm(schema)
|
|
|
|
const { getFirstErrorTranslation } = useFormTranslation()
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2022-08-28 18:26:56 +02:00
|
|
|
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
2022-03-05 18:22:30 +01:00
|
|
|
try {
|
|
|
|
await authentication.api.put(`/channels/${channel.id}`, formData)
|
2022-08-28 18:26:56 +02:00
|
|
|
setInputValues(formData)
|
2022-03-05 18:22:30 +01:00
|
|
|
await router.push(`/application/${guild.id}/${channel.id}`)
|
|
|
|
return null
|
|
|
|
} catch (error) {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
value: 'errors:server-error'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChange: React.ChangeEventHandler<
|
|
|
|
HTMLInputElement | HTMLTextAreaElement
|
|
|
|
> = (event) => {
|
|
|
|
setInputValues((oldInputValues) => {
|
|
|
|
return {
|
|
|
|
...oldInputValues,
|
|
|
|
[event.target.name]: event.target.value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleDelete = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const { data } =
|
|
|
|
await authentication.api.delete<ChannelWithDefaultChannelId>(
|
|
|
|
`/channels/${channel.id}`
|
|
|
|
)
|
|
|
|
await router.push(`/application/${guild.id}/${data.defaultChannelId}`)
|
|
|
|
} catch (error) {
|
|
|
|
setFetchState('error')
|
2022-04-09 11:09:51 +02:00
|
|
|
if (axios.isAxiosError(error) && error.response?.status === 400) {
|
2022-08-28 18:26:56 +02:00
|
|
|
setMessage('application:delete-channel-only-one')
|
2022-04-09 11:09:51 +02:00
|
|
|
} else {
|
2022-08-28 18:26:56 +02:00
|
|
|
setMessage('errors:server-error')
|
2022-04-09 11:09:51 +02:00
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-04-09 13:48:48 +02:00
|
|
|
<>
|
|
|
|
<Form
|
2022-08-28 18:26:56 +02:00
|
|
|
onSubmit={handleUseForm(onSubmit)}
|
2022-04-09 13:48:48 +02:00
|
|
|
className='my-auto flex flex-col items-center justify-center py-12'
|
|
|
|
>
|
|
|
|
<div className='flex w-full flex-col items-center justify-center sm:w-fit lg:flex-row'>
|
|
|
|
<div className=' flex w-full flex-wrap items-center justify-center px-6 sm:w-max'>
|
|
|
|
<div className='mx-12 flex flex-col'>
|
|
|
|
<Input
|
|
|
|
name='name'
|
|
|
|
label={t('common:name')}
|
|
|
|
placeholder={t('common:name')}
|
|
|
|
className='!mt-0'
|
|
|
|
onChange={onChange}
|
|
|
|
value={inputValues.name}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.name)}
|
2022-04-09 13:48:48 +02:00
|
|
|
data-cy='channel-name-input'
|
|
|
|
/>
|
|
|
|
</div>
|
2022-03-05 18:22:30 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-04-09 13:48:48 +02:00
|
|
|
<div className='mt-12 flex flex-col items-center justify-center sm:w-fit'>
|
|
|
|
<div className='space-x-6'>
|
|
|
|
<Button type='submit' data-cy='button-save-channel-settings'>
|
|
|
|
{t('application:save')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type='button'
|
|
|
|
color='red'
|
|
|
|
onClick={handleConfirmation}
|
|
|
|
data-cy='button-delete-channel-settings'
|
|
|
|
>
|
|
|
|
{t('application:delete')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
<FormState state={fetchState} message={message} />
|
2022-03-05 18:22:30 +01:00
|
|
|
</div>
|
2022-04-09 13:48:48 +02:00
|
|
|
</Form>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'pointer-events-none invisible absolute z-50 flex h-full w-full items-center justify-center bg-black bg-opacity-90 opacity-0 backdrop-blur-md transition-all',
|
|
|
|
{ 'pointer-events-auto !visible !opacity-100': confirmation }
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<ConfirmPopup
|
|
|
|
className={classNames('relative top-8 transition-all', {
|
|
|
|
'!top-0': confirmation
|
|
|
|
})}
|
|
|
|
handleYes={handleDelete}
|
|
|
|
handleNo={handleConfirmation}
|
|
|
|
title={`${t('application:delete-the-channel')} ?`}
|
|
|
|
/>
|
2022-03-05 18:22:30 +01:00
|
|
|
</div>
|
2022-04-09 13:48:48 +02:00
|
|
|
</>
|
2022-03-05 18:22:30 +01:00
|
|
|
)
|
|
|
|
}
|