Skip to content

Commit

Permalink
ft(#318) add request approval link to org email (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
jniyonkuru authored Nov 15, 2024
1 parent b45bb57 commit f2d96e3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 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
49 changes: 46 additions & 3 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,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);
Expand All @@ -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);
};
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Organization/AdminLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -85,11 +86,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 +101,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 f2d96e3

Please sign in to comment.