import { type FormEvent, useEffect, useState } from 'react'; import { httpGet, httpPut } from '../../lib/http'; import { Spinner } from '../ReactIcons/Spinner'; import UploadProfilePicture from '../UpdateProfile/UploadProfilePicture'; import type { TeamDocument } from '../CreateTeam/CreateTeamForm'; import { pageProgressMessage } from '../../stores/page'; import { useTeamId } from '../../hooks/use-team-id'; import { DeleteTeamPopup } from '../DeleteTeamPopup'; import { $isCurrentTeamAdmin } from '../../stores/team'; import { useStore } from '@nanostores/react'; import { useToast } from '../../hooks/use-toast'; export function UpdateTeamForm() { const [isLoading, setIsLoading] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const isCurrentTeamAdmin = useStore($isCurrentTeamAdmin); const toast = useToast(); const [name, setName] = useState(''); const [avatar, setAvatar] = useState(''); const [website, setWebsite] = useState(''); const [linkedIn, setLinkedIn] = useState(''); const [gitHub, setGitHub] = useState(''); const [teamType, setTeamType] = useState(''); const [teamSize, setTeamSize] = useState(''); const validTeamSizes = [ '0-1', '2-10', '11-50', '51-200', '201-500', '501-1000', '1000+', ]; const [isDisabled, setIsDisabled] = useState(false); const { teamId } = useTeamId(); useEffect(() => { setIsDisabled(!isCurrentTeamAdmin); }, [isCurrentTeamAdmin]); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); if (!name || !teamType) { setIsLoading(false); return; } const { response, error } = await httpPut( `${import.meta.env.PUBLIC_API_URL}/v1-update-team/${teamId}`, { name, website, type: teamType, gitHubUrl: gitHub || undefined, ...(teamType === 'company' && { teamSize, linkedInUrl: linkedIn || undefined, }), } ); if (error) { setIsLoading(false); toast.error(error.message || 'Something went wrong'); return; } if (response) { await loadTeam(); setIsLoading(false); toast.success('Team updated successfully'); } }; async function loadTeam() { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-team/${teamId}` ); if (error || !response) { console.log(error); return; } setName(response.name); setAvatar(response.avatar || ''); setWebsite(response?.links?.website || ''); setLinkedIn(response?.links?.linkedIn || ''); setGitHub(response?.links?.github || ''); setTeamType(response.type); if (response.teamSize) { setTeamSize(response.teamSize); } } useEffect(() => { if (!teamId) { return; } loadTeam().finally(() => { pageProgressMessage.set(''); }); }, [teamId]); return (
setName((e.target as HTMLInputElement).value)} />
setWebsite((e.target as HTMLInputElement).value)} />
{teamType === 'company' && (
setLinkedIn((e.target as HTMLInputElement).value)} />
)}
setGitHub((e.target as HTMLInputElement).value)} />
{teamType === 'company' && (
)}
{!isCurrentTeamAdmin && (

Only team admins can update team information.

)} {isCurrentTeamAdmin && ( <>
{isDeleting && ( { setIsDeleting(false); }} /> )}

Delete Team

Permanently delete this team and all of its resources.

)}
); }