Skip to content

Commit

Permalink
ft(#318) add request approval link to org email
Browse files Browse the repository at this point in the history
  • Loading branch information
jniyonkuru authored and Oliviier-dev committed Nov 15, 2024
1 parent 75d247a commit 79efbd7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
29 changes: 29 additions & 0 deletions src/components/Organizations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
Expand Down
9 changes: 8 additions & 1 deletion src/pages/Organization/AdminLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -54,6 +57,8 @@ function AdminLogin() {
}
};



// Call showRedirectMessage when the component mounts
useEffect(() => {
showRedirectMessage();
Expand Down Expand Up @@ -85,11 +90,12 @@ function AdminLogin() {
toast.success(t(`Welcome`) as ToastContent<unknown>);
/* 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'
Expand All @@ -99,6 +105,7 @@ function AdminLogin() {
: data.loginUser.user.role === 'ttl'
? navigate('/ttl-trainees')
: navigate('/performance');
sessionStorage.removeItem("redirectParams")
} else {
navigate('/dashboard');
}
Expand Down

0 comments on commit 79efbd7

Please sign in to comment.