Skip to content

Commit d6cc936

Browse files
committed
fix: resource api query
1 parent 540e99a commit d6cc936

File tree

5 files changed

+90
-72
lines changed

5 files changed

+90
-72
lines changed

src/app/api/resource/route.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,20 @@ export async function DELETE(req) {
7979
// Parse JSON
8080
const url = new URL(req.url);
8181
const requestId = url.searchParams.get("requestId");
82+
const projectRequest = url.searchParams.get("projectRequest");
83+
84+
const query = {};
85+
86+
// Add filters dynamically
87+
if (requestId) {
88+
query._id = requestId;
89+
}
90+
if (projectRequest) {
91+
query.projectid = projectRequest;
92+
}
8293

8394
// Delete resource
84-
await Resource.deleteOne({ _id: requestId });
95+
await Resource.deleteOne(query);
8596

8697
return NextResponse.json(
8798
{ message: "Successfully deleted resource" },

src/app/projectlist/page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function Projectlist() {
3939
const projectId = project._id;
4040

4141
try {
42-
const res1 = await fetch(`/api/request?projectid=${projectId}`, {
42+
const res1 = await fetch(`/api/request?projectId=${projectId}`, {
4343
method: "GET",
4444
headers: {
4545
"Content-Type": "application/json",
@@ -68,7 +68,7 @@ export default function Projectlist() {
6868
);
6969

7070
const deleteResource = await fetch(
71-
`/api/resource?requestId=${projectId}`,
71+
`/api/resource?projectRequest=${projectId}`,
7272
{
7373
method: "DELETE",
7474
headers: {
@@ -95,7 +95,7 @@ export default function Projectlist() {
9595
}
9696
}
9797

98-
setProjects(filteredProjects); // ✅ Only set remaining projects
98+
setProjects(filteredProjects);
9999
} catch (error) {
100100
console.error("Error fetching projects:", error);
101101
} finally {

src/app/projectpm/[projectId]/page.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ export default function Projectdetails({ params }) {
6767

6868
const fetchResource = async () => {
6969
try {
70-
71-
const res = await fetch(`/api/resource/?requestId=${requestId}`, {
70+
const res = await fetch(`/api/resource/?projectRequest=${requestId}`, {
7271
method: "GET",
7372
headers: { "Content-Type": "application/json" },
7473
});
@@ -114,33 +113,35 @@ export default function Projectdetails({ params }) {
114113
}
115114
}, [requestId]);
116115

117-
const handleDelete = async () => {
118-
toast.success("Resource deleted successfully");
116+
// const handleDelete = async () => {
117+
// toast.success("Resource deleted successfully");
119118

120-
try {
121-
const response = await fetch(`/api/resource/?requestId=${requestId}`, {
122-
method: "DELETE",
123-
headers: {
124-
"Content-Type": "application/json",
125-
},
126-
});
119+
// try {
120+
// const response = await fetch(`/api/resource/?requestId=${requestId}`, {
121+
// method: "DELETE",
122+
// headers: {
123+
// "Content-Type": "application/json",
124+
// },
125+
// });
127126

128-
if (!response.ok) {
129-
throw new Error(`Failed to delete resource: ${response.statusText}`);
130-
}
127+
// if (!response.ok) {
128+
// throw new Error(`Failed to delete resource: ${response.statusText}`);
129+
// }
131130

132-
router.push("/projectops");
133-
} catch (error) {
134-
console.error("Error while deleting resource:", error);
135-
toast.error("Failed to delete resource");
136-
}
137-
};
131+
// router.push("/projectpm");
132+
// } catch (error) {
133+
// console.error("Error while deleting resource:", error);
134+
// toast.error("Failed to delete resource");
135+
// }
136+
// };
138137

139138
return (
140139
<div>
141140
{/* Details Box */}
142141
<div className="flex flex-row items-center">
143-
<h1 className="text-5xl font-bold mx-16 my-5">{projectDetails.projectName}</h1>
142+
<h1 className="text-5xl font-bold mx-16 my-5">
143+
{projectDetails.projectName}
144+
</h1>
144145
{/* <span
145146
className={`rounded-2xl px-6 py-1 mt-3 ml-8 ${
146147
selectedButton === "Approved"
@@ -167,19 +168,25 @@ export default function Projectdetails({ params }) {
167168
<p className="text-xl font-medium ml-5 -16 mt-5">
168169
Application name
169170
</p>
170-
<p className="text-lg font-normal ml-5 mt-2">{projectDetails.projectName}</p>
171+
<p className="text-lg font-normal ml-5 mt-2">
172+
{projectDetails.projectName}
173+
</p>
171174
</div>
172175
<div>
173176
<p className="text-xl font-medium mx-16 mt-5">Description</p>
174-
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.projectDescription}</p>
177+
<p className="text-lg font-normal ml-16 mt-2">
178+
{projectDetails.projectDescription}
179+
</p>
175180
</div>
176181
{/* <div>
177182
<p className="text-xl font-medium mx-16 mt-5">Last Update</p>
178183
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.lastUpdate}</p>
179184
</div> */}
180185
<div>
181186
<p className="text-xl font-medium mx-16 mt-5">Create By</p>
182-
<p className="text-lg font-normal ml-16 mt-2">{projectDetails.pathWithNamespace}</p>
187+
<p className="text-lg font-normal ml-16 mt-2">
188+
{projectDetails.pathWithNamespace}
189+
</p>
183190
</div>
184191
</div>
185192
</div>
@@ -195,12 +202,12 @@ export default function Projectdetails({ params }) {
195202
>
196203
Configure
197204
</button> */}
198-
<button
205+
{/* <button
199206
className="ml-4 text-sm text-white bg-red-500 rounded py-3 px-5 hover:bg-red-600"
200207
onClick={handleDelete}
201208
>
202209
Destroy
203-
</button>
210+
</button> */}
204211
</div>
205212
</div>
206213

@@ -209,7 +216,9 @@ export default function Projectdetails({ params }) {
209216
<div className="flex flex-col space-y-4 mt-4">
210217
<div className="flex flex-row">
211218
<p className="text-lg font-semibold w-32">Name</p>
212-
<p className="text-lg font-light items-center">{resource.resourceName}</p>
219+
<p className="text-lg font-light items-center">
220+
{resource.resourceName}
221+
</p>
213222
</div>
214223

215224
<div className="flex flex-row items-center">

src/app/requestresource/[projectid]/page.js

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Azure from "../../../../public/azure-logo.png";
88
import toast from "react-hot-toast";
99

1010
export default function RequestDetails({ params }) {
11-
const [requestId, setRequestId] = useState(null);
1211
const router = useRouter();
12+
const [requestId, setRequestId] = useState(null);
1313
const [resourceName, setResourceName] = useState("");
1414
const [region, setRegion] = useState("");
1515
const [os, setOS] = useState("");
@@ -21,51 +21,51 @@ export default function RequestDetails({ params }) {
2121

2222
useEffect(() => {
2323
params.then((resolvedParams) => {
24-
setRequestId(resolvedParams.requestId);
24+
setRequestId(resolvedParams.projectId);
2525
});
2626
}, [params]);
2727

28-
const fetchResource = async () => {
29-
try {
30-
const res = await fetch(`/api/resource/?requestId=${requestId}`, {
31-
method: "GET",
32-
headers: {
33-
"Content-Type": "application/json",
34-
},
35-
});
36-
37-
if (!res.ok) {
38-
throw new Error(`Failed to fetch resource: ${res.statusText}`);
39-
}
40-
41-
const data = await res.json();
42-
if (data.length > 0) {
43-
const resource = data[0];
44-
setResourceName(resource.vmname);
45-
setRegion(resource.region);
46-
setOS(resource.os);
47-
setAdminUser(resource.username);
48-
setAdminPassword(resource.password);
49-
setVMSize(resource.vmsize);
50-
setAllocation(resource.allocationip);
51-
setType(resource.type);
28+
useEffect(() => {
29+
if (!requestId) return;
30+
// console.log(requestId);
31+
const fetchResource = async () => {
32+
try {
33+
const res = await fetch(`/api/resource/?requestId=${requestId}`, {
34+
method: "GET",
35+
headers: {
36+
"Content-Type": "application/json",
37+
},
38+
});
39+
40+
if (!res.ok) {
41+
throw new Error(`Failed to fetch resource: ${res.statusText}`);
42+
}
43+
44+
const data = await res.json();
45+
if (data.length > 0) {
46+
const resource = data[0];
47+
setResourceName(resource.vmname);
48+
setRegion(resource.region);
49+
setOS(resource.os);
50+
setAdminUser(resource.username);
51+
setAdminPassword(resource.password);
52+
setVMSize(resource.vmsize);
53+
setAllocation(resource.allocationip);
54+
setType(resource.type);
55+
}
56+
} catch (error) {
57+
console.log("Error while fetching resource data:", error);
5258
}
53-
} catch (error) {
54-
console.log("Error while fetching resource data:", error);
55-
}
56-
};
59+
};
5760

58-
useEffect(() => {
59-
if (requestId) {
60-
fetchResource();
61-
}
62-
});
61+
fetchResource();
62+
}, [requestId]);
6363

64-
const handleDelete = () => {
64+
const handleDelete = async () => {
6565
toast.success("Resource deleted successfully");
6666

6767
try {
68-
fetch(`/api/resource/?requestId=${requestId}`, {
68+
await fetch(`/api/resource/?requestId=${requestId}`, {
6969
method: "DELETE",
7070
headers: {
7171
"Content-Type": "application/json",
@@ -85,10 +85,8 @@ export default function RequestDetails({ params }) {
8585
{/* Header */}
8686
<div className="mx-16 my-6">
8787
<h1 className="text-4xl font-bold">Create Cloud Resource</h1>
88-
<h2 className="text-lg text-gray-400 ml-1 mt-4">
89-
Create Cloud Resource → Todo List
90-
</h2>
9188
</div>
89+
9290
{/* Form Container */}
9391
<div className="bg-white text-black mx-16 my-8 p-8 rounded-lg shadow-lg">
9492
{/* Section Title */}

src/app/requestresource/page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import gitlab from "../../../public/gitlab-logo-500.svg";
44
import Image from "next/image";
55
import Link from "next/link";
66
import { Card, Typography } from "@material-tailwind/react";
7-
import React, { useEffect, useState, use } from "react";
7+
import React, { useEffect, useState } from "react";
88
import { useRouter } from "next/navigation";
99
import toast from "react-hot-toast";
1010
import { useProvider } from "../components/ConText";

0 commit comments

Comments
 (0)