import { ArrowLeftIcon, PlusIcon, TrashIcon } from "@heroicons/react/outline"; import React, { useEffect, useRef, useState } from "react"; import { useLocale } from "@lib/hooks/useLocale"; import { Member } from "@lib/member"; import showToast from "@lib/notification"; import { Team } from "@lib/team"; import { Dialog, DialogTrigger } from "@components/Dialog"; import ImageUploader from "@components/ImageUploader"; import ConfirmationDialogContent from "@components/dialog/ConfirmationDialogContent"; import MemberInvitationModal from "@components/team/MemberInvitationModal"; import Avatar from "@components/ui/Avatar"; import Button from "@components/ui/Button"; import { UsernameInput } from "@components/ui/UsernameInput"; import ErrorAlert from "@components/ui/alerts/Error"; import MemberList from "./MemberList"; export default function EditTeam(props: { team: Team | undefined | null; onCloseEdit: () => void }) { const [members, setMembers] = useState([]); const nameRef = useRef() as React.MutableRefObject; const teamUrlRef = useRef() as React.MutableRefObject; const descriptionRef = useRef() as React.MutableRefObject; const hideBrandingRef = useRef() as React.MutableRefObject; const logoRef = useRef() as React.MutableRefObject; const [hasErrors, setHasErrors] = useState(false); const [showMemberInvitationModal, setShowMemberInvitationModal] = useState(false); const [inviteModalTeam, setInviteModalTeam] = useState(); const [errorMessage, setErrorMessage] = useState(""); const [imageSrc, setImageSrc] = useState(""); const { t } = useLocale(); const loadMembers = () => fetch("/api/teams/" + props.team?.id + "/membership") .then((res) => res.json()) .then((data) => setMembers(data.members)); useEffect(() => { loadMembers(); }, []); const deleteTeam = () => { return fetch("/api/teams/" + props.team?.id, { method: "DELETE", }).then(props.onCloseEdit()); }; const onRemoveMember = (member: Member) => { return fetch("/api/teams/" + props.team?.id + "/membership", { method: "DELETE", body: JSON.stringify({ userId: member.id }), headers: { "Content-Type": "application/json", }, }).then(loadMembers); }; const onInviteMember = (team: Team | null | undefined) => { setShowMemberInvitationModal(true); setInviteModalTeam(team); }; const handleError = async (resp: Response) => { if (!resp.ok) { const error = await resp.json(); throw new Error(error.message); } }; async function updateTeamHandler(event) { event.preventDefault(); const enteredUsername = teamUrlRef?.current?.value.toLowerCase(); const enteredName = nameRef?.current?.value; const enteredDescription = descriptionRef?.current?.value; const enteredLogo = logoRef?.current?.value; const enteredHideBranding = hideBrandingRef?.current?.checked; // TODO: Add validation await fetch("/api/teams/" + props.team?.id + "/profile", { method: "PATCH", body: JSON.stringify({ username: enteredUsername, name: enteredName, description: enteredDescription, logo: enteredLogo, hideBranding: enteredHideBranding, }), headers: { "Content-Type": "application/json", }, }) .then(handleError) .then(() => { showToast(t("your_team_updated_successfully"), "success"); setHasErrors(false); // dismiss any open errors }) .catch((err) => { setHasErrors(true); setErrorMessage(err.message); }); } const onMemberInvitationModalExit = () => { loadMembers(); setShowMemberInvitationModal(false); }; const handleLogoChange = (newLogo: string) => { logoRef.current.value = newLogo; const nativeInputValueSetter = Object.getOwnPropertyDescriptor(HTMLInputElement?.prototype, "value").set; nativeInputValueSetter?.call(logoRef.current, newLogo); const ev2 = new Event("input", { bubbles: true }); logoRef?.current?.dispatchEvent(ev2); updateTeamHandler(ev2); setImageSrc(newLogo); }; return (

{props.team?.name}

{t("manage_your_team")}


{t("profile")}

{hasErrors && }

{t("team_description")}


{t("members")}

{!!members.length && ( )}

{t("disable_cal_branding_description")}


{t("danger_zone")}

{ e.stopPropagation(); }} className="btn-sm btn-white"> {t("disband_team")} deleteTeam()}> {t("disband_team_confirmation_message")}

{showMemberInvitationModal && ( )}
); }