From 5d3e39ea6e25e547b431db6927c399cb4d524cb5 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Mon, 7 Jun 2021 15:12:00 +0000 Subject: [PATCH] Better error handling during team member invitation Now tells you if you have already added this member / invite is pending. Behaviour a little bit more predictable during team editting. --- components/team/EditTeamModal.tsx | 11 +++++---- components/team/MemberInvitationModal.tsx | 23 ++++++++++++++--- pages/api/teams.ts | 4 ++- pages/api/teams/[team]/invite.ts | 30 ++++++++++++++++------- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/components/team/EditTeamModal.tsx b/components/team/EditTeamModal.tsx index b03fdb9976..347a7ba68c 100644 --- a/components/team/EditTeamModal.tsx +++ b/components/team/EditTeamModal.tsx @@ -62,7 +62,8 @@ export default function EditTeamModal(props) { {member.email !== session.user.email && @@ -76,20 +77,20 @@ export default function EditTeamModal(props) {

Tick the box to disband this team.

- {!checkedDisbandTeam && } + */} {checkedDisbandTeam && }
diff --git a/components/team/MemberInvitationModal.tsx b/components/team/MemberInvitationModal.tsx index e4472b09b9..36e0d5640f 100644 --- a/components/team/MemberInvitationModal.tsx +++ b/components/team/MemberInvitationModal.tsx @@ -1,8 +1,22 @@ -import {useEffect, useState} from "react"; -import {UsersIcon} from "@heroicons/react/outline"; +import { UsersIcon } from "@heroicons/react/outline"; +import { useState } from "react"; export default function MemberInvitationModal(props) { + const [ errorMessage, setErrorMessage ] = useState(''); + + const handleError = async (res) => { + + const responseData = await res.json(); + + if (res.ok === false) { + setErrorMessage(responseData.message); + throw new Error(responseData.message); + } + + return responseData; + }; + const inviteMember = (e) => { e.preventDefault(); @@ -19,7 +33,9 @@ export default function MemberInvitationModal(props) { headers: { 'Content-Type': 'application/json' } - }).then(props.onExit); + }).then(handleError).then(props.onExit).catch( (e) => { + // do nothing. + }); }; return (
@@ -60,6 +76,7 @@ export default function MemberInvitationModal(props) {
+ {errorMessage &&

Error: {errorMessage}

}