Skip to content

Commit

Permalink
Merge branch 'develop' into ft-trainee-application
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdagreat committed Oct 4, 2024
2 parents fcb1866 + bd88713 commit 8d2b9d4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 175 deletions.
2 changes: 1 addition & 1 deletion src/components/form/SignInForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const LoginForm = () => {
const role = localStorage.getItem("roleName") as string;
if (role === "applicant") {
navigate("/applicant");
} else if (role === "superAdmin") {
} else if (role === "superAdmin" || role === "admin") {
navigate("/admin");
} else {
const searchParams = new URLSearchParams(location.search);
Expand Down
2 changes: 1 addition & 1 deletion src/components/profileDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function ProfileDropdown({
<div
className="w-full p-3 flex flex-row align-center justify-start text-gray-900 dark:text-black -100 dark:hover:bg-gray-300 dark:hover:text-gray-900 hover:bg-gray-600 hover:rounded-b-[20px] hover:text-gray-100 "
onClick={() => {
localStorage.removeItem("access_token");
localStorage.clear();
}}
>
<Link
Expand Down
9 changes: 5 additions & 4 deletions src/pages/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ const LoginPage = (props: any) => {
}
`;

return authenticated && roleName === "applicant" ?
<Navigate to="/applicant" /> : authenticated && roleName === "superAdmin" ? <Navigate to="/" />
:
(
return authenticated && roleName === "applicant" ? (
<Navigate to="/applicant" />
) : authenticated && (roleName === "superAdmin" || roleName === "admin") ? (
<Navigate to="/admin" />
) : (
<>
<div className="flex items-center dark:bg-zinc-800 ">
<div
Expand Down
2 changes: 1 addition & 1 deletion src/pages/LogoutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Icon } from "@iconify/react";
const LogoutPage = () => {
const navigate = useNavigate();
const handleLogout = async (e: any) => {
localStorage.removeItem("access_token");
localStorage.clear();
navigate("/login");
};
return (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/PageNotFound.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const PageNotFound = () => {
<Link to="/applicant">
<button>Go to Applicant Dashboard</button>
</Link>
) : role === "superAdmin" ? (
) : (role === "superAdmin" || role === "admin") ? (
<Link to="/admin">
<button>Go back to Homepage</button>
</Link>
Expand Down
248 changes: 83 additions & 165 deletions src/redux/actions/TraineeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@ import {
CREATE_CYCLE_ERROR,
FETCH_TRAINEES_REQUEST,
FETCH_TRAINEES_SUCCESS,
FETCH_TRAINEES_FAILURE,
UPDATE_TRAINEE_REQUEST,
UPDATE_TRAINEE_SUCCESS,
UPDATE_TRAINEE_FAILURE,
DELETE_TRAINEE_REQUEST,
DELETE_TRAINEE_SUCCESS,
DELETE_TRAINEE_FAILURE
FETCH_TRAINEES_FAILURE
} from "..";
import { toast } from "react-toastify";
import axios from "axios";

export const getAllTraineess = ({ page, itemsPerPage, All }: any) => async (dispatch: any) => {
dispatch({ type: FETCH_TRAINEES_REQUEST });
try {
const datas = await axios({
url: process.env.BACKEND_URL,
method: "post",
data: {
query: `
query AllTraineesDetails($input: pagination) {
interface PaginationInput {
page: number;
itemsPerPage: number;
All: boolean;
}

const createTraineeQuery = `
mutation CreateNewTraineeApplicant($input: newTraineeApplicantInput!) {
createNewTraineeApplicant(input: $input) {
_id
lastName
firstName
email
cycle_id {
id
name
}
status
}
}
`;

const getAllTraineessQuery= `
query AllTraineesDetails($input: pagination) {
allTraineesDetails(input: $input) {
_id
gender
Expand Down Expand Up @@ -58,165 +67,74 @@ export const getAllTraineess = ({ page, itemsPerPage, All }: any) => async (disp
}
}
}
`,
variables: {
input: {
page,
itemsPerPage,
All,
},
},
},
});
const trainee = await datas.data.data.allTraineesDetails;
dispatch(creator(GET_TRAINEE, trainee));
dispatch({ type: FETCH_TRAINEES_SUCCESS, payload: trainee });
} catch (error) {
console.log(error);
dispatch({ type: FETCH_TRAINEES_FAILURE, payload: error});
}
`;

const createTraineeVariables = (traineeData: any) => ({
input: {
lastName: traineeData.lastName,
firstName: traineeData.firstName,
email: traineeData.email,
cycle_id: traineeData.cycle_id,
...(traineeData.attributes && { attributes: traineeData.attributes }),
},
});

const handleGetAllTraineesSuccess = (dispatch: any, response: any) => {
const trainees = response.data.data.allTraineesDetails;
dispatch(creator(GET_TRAINEE, trainees));
dispatch({ type: FETCH_TRAINEES_SUCCESS, payload: trainees });
};

export const createTrainee = (traineeData: any) => async (dispatch: any) => {
dispatch({ type: CREATE_TRAINEES });
try {
const response = await axios({
url: process.env.BACKEND_URL,
method: "post",
data: {
query: `
mutation CreateNewTraineeApplicant($input: newTraineeApplicantInput!) {
createNewTraineeApplicant(input: $input) {
_id
lastName
firstName
email
cycle_id {
id
name
}
status
}
}`,
variables: {
input: {
lastName: traineeData.lastName,
firstName: traineeData.firstName,
email: traineeData.email,
cycle_id: traineeData.cycle_id,
...(traineeData.attributes && { attributes: traineeData.attributes }),
},
},
},
});
const handleGetAllTraineesError = (dispatch: any, error: any) => {
console.error(error);
dispatch({ type: FETCH_TRAINEES_FAILURE, payload: error });
};

if (response.data.data && response.data.data.createNewTraineeApplicant) {
toast.success("Trainee successfully created.");
dispatch(creator(CREATE_TRAINEES, response.data.data.createNewTraineeApplicant));
} else if (response.data.errors) {
const err = response.data.errors[0].message;
console.error('GraphQL Error:', err);
toast.error(err);
dispatch(creator(CREATE_CYCLE_ERROR, err));
}
} catch (error:any) {
console.error('Axios Error:', error.response?.data || error.message);
const errorMessage = error.response?.data?.errors?.[0]?.message || "An error occurred while creating the trainee.";
toast.error(errorMessage);
dispatch(creator(CREATE_CYCLE_ERROR, errorMessage));
}


const makeGraphQLRequest = async (query: string, variables: any) => {
return await axios({
url: process.env.BACKEND_URL,
method: "post",
data: { query, variables },
});
};

export const updateTrainee = (id: string, updateData: any) => async (dispatch: any) => {
dispatch({ type: UPDATE_TRAINEE_REQUEST });
try {
const response = await axios({
url: process.env.BACKEND_URL,
method: "post",
data: {
query: `
mutation UpdateTraineeApplicant($ID: ID!, $updateInput: traineeApplicantInputUpdate) {
updateTraineeApplicant(ID: $ID, updateInput: $updateInput) {
_id
gender
birth_date
Address
phone
field_of_study
education_level
currentEducationLevel
province
district
sector
isEmployed
haveLaptop
isStudent
Hackerrank_score
english_score
interview_decision
past_andela_programs
applicationPost
otherApplication
andelaPrograms
otherPrograms
understandTraining
discipline
trainee_id {
firstName
lastName
email
_id
}
}
}`,
variables: {
ID: id,
updateInput: updateData,
},
},
});
const handleSuccessResponse = (dispatch: any, data: any) => {
toast.success("Trainee successfully created.");
dispatch(creator(CREATE_TRAINEES, data));
};

if (response.data.data !== null) {
toast.success("Successfully updated.");
dispatch({ type: UPDATE_TRAINEE_SUCCESS, payload: response.data.data.updateTraineeApplicant });
} else {
const err = response.data.errors[0].message;
toast.error(err);
dispatch({ type: UPDATE_TRAINEE_FAILURE, payload: err });
}
const handleErrorResponse = (dispatch: any, error: any) => {
const errorMessage = error.response?.data?.errors?.[0]?.message || "An error occurred while creating the trainee.";
console.error('GraphQL Error:', errorMessage);
toast.error(errorMessage);
dispatch(creator(CREATE_CYCLE_ERROR, errorMessage));
};

export const getAllTrainees = ({ page, itemsPerPage, All }: PaginationInput) => async (dispatch: any) => {
dispatch({ type: FETCH_TRAINEES_REQUEST });
try {
const response = await makeGraphQLRequest(getAllTraineessQuery, { input: { page, itemsPerPage, All } });
handleGetAllTraineesSuccess(dispatch, response);
} catch (error) {
console.log(error);
dispatch({ type: UPDATE_TRAINEE_FAILURE, payload: error });
handleGetAllTraineesError(dispatch, error);
}
};

export const deleteTrainee = (email: string) => async (dispatch: any) => {
dispatch({ type: DELETE_TRAINEE_REQUEST });
export const createTrainee = (traineeData: any) => async (dispatch: any) => {
dispatch({ type: CREATE_TRAINEES });
try {
const response = await axios({
url: process.env.BACKEND_URL,
method: "post",
data: {
query: `
mutation DeleteTraineeApplicant($email: String!) {
deleteTraineeApplicant(email: $email)
}`,
variables: {
email,
},
},
});

if (response.data.data.deleteTraineeApplicant) {
toast.success("Successfully deleted.");
dispatch({ type: DELETE_TRAINEE_SUCCESS, payload: email });
} else {
const err = "Failed to delete trainee";
toast.error(err);
dispatch({ type: DELETE_TRAINEE_FAILURE, payload: err });
const response = await makeGraphQLRequest(createTraineeQuery, createTraineeVariables(traineeData));

if (response.data.data && response.data.data.createNewTraineeApplicant) {
handleSuccessResponse(dispatch, response.data.data.createNewTraineeApplicant);
} else if (response.data.errors) {
handleErrorResponse(dispatch, { response: { data: response.data } });
}
} catch (error) {
console.log(error);
dispatch({ type: DELETE_TRAINEE_FAILURE, payload: error });
} catch (error: any) {
handleErrorResponse(dispatch, error);
}
};
};


3 changes: 1 addition & 2 deletions src/redux/actions/axiosconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ config.interceptors.request.use(
(config) => {
const token = localStorage.getItem("access_token");
if (token) {
config.headers = config.headers || {};
config.headers["Authorization"] = `Bearer ${token}`;
config.headers["Authorization"] = `${token}`;
}
return config;
},
Expand Down

0 comments on commit 8d2b9d4

Please sign in to comment.