|
1 | 1 | import React, { useState, useEffect } from "react"; |
2 | 2 | import ProfileComponents from "../../shared/components/Profile/ProfileComponents.tsx"; |
3 | 3 | import { useParams } from "react-router"; |
4 | | -import SEO from "../../shared/components/SEO.tsx"; |
5 | 4 | import { useAuth } from "../../context/AuthContext.tsx"; |
6 | 5 |
|
7 | 6 | export default function StaffPage() { |
8 | 7 | const { auth } = useAuth(); |
9 | 8 |
|
10 | | - if (!auth.isAuthenticated) { |
11 | | - window.location.href = "/login"; |
12 | | - } |
13 | | - |
14 | 9 | const { staffId } = useParams(); |
15 | 10 | const [profile, setProfile] = useState<null | boolean>(null); |
16 | 11 |
|
17 | | - const checkProfile = (data) => { |
18 | | - return data.name && data.image && data.department && data.description; |
19 | | - }; |
| 12 | + useEffect(() => { |
| 13 | + const fetchProfile = async () => { |
| 14 | + const response = await fetch( |
| 15 | + `${process.env.REACT_APP_BACKEND_SERVER}/staff/${staffId}`, { |
| 16 | + headers: { |
| 17 | + Authorization: `Bearer ${auth.token}`, |
| 18 | + }, |
| 19 | + } |
| 20 | + ); |
20 | 21 |
|
21 | | - const fetchProfile = async () => { |
22 | | - const response = await fetch( |
23 | | - `${process.env.REACT_APP_BACKEND_SERVER}/staff/${staffId}`, { |
24 | | - headers: { |
25 | | - Authorization: `Bearer ${auth.token}`, |
26 | | - }, |
27 | | - } |
28 | | - ); |
| 22 | + if (!response.ok) { |
| 23 | + setProfile(false); |
| 24 | + } else { |
| 25 | + const data = await response.json(); |
| 26 | + if (checkProfile(data)) { |
| 27 | + setProfile(data); |
| 28 | + } else { |
| 29 | + setProfile(false); |
| 30 | + console.log(data); |
| 31 | + } |
| 32 | + } |
| 33 | + }; |
29 | 34 |
|
30 | | - if (!response.ok) { |
| 35 | + if (!auth.isAuthenticated) { |
| 36 | + window.location.href = "/login"; |
| 37 | + } else if (!staffId) { |
31 | 38 | setProfile(false); |
32 | 39 | } else { |
33 | | - const data = await response.json(); |
34 | | - if (checkProfile(data)) { |
35 | | - setProfile(data); |
36 | | - } else { |
37 | | - setProfile(false); |
38 | | - console.log(data); |
39 | | - } |
| 40 | + fetchProfile(); |
40 | 41 | } |
41 | | - }; |
42 | | - |
43 | | - useEffect(() => { |
44 | | - fetchProfile(); |
45 | | - }, []); |
| 42 | + const checkProfile = (data: { name: string; image: string; department: string; description: string }) => { |
| 43 | + return data.name && data.image && data.department && data.description; |
| 44 | + }; |
| 45 | + }, [auth.token, staffId, auth.isAuthenticated]); |
46 | 46 |
|
47 | 47 | return ( |
48 | 48 | <> |
49 | | - <SEO title="Staff - Labconnect" description="Labconnect staff page" /> |
50 | 49 | {profile === null && "Loading..."} |
51 | | - {profile && typeof profile === "object" && <ProfileComponents profile={profile} staffId={staffId} />} |
52 | | - {profile === false && "Profile not found"} |
| 50 | + {profile && typeof profile === "object" && staffId && <ProfileComponents profile={profile} id={staffId} staff={true} />} |
| 51 | + {profile === false && <h1>Profile not found</h1>} |
53 | 52 | </> |
54 | 53 | ); |
55 | 54 | }; |
0 commit comments