@@ -21,6 +21,8 @@ import {
2121 Delete as DeleteIcon ,
2222} from '@mui/icons-material' ;
2323import ColorPicker from 'react-pick-color' ;
24+ import { apiClient } from '@/utils/fetch' ;
25+ import { siteToSchema } from '@/utils/siteUtils' ;
2426
2527interface CellEntry {
2628 id : string ;
@@ -49,6 +51,37 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
4951 const [ boundaryEnabled , setBoundaryEnabled ] = useState ( true ) ;
5052 const [ boundaryPoints , setBoundaryPoints ] = useState < BoundaryPoint [ ] > ( [ ] ) ;
5153
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+
5285
5386 const handleBack = ( ) => {
5487 console . log ( 'Navigate back' ) ;
@@ -68,6 +101,11 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
68101 color : colorEnabled ? colorValue : undefined ,
69102 boundary : boundaryEnabled ? boundaryPoints . map ( point => [ parseFloat ( point . lat ) , parseFloat ( point . lng ) ] ) : undefined ,
70103 } ;
104+ if ( mode === 'edit' ) {
105+ editSite ( site ) ;
106+ } else {
107+ createSite ( site ) ;
108+ }
71109 }
72110 } ;
73111
@@ -156,11 +194,13 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
156194 }
157195 if ( siteData . boundary ) {
158196 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+ } ) ) ) ;
164204 }
165205 } catch ( error ) {
166206 console . error ( 'Failed to parse site data from URL:' , error ) ;
@@ -221,8 +261,8 @@ export default function CreateEditSite({ mode }: CreateEditSiteProps) {
221261 label = "Status"
222262 >
223263 < 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 >
226266 </ Select >
227267 </ FormControl >
228268
0 commit comments