2023-10-23 23:33:39 +02:00
|
|
|
import Image from "next/image"
|
|
|
|
import { useRouter } from "next/router"
|
|
|
|
import { useState } from "react"
|
|
|
|
import { Type } from "@sinclair/typebox"
|
|
|
|
import { PhotographIcon } from "@heroicons/react/solid"
|
|
|
|
import { Form, useForm } from "react-component-form"
|
|
|
|
import useTranslation from "next-translate/useTranslation"
|
|
|
|
import classNames from "clsx"
|
|
|
|
import type { HandleUseFormCallback } from "react-component-form"
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { guildSchema } from "../../../models/Guild"
|
|
|
|
import { FormState } from "../../design/FormState"
|
|
|
|
import { useGuildMember } from "../../../contexts/GuildMember"
|
|
|
|
import { Textarea } from "../../design/Textarea"
|
|
|
|
import { Input } from "../../design/Input"
|
|
|
|
import { Button } from "../../design/Button"
|
|
|
|
import { useAuthentication } from "../../../tools/authentication"
|
|
|
|
import { ConfirmPopup } from "../ConfirmPopup"
|
|
|
|
import { useFormTranslation } from "../../../hooks/useFormTranslation"
|
2022-08-28 18:26:56 +02:00
|
|
|
|
|
|
|
const schema = {
|
|
|
|
name: guildSchema.name,
|
2023-10-23 23:33:39 +02:00
|
|
|
description: Type.Optional(guildSchema.description),
|
2022-08-28 18:26:56 +02:00
|
|
|
}
|
2022-03-05 18:22:30 +01:00
|
|
|
|
|
|
|
export const GuildSettings: React.FC = () => {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const router = useRouter()
|
|
|
|
const { authentication } = useAuthentication()
|
|
|
|
const { guild, member } = useGuildMember()
|
|
|
|
|
|
|
|
const [inputValues, setInputValues] = useState({
|
|
|
|
name: guild.name,
|
2023-10-23 23:33:39 +02:00
|
|
|
description: guild.description,
|
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-09-21 10:09:36 +02:00
|
|
|
} = useForm(schema)
|
2022-08-28 18:26:56 +02:00
|
|
|
const { getFirstErrorTranslation } = useFormTranslation()
|
2022-03-05 18:22:30 +01:00
|
|
|
|
2022-09-21 10:09:36 +02:00
|
|
|
const onSubmit: HandleUseFormCallback<typeof schema> = async (formData) => {
|
2022-03-05 18:22:30 +01:00
|
|
|
try {
|
|
|
|
await authentication.api.put(`/guilds/${guild.id}`, formData)
|
2022-08-28 18:26:56 +02:00
|
|
|
setInputValues(formData as unknown as any)
|
2022-03-05 18:22:30 +01:00
|
|
|
return {
|
2023-10-23 23:33:39 +02:00
|
|
|
type: "success",
|
|
|
|
message: "application:saved-information",
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
} 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 handleFileChange: React.ChangeEventHandler<HTMLInputElement> = async (
|
2023-10-23 23:33:39 +02:00
|
|
|
event,
|
2022-03-05 18:22:30 +01:00
|
|
|
) => {
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("loading")
|
2022-03-05 18:22:30 +01:00
|
|
|
const files = event?.target?.files
|
2023-01-11 17:39:09 +01:00
|
|
|
if (files != null && files.length === 1 && files[0] != null) {
|
2022-03-05 18:22:30 +01:00
|
|
|
const file = files[0]
|
|
|
|
const formData = new FormData()
|
2023-10-23 23:33:39 +02:00
|
|
|
formData.append("icon", file)
|
2022-03-05 18:22:30 +01:00
|
|
|
try {
|
2023-07-22 16:34:23 +02:00
|
|
|
await authentication.api.put(`/guilds/${guild.id}/icon`, formData, {
|
|
|
|
headers: {
|
2023-10-23 23:33:39 +02:00
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
},
|
2023-07-22 16:34:23 +02:00
|
|
|
})
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("idle")
|
2022-03-05 18:22:30 +01:00
|
|
|
} catch (error) {
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("error")
|
|
|
|
setMessage("errors:server-error")
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleDelete = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
await authentication.api.delete(`/guilds/${guild.id}`)
|
|
|
|
} catch (error) {
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("error")
|
|
|
|
setMessage("errors:server-error")
|
2022-03-05 18:22:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleLeave = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
await authentication.api.delete(`/guilds/${guild.id}/members/leave`)
|
2023-10-23 23:33:39 +02:00
|
|
|
await router.push("/application")
|
2022-03-05 18:22:30 +01:00
|
|
|
} catch (error) {
|
2023-10-23 23:33:39 +02:00
|
|
|
setFetchState("error")
|
|
|
|
setMessage("errors:server-error")
|
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
|
|
|
>
|
|
|
|
{member.isOwner && (
|
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="relative">
|
|
|
|
<div className="absolute z-50 h-full w-full">
|
|
|
|
<button className="relative flex h-full w-full items-center justify-center transition hover:scale-110">
|
2022-04-09 13:48:48 +02:00
|
|
|
<input
|
2023-10-23 23:33:39 +02:00
|
|
|
type="file"
|
|
|
|
className="absolute h-full w-full cursor-pointer opacity-0"
|
2022-04-09 13:48:48 +02:00
|
|
|
onChange={handleFileChange}
|
|
|
|
/>
|
2023-10-23 23:33:39 +02:00
|
|
|
<PhotographIcon color="white" className="h-8 w-8" />
|
2022-04-09 13:48:48 +02:00
|
|
|
</button>
|
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<div className="flex items-center justify-center rounded-full bg-black shadow-xl">
|
2022-04-09 13:48:48 +02:00
|
|
|
<Image
|
|
|
|
quality={100}
|
2023-10-23 23:33:39 +02:00
|
|
|
className="rounded-full opacity-50"
|
2022-04-09 13:48:48 +02:00
|
|
|
src={
|
|
|
|
guild.icon == null
|
2023-10-23 23:33:39 +02:00
|
|
|
? "/images/data/guild-default.png"
|
2022-04-09 13:48:48 +02:00
|
|
|
: guild.icon
|
|
|
|
}
|
2023-10-23 23:33:39 +02:00
|
|
|
alt="Profil Picture"
|
|
|
|
draggable="false"
|
2022-04-09 13:48:48 +02:00
|
|
|
height={125}
|
|
|
|
width={125}
|
2022-03-05 18:22:30 +01:00
|
|
|
/>
|
2022-04-09 13:48:48 +02:00
|
|
|
</div>
|
2022-03-05 18:22:30 +01:00
|
|
|
</div>
|
2023-10-23 23:33:39 +02:00
|
|
|
<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="guild-name-input"
|
2022-04-09 13:48:48 +02:00
|
|
|
/>
|
|
|
|
<Textarea
|
2023-10-23 23:33:39 +02:00
|
|
|
name="description"
|
|
|
|
label={"Description"}
|
|
|
|
placeholder={"Description"}
|
|
|
|
id="textarea-description"
|
2022-04-09 13:48:48 +02:00
|
|
|
onChange={onChange}
|
2023-10-23 23:33:39 +02:00
|
|
|
value={inputValues.description ?? ""}
|
|
|
|
data-cy="guild-description-input"
|
2022-03-05 18:22:30 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-04-09 13:48:48 +02:00
|
|
|
)}
|
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">
|
2022-04-09 13:48:48 +02:00
|
|
|
{member.isOwner ? (
|
|
|
|
<>
|
2023-10-23 23:33:39 +02:00
|
|
|
<Button type="submit" data-cy="button-save-guild-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-guild-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>
|
|
|
|
</>
|
|
|
|
) : (
|
2022-03-05 18:22:30 +01:00
|
|
|
<Button
|
2023-10-23 23:33:39 +02:00
|
|
|
color="red"
|
2022-04-09 13:48:48 +02:00
|
|
|
onClick={handleLeave}
|
2023-10-23 23:33:39 +02:00
|
|
|
data-cy="button-leave-guild-settings"
|
2022-03-05 18:22:30 +01:00
|
|
|
>
|
2023-10-23 23:33:39 +02:00
|
|
|
{t("application:leave")} {guild.name}
|
2022-03-05 18:22:30 +01:00
|
|
|
</Button>
|
2022-04-09 13:48:48 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<FormState
|
|
|
|
state={fetchState}
|
2022-08-28 18:26:56 +02:00
|
|
|
message={
|
|
|
|
message != null
|
|
|
|
? t(message)
|
2022-09-21 10:09:36 +02:00
|
|
|
: getFirstErrorTranslation(errors.name)
|
2022-08-28 18:26:56 +02:00
|
|
|
}
|
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
|
|
|
</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-guild")} ?`}
|
2022-04-07 16:54:05 +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
|
|
|
)
|
|
|
|
}
|