Skip to content

Commit

Permalink
logout deleted user automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
elijahladdie committed Sep 25, 2024
1 parent 57c1c9c commit 9f2f78a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 61 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,6 @@
"lint-staged": {
"*.{js,ts,tsx}": "eslint --fix",
"*.{js,ts,html,css,md}": "prettier --write \"./**/*.{js,ts,jsx,tsx,json}\""
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
12 changes: 5 additions & 7 deletions src/components/ManagerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ function ManagerCard() {
grade,
teamname: team.name,
coordinator: team?.cohort?.coordinator?.profile
? team.cohort.coordinator.profile.name
: team?.cohort.coordinator?.email,
? team.cohort?.coordinator?.profile?.name
: team?.cohort?.coordinator?.email,
ttl: team?.ttl?.profile ? team.ttl.profile.name : team?.ttl?.email,
phase: team.cohort.phase.name,
phase: team?.cohort?.phase?.name,
week:
calculateWeeks(team.startingPhase) > 0
? calculateWeeks(team.startingPhase)
Expand All @@ -161,9 +161,7 @@ function ManagerCard() {
});

return (
<div
className="font-serif px-4 md:px-0 pb-20 w-full dark:bg-dark-frame-bg dark:text-black h-full flex overflow-x-auto "
>
<div className="font-serif px-4 md:px-0 pb-20 w-full dark:bg-dark-frame-bg dark:text-black h-full flex overflow-x-auto ">
{loading ? (
<div className="flex items-center justify-center w-full h-full">
<div className="spinner" />
Expand All @@ -172,7 +170,7 @@ function ManagerCard() {
<div className="pl-10 flex">
{teamData &&
teamData.map((teamProps: any, index: number) => (
<Link key={index} to={`/team/${(teamProps.teamname)}`}>
<Link key={index} to={`/team/${teamProps.teamname}`}>
<Card {...teamProps} />
</Link>
))}
Expand Down
74 changes: 38 additions & 36 deletions src/components/teamDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ function TeamDetails() {
cohort: team?.cohort?.name,
teamname: team.name,
coordinator: team?.cohort?.coordinator?.profile
? team.cohort.coordinator.profile.name
: team?.cohort.coordinator?.email,
ttl: team?.ttl?.profile ? team.ttl.profile.name : team?.ttl?.email,
? team?.cohort?.coordinator?.profile?.name
: team?.cohort?.coordinator?.email,
ttl: team?.ttl?.profile ? team?.ttl?.profile?.name : team?.ttl?.email,
Qty,
Qnty,
skills,
Expand All @@ -90,8 +90,8 @@ function TeamDetails() {

const selectedTeam = teamData
? teamData.find(
(team: { teamname: string | null }) => team.teamname === teamname,
)
(team: { teamname: string | null }) => team.teamname === teamname,
)
: null;
function getColor(rating: number) {
if (rating >= 1.5 && rating <= 2) {
Expand Down Expand Up @@ -228,12 +228,12 @@ function TeamDetails() {
<h1 className="font-bold text-xl">{teamname}</h1>
<div className="border-r-2 h-6 border-black" />
<h1 className="font-extrabold text-xl">
{selectedTeam.cohort}
{selectedTeam?.cohort}
</h1>
</div>
<div className="relative dark:text-black">
<p>Coordinator: {selectedTeam.coordinator}</p>
<p>TTL: {selectedTeam.ttl}</p>
<p>Coordinator: {selectedTeam?.coordinator}</p>
<p>TTL: {selectedTeam?.ttl}</p>
</div>
</div>

Expand Down Expand Up @@ -285,12 +285,12 @@ function TeamDetails() {
<div className="flex flex-row lg:flex-row ">
<div className="w-[90px]">
<CircularProgressbar
value={(selectedTeam.Qty / 2) * 100}
text={`${selectedTeam.Qty.toFixed(1)}`}
className={getRatingColorClass(selectedTeam.Qty)}
value={(selectedTeam?.Qty / 2) * 100}
text={`${selectedTeam?.Qty.toFixed(1)}`}
className={getRatingColorClass(selectedTeam?.Qty)}
styles={{
path: {
stroke: getColor(selectedTeam.Qty),
stroke: getColor(selectedTeam?.Qty),
transition: 'stroke-dashoffset 0.5s ease 0s',
},
trail: {
Expand All @@ -310,23 +310,24 @@ function TeamDetails() {
<ul className="list-disc">
<li>Quality</li>
</ul>
<span style={{ color: getColor(selectedTeam.Qty) }}>
{selectedTeam.Qty >= 1.5 && selectedTeam.Qty <= 2
<span style={{ color: getColor(selectedTeam?.Qty) }}>
{selectedTeam?.Qty >= 1.5 && selectedTeam?.Qty <= 2
? 'Good'
: selectedTeam.Qty >= 1 && selectedTeam.Qty < 1.5
? 'Improve'
: 'Poor'}
: selectedTeam?.Qty >= 1 &&
selectedTeam?.Qty < 1.5
? 'Improve'
: 'Poor'}
</span>
</div>
</div>
<div className=" flex flex-row lg:flex-row ">
<div className="w-[90px] ">
<CircularProgressbar
value={(selectedTeam.Qnty / 2) * 100}
text={`${selectedTeam.Qnty.toFixed(1)}`}
value={(selectedTeam?.Qnty / 2) * 100}
text={`${selectedTeam?.Qnty.toFixed(1)}`}
styles={{
path: {
stroke: getColor(selectedTeam.Qnty),
stroke: getColor(selectedTeam?.Qnty),
transition: 'stroke-dashoffset 0.5s ease 0s',
},
trail: {
Expand All @@ -346,25 +347,26 @@ function TeamDetails() {
<ul className="list-disc ">
<li>Quantity</li>
</ul>
<span style={{ color: getColor(selectedTeam.Qnty) }}>
{selectedTeam.Qnty >= 1.5 && selectedTeam.Qnty <= 2
<span style={{ color: getColor(selectedTeam?.Qnty) }}>
{selectedTeam?.Qnty >= 1.5 &&
selectedTeam?.Qnty <= 2
? 'Good'
: selectedTeam.Qnty >= 1 &&
selectedTeam.Qnty < 1.5
? 'Improve'
: 'Poor'}
: selectedTeam?.Qnty >= 1 &&
selectedTeam?.Qnty < 1.5
? 'Improve'
: 'Poor'}
</span>
</div>
</div>
</div>
<div className=" w-full flex flex-row">
<div className="w-[90px] ">
<CircularProgressbar
value={(selectedTeam.skills / 2) * 100}
text={`${selectedTeam.skills.toFixed(1)}`}
value={(selectedTeam?.skills / 2) * 100}
text={`${selectedTeam?.skills.toFixed(1)}`}
styles={{
path: {
stroke: getColor(selectedTeam.skills),
stroke: getColor(selectedTeam?.skills),
transition: 'stroke-dashoffset 0.5s ease 0s',
},
trail: {
Expand All @@ -384,14 +386,14 @@ function TeamDetails() {
<ul className="list-disc ">
<li>Professionalism</li>
</ul>
<span style={{ color: getColor(selectedTeam.skills) }}>
{selectedTeam.skills >= 1.5 &&
selectedTeam.skills <= 2
<span style={{ color: getColor(selectedTeam?.skills) }}>
{selectedTeam?.skills >= 1.5 &&
selectedTeam?.skills <= 2
? 'Good'
: selectedTeam.skills >= 1 &&
selectedTeam.skills < 1.5
? 'Improve'
: 'Poor'}
: selectedTeam?.skills >= 1 &&
selectedTeam?.skills < 1.5
? 'Improve'
: 'Poor'}
</span>
</div>
</div>
Expand Down
17 changes: 17 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,26 @@ import UserProvider from './hook/useAuth';
import App from './App';
import ThemeProvider from './hook/ThemeProvider';

const specificMessagesToLogout = [
'User not authenticated',
'User account does not exist or has been deleted',
'User with this token no longer exist!!',
];
const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path, extensions }) => {
if (
specificMessagesToLogout.some((errorMessage) =>
message.includes(errorMessage),
)
) {
const encodedMessage = encodeURIComponent(message); // Encode the message
window.location.href = `/users/login?redirect_message=${encodedMessage}`;
localStorage.removeItem('auth');
localStorage.removeItem('auth_token');
toast.error(message);
}

if (extensions?.code === 'JWT_EXPIRED') {
window.location.pathname = '/users/login';
localStorage.removeItem('auth');
Expand Down
49 changes: 32 additions & 17 deletions src/pages/Organization/AdminLogin.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable */
'use client';
import { useApolloClient, useMutation } from '@apollo/client';
import React, { useContext, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { FaRegEnvelope, FaRegEye } from 'react-icons/fa';
Expand Down Expand Up @@ -45,6 +46,19 @@ 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');
console.log(redirectMessage);
if (redirectMessage) {
toast.error(redirectMessage);
}
};

// Call showRedirectMessage when the component mounts
useEffect(() => {
showRedirectMessage();
}, [searchParams]);

const onSubmit = async (userInput: any) => {
userInput.orgToken = orgToken;
Expand Down Expand Up @@ -76,26 +90,28 @@ function AdminLogin() {
redirect
? navigate(`${redirect}`)
: data.loginUser.user.role === 'superAdmin'
? navigate(`/organizations`)
: data.loginUser.user.role === 'admin'
? navigate(`/trainees`)
: data.loginUser.user.role === 'coordinator'
? navigate(`/trainees`)
: data.loginUser.user.role === 'manager'
? navigate(`/dashboard`)
: data.loginUser.user.role === 'ttl'
? navigate('/ttl-trainees')
: navigate('/performance');
? navigate(`/organizations`)
: data.loginUser.user.role === 'admin'
? navigate(`/trainees`)
: data.loginUser.user.role === 'coordinator'
? navigate(`/trainees`)
: data.loginUser.user.role === 'manager'
? navigate(`/dashboard`)
: data.loginUser.user.role === 'ttl'
? navigate('/ttl-trainees')
: navigate('/performance');
} else {
navigate('/dashboard');
}
},
onError: (err) => {
/* istanbul ignore next */
if (err.networkError)
toast.error('There was a problem contacting the server')
toast.error('There was a problem contacting the server');
else if (err.message.toLowerCase() !== 'invalid credential') {
const translateError = t('Please wait to be added to a program or cohort')
const translateError = t(
'Please wait to be added to a program or cohort',
);
toast.error(translateError);
} else {
/* istanbul ignore next */
Expand Down Expand Up @@ -178,7 +194,7 @@ function AdminLogin() {
{t('Welcome to')}{' '}
{orgName
? orgName.charAt(0).toUpperCase() +
orgName.slice(1).toLowerCase()
orgName.slice(1).toLowerCase()
: ''}
</h2>
<div className="border-[1px] w-10 bg-primary border-primary inline-block mb-2" />
Expand All @@ -202,7 +218,7 @@ function AdminLogin() {
data-testid="loginForm"
>
{errors.password &&
errors.password.message === t('Invalid credentials') ? (
errors.password.message === t('Invalid credentials') ? (
<div className=" bg-red-400 rounded-md w-full text-center p-4 my-4">
<small className="text-white">
{errors.password.message}
Expand Down Expand Up @@ -250,14 +266,13 @@ function AdminLogin() {
</div>
<div className="pl-4 mb-1 text-left">
{errors.password &&
errors.password.message !== t('Invalid credentials') ? (
errors.password.message !== t('Invalid credentials') ? (
<small className="text-red-600">
{errors.password.message}
</small>
) : (
''
)}

</div>
<div className="flex w-full flex-col sm:flex-row justify-between items-center rounded mb-5 mt-5">
<div className="w-50%">
Expand Down

0 comments on commit 9f2f78a

Please sign in to comment.