Skip to content

Commit

Permalink
natesan pathuko
Browse files Browse the repository at this point in the history
  • Loading branch information
mukilan2815 committed Jul 4, 2024
1 parent a6255fa commit 41daabd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
10 changes: 9 additions & 1 deletion src/Pages/Admin/Allmembers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@ const Allmembers = () => {

useEffect(() => {
const fetchData = async () => {
const token = localStorage.getItem("token");

try {
const response = await fetch(
"http://192.168.169.82:8000/membershipform/"
"http://192.168.169.82:8000/membershipform/",
{
headers: {
Authorization: token,
},
}
);

const jsonData = await response.json();
setData(jsonData);
console.log(jsonData);
Expand Down
67 changes: 50 additions & 17 deletions src/Pages/Admin/Approvalpage.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
import React, { useEffect, useState } from "react";
import axios from "axios";
import Cookies from "js-cookie";

const ApprovalPage = () => {
const [applications, setApplications] = useState([]);
const [loading, setLoading] = useState(true);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);

useEffect(() => {
const fetchApplications = async () => {
try {
const csrfToken = Cookies.get("csrftoken")
// if (!csrfToken) {
// console.log("Retrying to fetch CSRF token...");
// setTimeout(fetchApplications, 1000);
// return;
// }
console.log("CSRF Token:", csrfToken);

const token = localStorage.getItem("token");
const response = await axios.get(
"http://192.168.169.82:8000/approval/",
{
withCredentials: true,
headers: {
"X-CSRFToken": csrfToken,
Authorization: `Token ${token}`,
},
}
);
setApplications(response.data);
console.log("API Response:", response);

const combinedData = [
...response.data.pending,
...response.data.rejected,
];
setApplications(combinedData);
} catch (error) {
console.error("Error fetching data:", error);
setError(error.message);
Expand All @@ -38,7 +35,6 @@ const ApprovalPage = () => {

fetchApplications();
}, []);

if (loading) {
return <div>Loading...</div>;
}
Expand All @@ -47,6 +43,44 @@ const ApprovalPage = () => {
return <div>Error: {error}</div>;
}

const accept = async (id) => {
try {
const token = localStorage.getItem("token");
const response = await axios.post(
"http://192.168.169.82:8000/approval/",
{ id },
{
headers: {
Authorization: `Token ${token}`,
},
}
);
console.log("Accepted User ID:", id);
console.log("API Response:", response);
} catch (error) {
console.error("Error accepting user:", error);
}
};

const reject = async (id, reason) => {
try {
const token = localStorage.getItem("token");
const response = await axios.post(
"http://192.168.169.82:8000/approval/",
{ id, reason },
{
headers: {
Authorization: `Token ${token}`,
},
}
);
console.log("Accepted User ID:", id);
console.log("API Response:", response);
} catch (error) {
console.error("Error accepting user:", error);
}
};

return (
<div className="p-6">
<h1 className="text-2xl font-bold mb-4">Approval Page</h1>
Expand All @@ -73,13 +107,13 @@ const ApprovalPage = () => {
<td className="py-2 px-4 border-b">
<button
className="bg-green-500 hover:bg-green-600 text-white py-1 px-3 rounded focus:outline-none focus:shadow-outline"
// onClick={() => handleAccept(application.id)}
onClick={() => accept(application.id)}
>
Accept
</button>
<button
className="bg-red-500 hover:bg-red-600 text-white py-1 px-3 ml-2 rounded focus:outline-none focus:shadow-outline"
// onClick={() => handleReject(application.id)}
onClick={() => reject(application.id, application.reason)}
>
Reject
</button>
Expand All @@ -92,5 +126,4 @@ const ApprovalPage = () => {
</div>
);
};

export default ApprovalPage;
12 changes: 7 additions & 5 deletions src/Pages/Admin/LoginAdmin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ const LoginAdmin = () => {
const password = formData.get("password");

try {
const response = await axios.post(
"http://192.168.169.82:8000/login/",
{ username, password },
{ withCredentials: true }
);
const response = await axios.post("http://192.168.169.82:8000/login/", {
username,
password,
});

if (response.status === 200) {
const token = response.data["token"];
localStorage.setItem("token", token);
console.log("Token ", token);
navigate("/adminhome");
} else {
setError("Invalid username or password");
Expand Down

0 comments on commit 41daabd

Please sign in to comment.