diff --git a/src/ProtectedRoute.tsx b/src/ProtectedRoute.tsx index ad86f418..6e99adaa 100644 --- a/src/ProtectedRoute.tsx +++ b/src/ProtectedRoute.tsx @@ -2,17 +2,20 @@ import React, { useContext } from 'react'; import Notify from './components/Notify'; import { UserContext } from './hook/useAuth'; import checkOrgTokenExpiration from './utils/validateOrgToken'; - +import { useSearchParams } from 'react-router-dom'; interface SomeType { children: any; } // eslint-disable-next-line react/prop-types export default function ProtectedRoutes(obj: SomeType) { + + const[searchParams]=useSearchParams() const { user } = useContext(UserContext); /* istanbul ignore next */ checkOrgTokenExpiration(); if (!user?.auth) { + if(searchParams){sessionStorage.setItem("redirectParams",searchParams.toString())} return obj.children; } /* istanbul ignore next */ diff --git a/src/components/Organizations.tsx b/src/components/Organizations.tsx index 1410ca4c..227ea77b 100644 --- a/src/components/Organizations.tsx +++ b/src/components/Organizations.tsx @@ -14,6 +14,8 @@ import OrgSkeleton from '../Skeletons/Organization.skeleton'; import { DeleteOrganization } from '../Mutations/OrganisationMutations'; import { RegisterNewOrganization } from '../Mutations/OrganisationMutations'; import { AddOrganization } from '../Mutations/OrganisationMutations'; +import jwtDecode from 'jwt-decode'; +import { useSearchParams,useNavigate } from 'react-router-dom'; export interface Admin { id: string; @@ -145,6 +147,23 @@ const Organizations = () => { refetch: Function; } = useQuery(getOrganizations); +const ApproveNewOrganization= async (token:string)=>{ + try { + const decodedToken:any = await jwtDecode(token); + if(! decodedToken) throw new Error("Failed to decode token") + const {nm:name,desc:description,email}=decodedToken; + const approvalResult = await ApproveOrganization({ name, description, email }); + + if (approvalResult && approvalResult.success) { + toast.success(`${name} organization has been approved.`); + } else { + toast.error(`${name} organization approval failed.`); + } + } catch (error:any) { + toast.error(`An error occurred, Try again`); + } +} + const [createOrganizationModel, setCreateOrganizationModel] = useState(false); const [deleteOrganizationModel, setDeleteOrganizationModel] = useState(false); const [showActions, setShowActions] = useState(false); @@ -162,6 +181,20 @@ const Organizations = () => { description: '', }); + const [searchParams]=useSearchParams() +const navigate = useNavigate(); + + +useEffect(() => { + const newOrgToken = searchParams.get("newOrgToken"); + if (newOrgToken) { + ApproveNewOrganization(newOrgToken); + searchParams.delete('newOrgToken'); + navigate(`?${searchParams.toString()}`, { replace: true }); + + } +}, []); + const handleShowActions = () => { setShowActions(!showActions); }; @@ -230,9 +263,19 @@ const Organizations = () => { } async function ApproveOrganization(data: any) { - await RegisterOrganizationMutation({ - variables: { organizationInput: data, action: 'approve' }, - }); + try { + const { data: mutationResult } = await RegisterOrganizationMutation({ + variables: { organizationInput: data, action: 'approve' }, + }); + + if (mutationResult) { + return { success: true }; + } else { + return { success: false}; + } + } catch (error:any) { + toast.error(`An error occurred, Try again`); + } } async function RejectOrganization(data: any) { diff --git a/src/pages/Organization/AdminLogin.tsx b/src/pages/Organization/AdminLogin.tsx index b84c2847..dfd8dcab 100644 --- a/src/pages/Organization/AdminLogin.tsx +++ b/src/pages/Organization/AdminLogin.tsx @@ -46,6 +46,7 @@ function AdminLogin() { const [LoginUser] = useMutation(LOGIN_MUTATION); const client = useApolloClient(); const [searchParams] = useSearchParams(); + // Function to get the redirect_message from the URL and toast it const showRedirectMessage = () => { const redirectMessage = searchParams.get('redirect_message'); @@ -85,11 +86,12 @@ function AdminLogin() { toast.success(t(`Welcome`) as ToastContent); /* istanbul ignore next */ + const redirectParams=sessionStorage.getItem("redirectParams")||'' if (data.loginUser) { redirect ? navigate(`${redirect}`) : data.loginUser.user.role === 'superAdmin' - ? navigate(`/organizations`) + ? navigate(`/organizations${redirectParams}`) : data.loginUser.user.role === 'admin' ? navigate(`/trainees`) : data.loginUser.user.role === 'coordinator' @@ -99,6 +101,7 @@ function AdminLogin() { : data.loginUser.user.role === 'ttl' ? navigate('/ttl-trainees') : navigate('/performance'); + sessionStorage.removeItem("redirectParams") } else { navigate('/dashboard'); }