@@ -12,29 +12,26 @@ import {
12
12
} from '@mui/material' ;
13
13
import { Add as AddIcon } from '@mui/icons-material' ;
14
14
import { apiClient } from '@/utils/fetch' ;
15
+ import { components } from '@/types/api' ;
15
16
16
- const parseSitesFromJSON = ( jsonString : string ) : Site [ ] => {
17
+ const parseSitesFromJSON = ( jsonString : string ) : components [ 'schemas' ] [ ' Site' ] [ ] => {
17
18
try {
18
19
const parsed = JSON . parse ( jsonString ) ;
19
20
20
21
if ( ! Array . isArray ( parsed . sites ) ) {
21
22
throw new Error ( "Invalid format: 'sites' should be an array" ) ;
22
23
}
23
24
24
- const sites : Site [ ] = parsed . sites . map ( ( site : any ) : Site => {
25
+ const sites : components [ 'schemas' ] [ ' Site' ] [ ] = parsed . sites . map ( ( site : any ) : components [ 'schemas' ] [ ' Site' ] => {
25
26
return {
26
27
name : site . name ,
27
28
latitude : site . latitude ,
28
29
longitude : site . longitude ,
29
- status : site . status as SiteStatus ,
30
+ status : site . status ,
30
31
address : site . address ,
31
32
cell_id : site . cell_id ,
32
33
color : site . color ,
33
- boundary :
34
- site . boundary ?. map ( ( point : any ) => ( {
35
- lat : point . lat ,
36
- lng : point . lng ,
37
- } ) ) ?? [ ] ,
34
+ boundary : site . boundary ?. map ( ( point : any ) => [ point . lat , point . lng ] as [ number , number ] ) ?? undefined ,
38
35
} ;
39
36
} ) ;
40
37
@@ -46,13 +43,17 @@ const parseSitesFromJSON = (jsonString: string): Site[] => {
46
43
} ;
47
44
48
45
export default function ListSites ( ) {
49
- const [ sites , setSites ] = useState < Site [ ] > ( [ ] ) ;
46
+ const [ sites , setSites ] = useState < components [ 'schemas' ] [ ' Site' ] [ ] > ( [ ] ) ;
50
47
const handleEdit = ( siteName : string ) => {
51
48
console . log ( `Edit site with ID: ${ siteName } ` ) ;
52
49
} ;
53
50
54
51
const handleDelete = ( siteName : string ) => {
55
- console . log ( `Delete site with ID: ${ siteName } ` ) ;
52
+ const site = sites . find ( s => s . name === siteName ) ;
53
+ if ( site ) {
54
+ deleteSite ( site ) ;
55
+ reloadSites ( ) ;
56
+ }
56
57
} ;
57
58
58
59
const handleAdd = ( ) => {
@@ -78,6 +79,53 @@ export default function ListSites() {
78
79
reloadSites ( ) ;
79
80
} ) ;
80
81
82
+ const deleteSite = ( site : components [ 'schemas' ] [ 'Site' ] ) => {
83
+ apiClient . DELETE ( '/api/secure-site' , {
84
+ body : site
85
+ } ) . then ( res => {
86
+ const { data, error } = res ;
87
+ if ( error ) {
88
+ console . error ( `Failed to delete site: ${ error } ` ) ;
89
+ return ;
90
+ }
91
+ console . log ( `Successfully deleted site: ${ site . name } ` ) ;
92
+ reloadSites ( ) ;
93
+ } ) . catch ( err => {
94
+ console . error ( `Error deleting site: ${ err } ` ) ;
95
+ } ) ;
96
+ } ;
97
+
98
+ const editSite = ( site : components [ 'schemas' ] [ 'Site' ] ) => {
99
+ apiClient . PUT ( '/api/secure-site' , {
100
+ body : site
101
+ } ) . then ( res => {
102
+ const { data, error } = res ;
103
+ if ( error ) {
104
+ console . error ( `Failed to edit site: ${ error } ` ) ;
105
+ return ;
106
+ }
107
+ console . log ( `Successfully edited site: ${ site . name } ` ) ;
108
+ reloadSites ( ) ;
109
+ } ) . catch ( err => {
110
+ console . error ( `Error editing site: ${ err } ` ) ;
111
+ } ) ;
112
+ } ;
113
+
114
+ const createSite = ( site : components [ 'schemas' ] [ 'Site' ] ) => {
115
+ apiClient . POST ( '/api/secure-site' , {
116
+ body : site
117
+ } ) . then ( res => {
118
+ const { data, error } = res ;
119
+ if ( error ) {
120
+ console . error ( `Failed to create site: ${ error } ` ) ;
121
+ return ;
122
+ }
123
+ console . log ( `Successfully created site: ${ site . name } ` ) ;
124
+ reloadSites ( ) ;
125
+ } ) . catch ( err => {
126
+ console . error ( `Error creating site: ${ err } ` ) ;
127
+ } ) ;
128
+
81
129
return (
82
130
< Container maxWidth = 'md' sx = { { mt : 4 , mb : 4 } } >
83
131
< Paper elevation = { 3 } sx = { { p : 3 } } >
@@ -158,3 +206,4 @@ export default function ListSites() {
158
206
</ Container >
159
207
) ;
160
208
}
209
+ }
0 commit comments