Skip to content

Commit 79efbd7

Browse files
jniyonkuruOliviier-dev
authored andcommitted
ft(#318) add request approval link to org email
1 parent 75d247a commit 79efbd7

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/ProtectedRoute.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import React, { useContext } from 'react';
22
import Notify from './components/Notify';
33
import { UserContext } from './hook/useAuth';
44
import checkOrgTokenExpiration from './utils/validateOrgToken';
5-
5+
import { useSearchParams } from 'react-router-dom';
66
interface SomeType {
77
children: any;
88
}
99
// eslint-disable-next-line react/prop-types
1010
export default function ProtectedRoutes(obj: SomeType) {
11+
12+
const[searchParams]=useSearchParams()
1113
const { user } = useContext(UserContext);
1214
/* istanbul ignore next */
1315
checkOrgTokenExpiration();
1416

1517
if (!user?.auth) {
18+
if(searchParams){sessionStorage.setItem("redirectParams",searchParams.toString())}
1619
return obj.children;
1720
}
1821
/* istanbul ignore next */

src/components/Organizations.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import OrgSkeleton from '../Skeletons/Organization.skeleton';
1414
import { DeleteOrganization } from '../Mutations/OrganisationMutations';
1515
import { RegisterNewOrganization } from '../Mutations/OrganisationMutations';
1616
import { AddOrganization } from '../Mutations/OrganisationMutations';
17+
import jwtDecode from 'jwt-decode';
18+
import { useSearchParams,useNavigate } from 'react-router-dom';
1719

1820
export interface Admin {
1921
id: string;
@@ -145,6 +147,19 @@ const Organizations = () => {
145147
refetch: Function;
146148
} = useQuery(getOrganizations);
147149

150+
const ApproveNewOrganization= async (token:string)=>{
151+
try {
152+
const decodedToken:any = await jwtDecode(token);
153+
if(! decodedToken) throw new Error("Failed to decode token")
154+
const {nm:name,desc:description,email}=decodedToken;
155+
await ApproveOrganization({name,description,email})
156+
toast.success(`${name} organization has been approved.`);
157+
} catch (error:any) {
158+
console.error(error.message);
159+
160+
}
161+
}
162+
148163
const [createOrganizationModel, setCreateOrganizationModel] = useState(false);
149164
const [deleteOrganizationModel, setDeleteOrganizationModel] = useState(false);
150165
const [showActions, setShowActions] = useState(false);
@@ -162,6 +177,20 @@ const Organizations = () => {
162177
description: '',
163178
});
164179

180+
const [searchParams]=useSearchParams()
181+
const navigate = useNavigate();
182+
183+
184+
useEffect(() => {
185+
const newOrgToken = searchParams.get("newOrgToken");
186+
if (newOrgToken) {
187+
ApproveNewOrganization(newOrgToken);
188+
searchParams.delete('newOrgToken');
189+
navigate(`?${searchParams.toString()}`, { replace: true });
190+
191+
}
192+
}, []);
193+
165194
const handleShowActions = () => {
166195
setShowActions(!showActions);
167196
};

src/pages/Organization/AdminLogin.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ function AdminLogin() {
4646
const [LoginUser] = useMutation(LOGIN_MUTATION);
4747
const client = useApolloClient();
4848
const [searchParams] = useSearchParams();
49+
50+
51+
4952
// Function to get the redirect_message from the URL and toast it
5053
const showRedirectMessage = () => {
5154
const redirectMessage = searchParams.get('redirect_message');
@@ -54,6 +57,8 @@ function AdminLogin() {
5457
}
5558
};
5659

60+
61+
5762
// Call showRedirectMessage when the component mounts
5863
useEffect(() => {
5964
showRedirectMessage();
@@ -85,11 +90,12 @@ function AdminLogin() {
8590
toast.success(t(`Welcome`) as ToastContent<unknown>);
8691
/* istanbul ignore next */
8792

93+
const redirectParams=sessionStorage.getItem("redirectParams")||''
8894
if (data.loginUser) {
8995
redirect
9096
? navigate(`${redirect}`)
9197
: data.loginUser.user.role === 'superAdmin'
92-
? navigate(`/organizations`)
98+
? navigate(`/organizations${redirectParams}`)
9399
: data.loginUser.user.role === 'admin'
94100
? navigate(`/trainees`)
95101
: data.loginUser.user.role === 'coordinator'
@@ -99,6 +105,7 @@ function AdminLogin() {
99105
: data.loginUser.user.role === 'ttl'
100106
? navigate('/ttl-trainees')
101107
: navigate('/performance');
108+
sessionStorage.removeItem("redirectParams")
102109
} else {
103110
navigate('/dashboard');
104111
}

0 commit comments

Comments
 (0)