@@ -21,6 +21,8 @@ import {
21
21
Delete as DeleteIcon ,
22
22
} from '@mui/icons-material' ;
23
23
import ColorPicker from 'react-pick-color' ;
24
+ import { apiClient } from '@/utils/fetch' ;
25
+ import { siteToSchema } from '@/utils/siteUtils' ;
24
26
25
27
interface CellEntry {
26
28
id : string ;
@@ -49,6 +51,37 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
49
51
const [ boundaryEnabled , setBoundaryEnabled ] = useState ( true ) ;
50
52
const [ boundaryPoints , setBoundaryPoints ] = useState < BoundaryPoint [ ] > ( [ ] ) ;
51
53
54
+ const editSite = ( site : Site ) => {
55
+ apiClient . PUT ( '/api/secure-site' , {
56
+ body : siteToSchema ( site )
57
+ } ) . then ( res => {
58
+ const { data, error } = res ;
59
+ if ( error ) {
60
+ console . error ( `Failed to edit site: ${ error } ` ) ;
61
+ return ;
62
+ }
63
+ console . log ( `Successfully edited site: ${ site . name } ` ) ;
64
+
65
+ } ) . catch ( err => {
66
+ console . error ( `Error editing site: ${ err } ` ) ;
67
+ } ) ;
68
+ } ;
69
+
70
+ const createSite = ( site : Site ) => {
71
+ apiClient . POST ( '/api/secure-site' , {
72
+ body : siteToSchema ( site )
73
+ } ) . then ( res => {
74
+ const { data, error } = res ;
75
+ if ( error ) {
76
+ console . error ( `Failed to create site: ${ error } ` ) ;
77
+ return ;
78
+ }
79
+ console . log ( `Successfully created site: ${ site . name } ` ) ;
80
+ } ) . catch ( err => {
81
+ console . error ( `Error creating site: ${ err } ` ) ;
82
+ } ) ;
83
+ } ;
84
+
52
85
53
86
const handleBack = ( ) => {
54
87
console . log ( 'Navigate back' ) ;
@@ -68,6 +101,11 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
68
101
color : colorEnabled ? colorValue : undefined ,
69
102
boundary : boundaryEnabled ? boundaryPoints . map ( point => [ parseFloat ( point . lat ) , parseFloat ( point . lng ) ] ) : undefined ,
70
103
} ;
104
+ if ( mode === 'edit' ) {
105
+ editSite ( site ) ;
106
+ } else {
107
+ createSite ( site ) ;
108
+ }
71
109
}
72
110
} ;
73
111
@@ -156,11 +194,13 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
156
194
}
157
195
if ( siteData . boundary ) {
158
196
setBoundaryEnabled ( true ) ;
159
- setBoundaryPoints ( siteData . boundary . map ( ( point : [ number , number ] ) => ( {
160
- id : Date . now ( ) . toString ( ) + point . join ( ',' ) ,
161
- lat : point [ 0 ] . toString ( ) ,
162
- lng : point [ 1 ] . toString ( )
163
- } ) ) ) ;
197
+ setBoundaryPoints ( siteData . boundary
198
+ . filter ( ( point : [ number , number ] ) => point && point [ 0 ] !== null && point [ 1 ] !== null )
199
+ . map ( ( point : [ number , number ] , index : number ) => ( {
200
+ id : Date . now ( ) . toString ( ) + index ,
201
+ lat : point [ 0 ] . toString ( ) ,
202
+ lng : point [ 1 ] . toString ( )
203
+ } ) ) ) ;
164
204
}
165
205
} catch ( error ) {
166
206
console . error ( 'Failed to parse site data from URL:' , error ) ;
@@ -221,8 +261,8 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
221
261
label = "Status"
222
262
>
223
263
< MenuItem value = "active" > Active</ MenuItem >
224
- < MenuItem value = "inactive" > Inactive </ MenuItem >
225
- < MenuItem value = "maintenance" > Maintenance </ MenuItem >
264
+ < MenuItem value = "confirmed" > Confirmed </ MenuItem >
265
+ < MenuItem value = "in-conversation" > In Conversation </ MenuItem >
226
266
</ Select >
227
267
</ FormControl >
228
268
0 commit comments