2023-10-23 23:33:39 +02:00
|
|
|
import { useRouter } from "next/router"
|
|
|
|
import { useState } from "react"
|
|
|
|
import { Form, useForm } from "react-component-form"
|
|
|
|
import useTranslation from "next-translate/useTranslation"
|
|
|
|
import classNames from "clsx"
|
|
|
|
import axios from "axios"
|
|
|
|
import type { HandleUseFormCallback } from "react-component-form"
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2023-10-23 23:33:39 +02: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,
|
2023-10-23 23:33:39 +02:00
|
|
|
ChannelWithDefaultChannelId,
|
|
|
|
} from "../../../models/Channel"
|
|
|
|
import { channelSchema } from "../../../models/Channel"
|
|
|
|
import { ConfirmPopup } from "../ConfirmPopup"
|
|
|
|
import { useFormTranslation } from "../../../hooks/useFormTranslation"
|
2022-08-28 18:26:56 +02:00
|
|
|
|
|
|
|
const schema = {
|
2023-10-23 23:33:39 +02:00
|
|
|
name: channelSchema.name,
|
2022-08-28 18:26:56 +02:00
|
|
|
}
|
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({
|
2023-10-23 23:33:39 +02:00
|
|
|
name: channel.name,
|
2022-03-05 18:22:30 +01:00
|
|
|
})
|
|
|
|
|
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,
|
2023-10-23 23:33:39 +02:00
|
|
|
setMessage,
|
2022-08-28 18:26:56 +02:00
|
|
|
} = 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 {
|
2023-10-23 23:33:39 +02:00
|
|
|
type: "error",
|
|
|
|
message: "errors:server-error",
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onChange: React.ChangeEventHandler<
|
|
|
|
HTMLInputElement | HTMLTextAreaElement
|
|
|
|
> = (event) => {
|
|
|
|
setInputValues((oldInputValues) => {
|
|
|
|
return {
|
|
|
|
...oldInputValues,
|
2023-10-23 23:33:39 +02:00
|
|
|
[event.target.name]: event.target.value,
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleDelete = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const { data } =
|
|
|
|
await authentication.api.delete<ChannelWithDefaultChannelId>(
|
2023-10-23 23:33:39 +02:00
|
|
|
`/channels/${channel.id}`,
|
2022-03-05 18:22:30 +01:00
|
|
|
)
|
|
|
|
await router.push(`/application/${guild.id}/${data.defaultChannelId}`)
|
|
|
|
} catch (error) {
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("error")
|
2022-04-09 11:09:51 +02:00
|
|
|
if (axios.isAxiosError(error) && error.response?.status === 400) {
|
2023-10-23 23:33:39 +02:00
|
|
|
setMessage("application:delete-channel-only-one")
|
2022-04-09 11:09:51 +02:00
|
|
|
} else {
|
2023-10-23 23:33:39 +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)}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="my-auto flex flex-col items-center justify-center py-12"
|
2022-04-09 13:48:48 +02:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
<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">
|
2022-04-09 13:48:48 +02:00
|
|
|
<Input
|
2023-10-23 23:33:39 +02:00
|
|
|
name="name"
|
|
|
|
label={t("common:name")}
|
|
|
|
placeholder={t("common:name")}
|
|
|
|
className="!mt-0"
|
2022-04-09 13:48:48 +02:00
|
|
|
onChange={onChange}
|
|
|
|
value={inputValues.name}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.name)}
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="channel-name-input"
|
2022-04-09 13:48:48 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2022-03-05 18:22:30 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2023-10-23 23:33:39 +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")}
|
2022-04-09 13:48:48 +02:00
|
|
|
</Button>
|
|
|
|
<Button
|
2023-10-23 23:33:39 +02:00
|
|
|
type="button"
|
|
|
|
color="red"
|
2022-04-09 13:48:48 +02:00
|
|
|
onClick={handleConfirmation}
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="button-delete-channel-settings"
|
2022-04-09 13:48:48 +02:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{t("application:delete")}
|
2022-04-09 13:48:48 +02:00
|
|
|
</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(
|
2023-10-23 23:33:39 +02:00
|
|
|
"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 },
|
2022-04-09 13:48:48 +02:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<ConfirmPopup
|
2023-10-23 23:33:39 +02:00
|
|
|
className={classNames("relative top-8 transition-all", {
|
|
|
|
"!top-0": confirmation,
|
2022-04-09 13:48:48 +02:00
|
|
|
})}
|
|
|
|
handleYes={handleDelete}
|
|
|
|
handleNo={handleConfirmation}
|
2023-10-23 23:33:39 +02:00
|
|
|
title={`${t("application:delete-the-channel")} ?`}
|
2022-04-09 13:48:48 +02:00
|
|
|
/>
|
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
|
|
|
)
|
|
|
|
}
|