Skip to content

Commit 950701a

Browse files
committed
2 parents ad52314 + f77b19f commit 950701a

File tree

2 files changed

+58
-75
lines changed

2 files changed

+58
-75
lines changed

src/app/project/page.js

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
import gitlab from "../../../public/gitlab-logo-500.svg";
44
import Image from "next/image";
55
import { Card, Typography } from "@material-tailwind/react";
6-
import React, { useState, useEffect, use } from "react";
6+
import React, { useState, useEffect } from "react";
77
import { toast } from "react-hot-toast";
88
import Link from "next/link";
9-
import { redirect, useSearchParams } from "next/navigation";
9+
import { useSearchParams } from "next/navigation";
10+
import { useRouter } from "next/navigation";
1011

1112
export default function Project() {
1213
const searchParams = useSearchParams();
1314
const projectId = searchParams.get("projectId");
14-
console.log("projectId", projectId);
15-
1615
const [projectName, setProjectName] = useState("");
1716
const [projectDescription, setProjectDescription] = useState("");
1817
const [rootPath, setRootPath] = useState("");
19-
20-
const TABLE_HEAD_CR = ["Name", "Type"];
18+
const [selectedButton, setSelectedButton] = useState("");
2119
const [TABLE_ROWS_CR, setTableRowsCR] = useState([]);
20+
const TABLE_HEAD_CR = ["Name", "Type"];
21+
const router = useRouter();
2222

23+
// Fetch project details
2324
useEffect(() => {
2425
const fetchProjectDetails = async () => {
2526
try {
@@ -32,7 +33,6 @@ export default function Project() {
3233
setRootPath(project.rootPath);
3334
} else {
3435
console.error("Project not found");
35-
// Optionally, handle the case where the project is not found
3636
}
3737
} catch (error) {
3838
console.error("Failed to fetch project details:", error.message);
@@ -42,31 +42,20 @@ export default function Project() {
4242
fetchProjectDetails();
4343
}, [projectId]);
4444

45+
// Fetch table rows for resources
4546
useEffect(() => {
4647
const fetchTableRows = async () => {
4748
try {
48-
const res = await fetch(`/api/resourcelist`, {
49-
method: "GET",
50-
headers: {
51-
"Content-Type": "application/json",
52-
},
53-
});
49+
const res = await fetch(`/api/resourcelist`);
5450
const data = await res.json();
55-
console.log("API Response for /api/resourcelist:", data);
56-
// Find the resources matching the projectId
5751
const resources = data.filter(
5852
(resource) => resource.projectid === projectId
59-
); // corrected line
60-
61-
// Map the data to table rows
53+
);
6254
const rows = resources.map((element) => ({
6355
id: element._id,
6456
name: element.vmname,
6557
type: element.type,
66-
userid: element.userid,
6758
}));
68-
69-
// Update the state with the fetched rows
7059
setTableRowsCR(rows);
7160
} catch (error) {
7261
console.error("Failed to fetch resources:", error.message);
@@ -76,73 +65,54 @@ export default function Project() {
7665
fetchTableRows();
7766
}, [projectId]);
7867

68+
// Fetch project status
7969
useEffect(() => {
8070
const fetchProjectStatus = async () => {
8171
try {
82-
const res = await fetch("/api/request", {
83-
method: "GET",
84-
headers: {
85-
"Content-Type": "application/json",
86-
},
87-
});
88-
89-
if (!res.ok) {
90-
throw new Error(`Error: ${res.status} - ${res.statusText}`);
91-
}
92-
72+
const res = await fetch("/api/request");
9373
const data = await res.json();
94-
console.log("API Response:", data);
95-
96-
if (!Array.isArray(data)) {
97-
// Check if data is an array directly
98-
console.error(
99-
"Unexpected API response format. Expected an array but received:",
100-
data
101-
);
102-
return;
103-
}
104-
105-
const requests = data; // data is already the array
106-
107-
if (TABLE_ROWS_CR.length === 0 || !TABLE_ROWS_CR[0]) {
108-
console.warn("No resources found for the project.");
109-
return;
110-
}
111-
112-
const matchingRequest = requests.find(
113-
(item) => item.projectid === TABLE_ROWS_CR[0].projectid
114-
);
115-
116-
if (matchingRequest) {
117-
setSelectedButton(matchingRequest.statuspm);
118-
} else {
119-
console.warn(
120-
"No matching request found for project ID:",
121-
TABLE_ROWS_CR[0].projectid
74+
if (Array.isArray(data)) {
75+
const requests = data;
76+
const matchingRequest = requests.find(
77+
(item) => item.projectid === projectId
12278
);
79+
if (matchingRequest) {
80+
setSelectedButton(matchingRequest.statuspm);
81+
}
12382
}
12483
} catch (error) {
12584
console.error("Failed to retrieve request:", error.message);
12685
}
12786
};
12887

12988
fetchProjectStatus();
130-
}, [TABLE_ROWS_CR]); // Re-runs whenever TABLE_ROWS_CR changes
89+
}, [projectId, TABLE_ROWS_CR]);
13190

91+
// Handle delete action
13292
const handleDelete = async () => {
133-
toast.success("Resource deleted successfully");
134-
93+
toast.success("Destroy sent successfully");
13594
try {
136-
const response = await fetch(`/api/resource/?requestId=${requestId}`, {
137-
method: "DELETE",
95+
const requestTypeData = {
96+
projectid: projectId,
97+
status: "destroy",
98+
};
99+
100+
const resRequesttype = await fetch("/api/requesttype", {
101+
method: "PUT",
138102
headers: {
139103
"Content-Type": "application/json",
140104
},
105+
body: JSON.stringify(requestTypeData),
141106
});
142107

143-
if (!response.ok) {
144-
throw new Error(`Failed to delete resource: ${response.statusText}`);
108+
if (!resRequesttype.ok) {
109+
const errorMessage = await resRequesttype.text();
110+
throw new Error(
111+
`RequestType API failed: ${resRequesttype.status} - ${errorMessage}`
112+
);
145113
}
114+
115+
router.push("/projectlist");
146116
} catch (error) {
147117
console.error("Error while deleting resource:", error);
148118
toast.error("Failed to delete resource");
@@ -152,14 +122,27 @@ export default function Project() {
152122
return (
153123
<div>
154124
{/* Header */}
155-
<div className="flex flex-row justify-between items-center">
156-
<div className="flex flex-row items-center">
157-
<p className="text-5xl font-bold ml-16 my-5">{projectName}</p>
125+
<div className="flex flex-row justify-between items-center px-4 py-5">
126+
<div className="flex flex-row items-center space-x-8 ml-12">
127+
<p className="text-5xl font-bold">{projectName}</p>
128+
<span
129+
className={`rounded-2xl px-6 py-1 mt-3 ml-8 ${
130+
selectedButton === "Approved"
131+
? "bg-green-500 text-white"
132+
: selectedButton === "Rejected"
133+
? "bg-red-500 text-white"
134+
: selectedButton === "Under Review"
135+
? "bg-amber-500 text-white"
136+
: "bg-gray-500 text-white"
137+
}`}
138+
>
139+
{selectedButton}
140+
</span>
158141
</div>
159-
<div className="flex flex-row justify-between items-center px-4">
142+
<div className="flex flex-row items-center">
160143
<button
161-
className="mr-10 text-sm text-white bg-red-500 rounded py-3 px-5 hover:bg-red-600"
162-
//onClick={handleDelete}
144+
className="text-sm text-white bg-red-500 rounded py-3 px-5 hover:bg-red-600 mr-14"
145+
onClick={handleDelete}
163146
>
164147
Destroy
165148
</button>
@@ -224,7 +207,6 @@ export default function Project() {
224207
{TABLE_ROWS_CR.map(({ id, name, type }, index) => {
225208
const isOdd = index % 2 === 1;
226209
const rowBgColor = isOdd ? "bg-gray-50" : "bg-white";
227-
// console.log(TABLE_ROWS_CR);
228210
return (
229211
<tr key={id} className={`${rowBgColor} cursor-pointer`}>
230212
<td className="p-4 border-b border-blue-gray-50">

src/app/requestresource/page.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export default function RequestResource() {
9797
// Prepare JSON data for /api/requesttype
9898
const requestTypeData = {
9999
projectid: TABLE_ROWS_CR[0].projectid,
100-
status: "created",
100+
status: "create",
101101
};
102102

103103
// Send request to /api/requesttype with JSON body
@@ -122,6 +122,7 @@ export default function RequestResource() {
122122
console.error("Error while saving request:", error.message);
123123
}
124124
};
125+
125126
return (
126127
<div>
127128
<h1 className="text-5xl font-bold mx-16 my-5">{projectName}</h1>

0 commit comments

Comments
 (0)