From 8e945f5e1ce7dd649e917bf3e6c0c97df78d4d7c Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Tue, 25 Jul 2023 04:38:49 +0600 Subject: [PATCH] Leave Team confirmation popup (#4254) * wip: leave team popup * fix: leave warning --- src/components/DeleteTeamPopup.tsx | 9 +- .../TeamMembers/LeaveTeamButton.tsx | 52 +++----- src/components/TeamMembers/LeaveTeamPopup.tsx | 124 ++++++++++++++++++ .../TeamSettings/UpdateTeamForm.tsx | 1 - 4 files changed, 151 insertions(+), 35 deletions(-) create mode 100644 src/components/TeamMembers/LeaveTeamPopup.tsx diff --git a/src/components/DeleteTeamPopup.tsx b/src/components/DeleteTeamPopup.tsx index 106d7db93..893dd2ca3 100644 --- a/src/components/DeleteTeamPopup.tsx +++ b/src/components/DeleteTeamPopup.tsx @@ -4,6 +4,7 @@ import type { TeamDocument } from './CreateTeam/CreateTeamForm'; import { useTeamId } from '../hooks/use-team-id'; import { useOutsideClick } from '../hooks/use-outside-click'; import { useKeydown } from '../hooks/use-keydown'; +import { useToast } from '../hooks/use-toast'; type DeleteTeamPopupProps = { onClose: () => void; @@ -12,6 +13,7 @@ type DeleteTeamPopupProps = { export function DeleteTeamPopup(props: DeleteTeamPopupProps) { const { onClose } = props; + const toast = useToast(); const popupBodyEl = useRef(null); const inputEl = useRef(null); @@ -53,6 +55,7 @@ export function DeleteTeamPopup(props: DeleteTeamPopupProps) { return; } + toast.success('Team deleted successfully'); window.location.href = '/account'; }; @@ -72,9 +75,9 @@ export function DeleteTeamPopup(props: DeleteTeamPopupProps) { ref={popupBodyEl} class="popup-body relative rounded-lg bg-white p-4 shadow" > -

- This will permanently delete your account and all your associated - data including your progress. +

Delete Team

+

+ This will permanently delete your team and all associated data.

diff --git a/src/components/TeamMembers/LeaveTeamButton.tsx b/src/components/TeamMembers/LeaveTeamButton.tsx index e22ad5290..bd716892d 100644 --- a/src/components/TeamMembers/LeaveTeamButton.tsx +++ b/src/components/TeamMembers/LeaveTeamButton.tsx @@ -1,40 +1,30 @@ -import { useState } from "preact/hooks"; -import { httpDelete } from "../../lib/http"; -import { Spinner } from "../ReactIcons/Spinner"; -import { useToast } from "../../hooks/use-toast"; +import { useState } from 'preact/hooks'; +import { LeaveTeamPopup } from './LeaveTeamPopup'; type LeaveTeamButtonProps = { teamId: string; }; export function LeaveTeamButton(props: LeaveTeamButtonProps) { - const toast = useToast(); - const [isLoading, setIsLoading] = useState(false); - const { teamId } = props; - - async function leaveTeam() { - setIsLoading(true); - const { response, error } = await httpDelete( - `${import.meta.env.PUBLIC_API_URL}/v1-leave-team/${teamId}`, - {} - ); - - if (error || !response) { - setIsLoading(false); - toast.error(error?.message || 'Something went wrong'); - return; - } - - window.location.href = '/account'; - } + const [showLeaveTeamPopup, setShowLeaveTeamPopup] = useState(false); return ( - - ) - + <> + {showLeaveTeamPopup && ( + { + setShowLeaveTeamPopup(false); + }} + /> + )} + + + ); } diff --git a/src/components/TeamMembers/LeaveTeamPopup.tsx b/src/components/TeamMembers/LeaveTeamPopup.tsx new file mode 100644 index 000000000..153d21636 --- /dev/null +++ b/src/components/TeamMembers/LeaveTeamPopup.tsx @@ -0,0 +1,124 @@ +import { useEffect, useRef, useState } from 'preact/hooks'; +import { httpDelete } from '../../lib/http'; +import { useTeamId } from '../../hooks/use-team-id'; +import { useOutsideClick } from '../../hooks/use-outside-click'; + +type LeaveTeamPopupProps = { + onClose: () => void; +}; + +export function LeaveTeamPopup(props: LeaveTeamPopupProps) { + const { onClose } = props; + + const popupBodyRef = useRef(null); + const confirmationEl = useRef(null); + const [confirmationText, setConfirmationText] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const [error, setError] = useState(''); + const { teamId } = useTeamId(); + + useEffect(() => { + setError(''); + setConfirmationText(''); + confirmationEl?.current?.focus(); + }, []); + + const handleSubmit = async (e: Event) => { + e.preventDefault(); + setIsLoading(true); + setError(''); + + if (confirmationText.toUpperCase() !== 'LEAVE') { + setError('Verification text does not match'); + setIsLoading(false); + return; + } + + const { response, error } = await httpDelete( + `${import.meta.env.PUBLIC_API_URL}/v1-leave-team/${teamId}`, + {} + ); + + if (error || !response) { + setIsLoading(false); + setError(error?.message || 'Something went wrong'); + return; + } + + window.location.href = '/account'; + }; + + const handleClosePopup = () => { + setIsLoading(false); + setError(''); + setConfirmationText(''); + + onClose(); + }; + + useOutsideClick(popupBodyRef, handleClosePopup); + + return ( +

+ ); +} diff --git a/src/components/TeamSettings/UpdateTeamForm.tsx b/src/components/TeamSettings/UpdateTeamForm.tsx index 5fbf062a1..90fdbe0c5 100644 --- a/src/components/TeamSettings/UpdateTeamForm.tsx +++ b/src/components/TeamSettings/UpdateTeamForm.tsx @@ -267,7 +267,6 @@ export function UpdateTeamForm() { {isDeleting && ( { - toast.success('Team deleted successfully'); setIsDeleting(false); }} />