Skip to content

Commit dfd9f30

Browse files
committed
update staff page
1 parent f231f87 commit dfd9f30

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

src/staff/pages/Staff.tsx

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
11
import React, { useState, useEffect } from "react";
22
import ProfileComponents from "../../shared/components/Profile/ProfileComponents.tsx";
33
import { useParams } from "react-router";
4-
import SEO from "../../shared/components/SEO.tsx";
54
import { useAuth } from "../../context/AuthContext.tsx";
65

76
export default function StaffPage() {
87
const { auth } = useAuth();
98

10-
if (!auth.isAuthenticated) {
11-
window.location.href = "/login";
12-
}
13-
149
const { staffId } = useParams();
1510
const [profile, setProfile] = useState<null | boolean>(null);
1611

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+
);
2021

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+
};
2934

30-
if (!response.ok) {
35+
if (!auth.isAuthenticated) {
36+
window.location.href = "/login";
37+
} else if (!staffId) {
3138
setProfile(false);
3239
} 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();
4041
}
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]);
4646

4747
return (
4848
<>
49-
<SEO title="Staff - Labconnect" description="Labconnect staff page" />
5049
{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>}
5352
</>
5453
);
5554
};

0 commit comments

Comments
 (0)