Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#318) add request approval link to org email #616

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading