diff --git a/src/ProtectedRoute.tsx b/src/ProtectedRoute.tsx index ad86f418f..6e99adaa8 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 1410ca4c7..b94347779 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,19 @@ 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; + await ApproveOrganization({name,description,email}) + toast.success(`${name} organization has been approved.`); + } catch (error:any) { + console.error(error.message); + + } +} + const [createOrganizationModel, setCreateOrganizationModel] = useState(false); const [deleteOrganizationModel, setDeleteOrganizationModel] = useState(false); const [showActions, setShowActions] = useState(false); @@ -162,6 +177,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); }; diff --git a/src/pages/Organization/AdminLogin.tsx b/src/pages/Organization/AdminLogin.tsx index f199490f4..a7f9d26d6 100644 --- a/src/pages/Organization/AdminLogin.tsx +++ b/src/pages/Organization/AdminLogin.tsx @@ -46,6 +46,9 @@ 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'); @@ -54,6 +57,8 @@ function AdminLogin() { } }; + + // Call showRedirectMessage when the component mounts useEffect(() => { showRedirectMessage(); @@ -85,11 +90,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 +105,7 @@ function AdminLogin() { : data.loginUser.user.role === 'ttl' ? navigate('/ttl-trainees') : navigate('/performance'); + sessionStorage.removeItem("redirectParams") } else { navigate('/dashboard'); }