From 58177be24334f266af23cea40005e0cf57a72c94 Mon Sep 17 00:00:00 2001 From: sanjay-k1910 Date: Thu, 19 Oct 2023 13:55:58 +0530 Subject: [PATCH] refactor: restrict user to send invitation to self Signed-off-by: sanjay-k1910 --- .../invitations/SendInvitationModal.tsx | 54 +++++++++++++++---- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/src/components/organization/invitations/SendInvitationModal.tsx b/src/components/organization/invitations/SendInvitationModal.tsx index 62a99878c..2daefffab 100644 --- a/src/components/organization/invitations/SendInvitationModal.tsx +++ b/src/components/organization/invitations/SendInvitationModal.tsx @@ -6,12 +6,13 @@ import { useEffect, useState } from 'react'; import { AlertComponent } from '../../AlertComponent'; import type { AxiosResponse } from 'axios'; -import { apiStatusCodes } from '../../../config/CommonConstant'; +import { apiStatusCodes, storageKeys } from '../../../config/CommonConstant'; import { createEcoSystemInvitations, createInvitations, } from '../../../api/invitations'; import { getOrganizationRoles } from '../../../api/organization'; +import { getFromLocalStorage } from '../../../api/Auth'; interface Values { email: string; @@ -29,14 +30,18 @@ interface RoleI { } const SendInvitationModal = (props: { - getAllSentInvitations?: ()=> void; - ecosystemId? : string; + getAllSentInvitations?: () => void; + ecosystemId?: string; flag?: boolean; openModal: boolean; setMessage: (message: string) => void; setOpenModal: (flag: boolean) => void; }) => { const [loading, setLoading] = useState(false); + const [selfEmail, setSelfEmail] = useState({ + email: "", + error: "" + }); const [invitations, setInvitations] = useState([]); @@ -143,6 +148,18 @@ const SendInvitationModal = (props: { } setLoading(false); }; + + useEffect(() => { + const getEmail = async () => { + const email = await getFromLocalStorage(storageKeys.USER_EMAIL) + setSelfEmail({ + ...selfEmail, + email + }) + } + getEmail() + }, []) + return ( { setInvitations([]); setInitialInvitationData({ - email:" ", + email: " ", }); props.setOpenModal(false); }} @@ -180,6 +197,17 @@ const SendInvitationModal = (props: { values: Values, { resetForm }: FormikHelpers, ) => { + if(values.email === selfEmail.email){ + setSelfEmail({ + ...selfEmail, + error: "You can't send invitation to self." + }) + return + } + setSelfEmail({ + ...selfEmail, + error: "" + }) await includeInvitation(values); resetForm({ values: initialInvitationData }); }} @@ -195,19 +223,27 @@ const SendInvitationModal = (props: {