Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:WildCodeSchool/2023-09-JS-BDX-P3-La_…
Browse files Browse the repository at this point in the history
…Rive_Droite into course_cvid
  • Loading branch information
FredD27 committed Jan 25, 2024
2 parents df131fb + a77f5d2 commit ce49b24
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 60 deletions.
14 changes: 7 additions & 7 deletions frontend/src/contexts/GlobalContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ function GlobalContextProvider({ children, apiService }) {
navigate(`/offer/${id}`);
};

// const unauthorized = () => {
// if (!isAdmin) {
// return navigate("/");
// }
// };
function unauthorized() {
if (!isAdmin) {
navigate("/");
}
}

// const handleLog = () => {
// console.log(isAdmin);
Expand Down Expand Up @@ -110,7 +110,7 @@ function GlobalContextProvider({ children, apiService }) {
apiService,
handleLogout,
goToOffer,
// unauthorized,
unauthorized,
// handleLog,
}),
[
Expand All @@ -134,7 +134,7 @@ function GlobalContextProvider({ children, apiService }) {
apiService,
handleLogout,
goToOffer,
// unauthorized,
unauthorized,
// handleLog,
]
);
Expand Down
26 changes: 3 additions & 23 deletions frontend/src/contexts/LogContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function LogContextProvider({ children }) {
const getUserFromStorage = () => {
setShowStorage(JSON.parse(localStorage.getItem("User")));
};

const handleSubmitLogIn = async () => {
try {
const data = await globalContext.apiService.post(
Expand All @@ -44,8 +45,9 @@ function LogContextProvider({ children }) {
globalContext.setSuccesMsg(false);
if (result.data.is_admin === 1) {
globalContext.navigate("/dashboard");
} else {
globalContext.navigate("/");
}
globalContext.navigate("/");
}, 2000);
} catch (err) {
console.error(err);
Expand All @@ -55,31 +57,9 @@ function LogContextProvider({ children }) {
globalContext.setErrorMsg(false);
}, 2000);
}

return null;
};

// getUserFromStorage();

// // Compare l'email.
// for (let i = 0; i < showStorage.length; i += 1) {
// // i++
// if (
// showStorage[i].email === logIn.email &&
// showStorage[i].password === logIn.password
// ) {
// // const idUser = showStorage[i].id;
// const nameUser = showStorage[i].userName;
// // console.log(nameUser);
// // console.log(idUser);

// break;
// } else {
//
// }
// }
// };

const contextValues = useMemo(
() => ({
userConnected,
Expand Down
56 changes: 56 additions & 0 deletions frontend/src/loaders/current-admin.loader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// import React, { useEffect } from "react";
// import { useGlobalContext } from "../contexts/GlobalContext";

const currentAdmin = async (apiService) => {
try {
const response = await apiService.get(
`${import.meta.env.VITE_BACKEND_URL}/api/users/me`
);
// console.log(response.data.is_admin);
// console.log(response.data);
// if (response.data.is_admin !== 1) {
// // console.log("Unauthorized !");
// return navigate("/");
// }
return response;
} catch (err) {
console.error(err.message);
return null;
}
};

export default currentAdmin;

// function AdminChecker() {
// const globalContext = useGlobalContext();

// const currentAdmin = async (apiService) => {
// try {
// const response = await apiService.get(
// `${import.meta.env.VITE_BACKEND_URL}/api/users/me`
// );
// // console.log(response.data.is_admin);
// console.log(response.data);
// if (response.data.is_admin !== 1) {
// // console.log("Unauthorized !");
// return globalContext.navigate("/");
// }
// // return response;
// } catch (err) {
// console.error(err.message);
// return null;
// }
// };

// useEffect(() => {
// const checkAdmin = async () => {
// await currentAdmin(globalContext.apiService);
// };

// checkAdmin();
// }, [globalContext.apiService]);

// return <div>AdminChecker is checking...</div>;
// }

// export default AdminChecker;
33 changes: 33 additions & 0 deletions frontend/src/loaders/current-requests-profil.loader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const currentRequestsUserProfile = async (apiService) => {
try {
// D'abord, on va chercher le CV de l'utilisateur, ce qui nous intéresse est l'id du CV
const cvData = await apiService.get(
`${import.meta.env.VITE_BACKEND_URL}/api/users/id/cvs` // TODO: remplacer le 5 par l'id de l'utilisateur connecté
// `${import.meta.env.VITE_BACKEND_URL}/api/users${cvData.data.id}/cvs` // Get CV avec l'id de l'user connecté.
);
// console.log(cvData.data.id);

// Ensuite, on va chercher les expériences de l'utilisateur via l'id du CV qu'on vient de récupérer
// le but est de pouvoir faire SELECT * FROM experiences WHERE cv_id = cvData.data.id
const experienceData = await apiService.get(
`${import.meta.env.VITE_BACKEND_URL}/api/experiences/by-cv-id/${
cvData.data.id
}`
);
const courseData = await apiService.get(
`${import.meta.env.VITE_BACKEND_URL}/api/courses/by-cv-id/${
cvData.data.id
}`
);

return {
experiences: experienceData.data,
courses: courseData.data,
};
} catch (err) {
console.error(err.message);
return null;
}
};

export default currentRequestsUserProfile;
34 changes: 10 additions & 24 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Import Composant React.
import React from "react";
import ReactDOM from "react-dom";
import { createBrowserRouter, RouterProvider } from "react-router-dom";
// Import Composants.
import App from "./App";
import Home from "./pages/HomeOffer/Home";
import ReadOffer from "./pages/Offer/ReadOffer";
import SignIn from "./pages/Connexion/SignIn";
import LogIn from "./pages/Connexion/LogIn";
import History from "./pages/Historique/History";
import currentUserProfileLoader from "./loaders/current-user-profil.loader";
import Favoris from "./pages/Favoris/Favoris";
import UserProfileModel from "./pages/ProfileUser/UserProfileModel";
import AddExperience from "./pages/Experience/AddExperience";
Expand All @@ -23,6 +24,10 @@ import SignContextProvider from "./contexts/SignContext";
import LogContextProvider from "./contexts/LogContext";
import GlobalContextProvider from "./contexts/GlobalContext";
import UserContextProvider from "./contexts/UserContext";
// Import de loaders.
import currentUserProfileLoader from "./loaders/current-user-profil.loader";
import currentRequestsUserProfile from "./loaders/current-requests-profil.loader";
import currentAdmin from "./loaders/current-admin.loader";
// Import de classe.
import ApiService from "./services/api.service";
// Import Styles.
Expand Down Expand Up @@ -77,6 +82,7 @@ const router = createBrowserRouter([
},
{
path: "/edit-profile",
loader: async () => currentRequestsUserProfile(apiService),
element: (
<SignContextProvider>
<UserProfileModel />
Expand All @@ -101,36 +107,16 @@ const router = createBrowserRouter([
path: "/dashboard",
element: (
<AdminContextProvider>
{/* <AdminChecker> */}
<Dashboard1 />
{/* </AdminChecker> */}
</AdminContextProvider>
),
// loader: async () => {
// try {
// const response = await apiService.get(
// `${import.meta.env.VITE_BACKEND_URL}/api/users/me`
// );
// // return preloadUser: data ?? null;
// // console.log(response.data.is_admin);
// if (response.data.is_admin !== 1) {
// // console.log("Unauthorized !");
// Navigate("/");
// }
// } catch (err) {
// console.error(err.message);
// return null;
// }
// },
loader: async () => currentAdmin(apiService),
children: [
{
path: "/dashboard/user",
element: <Dashboard3 />,
// loader: async () => {
// try {
// // GET /api/users
// // si ok => renvoir données
// // si ko => null
// }
// },
},
{
path: "/dashboard/candidates",
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/pages/Dashboard/Dashboard1.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// import React, { useEffect } from "react";
import React, { useEffect } from "react";
import { Outlet } from "react-router-dom";
import RowDash from "../../components/Dashboards/RowDash";
import TitleDashboard from "../../components/Dashboards/TitleDashboard";
import ButtonMini from "../../components/Boutons/ButtonMini";
import { useGlobalContext } from "../../contexts/GlobalContext";
// import { useAdminContext } from "../../contexts/AdminConte";

function Dashboard1() {
const { navigate } = useGlobalContext();
const { navigate, unauthorized } = useGlobalContext();

const handleAddOffer = () => {
navigate("/dashboard/offer");
};

// useEffect(() => {
// unauthorized();
// }, []);
useEffect(() => {
unauthorized();
}, []);

return window.location.pathname === "/dashboard" ||
window.location.pathname === "/dashboard/" ? (
Expand Down

0 comments on commit ce49b24

Please sign in to comment.