3
3
import gitlab from "../../../public/gitlab-logo-500.svg" ;
4
4
import Image from "next/image" ;
5
5
import { Card , Typography } from "@material-tailwind/react" ;
6
- import React , { useState , useEffect , use } from "react" ;
6
+ import React , { useState , useEffect } from "react" ;
7
7
import { toast } from "react-hot-toast" ;
8
8
import Link from "next/link" ;
9
- import { redirect , useSearchParams } from "next/navigation" ;
9
+ import { useSearchParams } from "next/navigation" ;
10
+ import { useRouter } from "next/navigation" ;
10
11
11
12
export default function Project ( ) {
12
13
const searchParams = useSearchParams ( ) ;
13
14
const projectId = searchParams . get ( "projectId" ) ;
14
- console . log ( "projectId" , projectId ) ;
15
-
16
15
const [ projectName , setProjectName ] = useState ( "" ) ;
17
16
const [ projectDescription , setProjectDescription ] = useState ( "" ) ;
18
17
const [ rootPath , setRootPath ] = useState ( "" ) ;
19
-
20
- const TABLE_HEAD_CR = [ "Name" , "Type" ] ;
18
+ const [ selectedButton , setSelectedButton ] = useState ( "" ) ;
21
19
const [ TABLE_ROWS_CR , setTableRowsCR ] = useState ( [ ] ) ;
20
+ const TABLE_HEAD_CR = [ "Name" , "Type" ] ;
21
+ const router = useRouter ( ) ;
22
22
23
+ // Fetch project details
23
24
useEffect ( ( ) => {
24
25
const fetchProjectDetails = async ( ) => {
25
26
try {
@@ -32,7 +33,6 @@ export default function Project() {
32
33
setRootPath ( project . rootPath ) ;
33
34
} else {
34
35
console . error ( "Project not found" ) ;
35
- // Optionally, handle the case where the project is not found
36
36
}
37
37
} catch ( error ) {
38
38
console . error ( "Failed to fetch project details:" , error . message ) ;
@@ -42,31 +42,20 @@ export default function Project() {
42
42
fetchProjectDetails ( ) ;
43
43
} , [ projectId ] ) ;
44
44
45
+ // Fetch table rows for resources
45
46
useEffect ( ( ) => {
46
47
const fetchTableRows = async ( ) => {
47
48
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` ) ;
54
50
const data = await res . json ( ) ;
55
- console . log ( "API Response for /api/resourcelist:" , data ) ;
56
- // Find the resources matching the projectId
57
51
const resources = data . filter (
58
52
( resource ) => resource . projectid === projectId
59
- ) ; // corrected line
60
-
61
- // Map the data to table rows
53
+ ) ;
62
54
const rows = resources . map ( ( element ) => ( {
63
55
id : element . _id ,
64
56
name : element . vmname ,
65
57
type : element . type ,
66
- userid : element . userid ,
67
58
} ) ) ;
68
-
69
- // Update the state with the fetched rows
70
59
setTableRowsCR ( rows ) ;
71
60
} catch ( error ) {
72
61
console . error ( "Failed to fetch resources:" , error . message ) ;
@@ -76,73 +65,54 @@ export default function Project() {
76
65
fetchTableRows ( ) ;
77
66
} , [ projectId ] ) ;
78
67
68
+ // Fetch project status
79
69
useEffect ( ( ) => {
80
70
const fetchProjectStatus = async ( ) => {
81
71
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" ) ;
93
73
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
122
78
) ;
79
+ if ( matchingRequest ) {
80
+ setSelectedButton ( matchingRequest . statuspm ) ;
81
+ }
123
82
}
124
83
} catch ( error ) {
125
84
console . error ( "Failed to retrieve request:" , error . message ) ;
126
85
}
127
86
} ;
128
87
129
88
fetchProjectStatus ( ) ;
130
- } , [ TABLE_ROWS_CR ] ) ; // Re-runs whenever TABLE_ROWS_CR changes
89
+ } , [ projectId , TABLE_ROWS_CR ] ) ;
131
90
91
+ // Handle delete action
132
92
const handleDelete = async ( ) => {
133
- toast . success ( "Resource deleted successfully" ) ;
134
-
93
+ toast . success ( "Destroy sent successfully" ) ;
135
94
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" ,
138
102
headers : {
139
103
"Content-Type" : "application/json" ,
140
104
} ,
105
+ body : JSON . stringify ( requestTypeData ) ,
141
106
} ) ;
142
107
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
+ ) ;
145
113
}
114
+
115
+ router . push ( "/projectlist" ) ;
146
116
} catch ( error ) {
147
117
console . error ( "Error while deleting resource:" , error ) ;
148
118
toast . error ( "Failed to delete resource" ) ;
@@ -152,14 +122,27 @@ export default function Project() {
152
122
return (
153
123
< div >
154
124
{ /* 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 >
158
141
</ div >
159
- < div className = "flex flex-row justify-between items-center px-4 " >
142
+ < div className = "flex flex-row items-center" >
160
143
< 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 }
163
146
>
164
147
Destroy
165
148
</ button >
@@ -224,7 +207,6 @@ export default function Project() {
224
207
{ TABLE_ROWS_CR . map ( ( { id, name, type } , index ) => {
225
208
const isOdd = index % 2 === 1 ;
226
209
const rowBgColor = isOdd ? "bg-gray-50" : "bg-white" ;
227
- // console.log(TABLE_ROWS_CR);
228
210
return (
229
211
< tr key = { id } className = { `${ rowBgColor } cursor-pointer` } >
230
212
< td className = "p-4 border-b border-blue-gray-50" >
0 commit comments