Skip to content

Commit 2d6aa54

Browse files
committed
feat: connect API calls to save button in CreateEditSite
1 parent ec7cebc commit 2d6aa54

File tree

3 files changed

+70
-59
lines changed

3 files changed

+70
-59
lines changed

src/admin/CreateEditSite.tsx

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
Delete as DeleteIcon,
2222
} from '@mui/icons-material';
2323
import ColorPicker from 'react-pick-color';
24+
import { apiClient } from '@/utils/fetch';
25+
import { siteToSchema } from '@/utils/siteUtils';
2426

2527
interface 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

src/admin/ListSites.tsx

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@mui/material';
1313
import { Add as AddIcon } from '@mui/icons-material';
1414
import { apiClient } from '@/utils/fetch';
15-
import { components } from '@/types/api';
15+
import { siteToSchema } from '@/utils/siteUtils';
1616

1717
const parseSitesFromJSON = (jsonString: string): Site[] => {
1818
try {
@@ -85,27 +85,6 @@ export default function ListSites() {
8585
reloadSites();
8686
});
8787

88-
const siteToSchema = (site: Site): components['schemas']['Site'] => {
89-
return {
90-
name: site.name,
91-
latitude: site.latitude,
92-
longitude: site.longitude,
93-
status: siteStatusToSchema(site.status),
94-
address: site.address,
95-
cell_id: site.cell_id,
96-
color: site.color,
97-
boundary: site.boundary
98-
};
99-
}
100-
101-
const siteStatusToSchema = (siteStatus: SiteStatus): components['parameters']['SiteStatus'] => {
102-
if (siteStatus === 'unknown') {
103-
throw new Error(`Invalid site status: ${siteStatus}`);
104-
} else {
105-
return siteStatus as components['parameters']['SiteStatus'];
106-
}
107-
}
108-
10988
const deleteSite = (site: Site) => {
11089
apiClient.DELETE('/api/secure-site', {
11190
body: siteToSchema(site)
@@ -122,37 +101,7 @@ export default function ListSites() {
122101
});
123102
};
124103

125-
const editSite = (site: Site) => {
126-
apiClient.PUT('/api/secure-site', {
127-
body: siteToSchema(site)
128-
}).then(res => {
129-
const { data, error } = res;
130-
if (error) {
131-
console.error(`Failed to edit site: ${error}`);
132-
return;
133-
}
134-
console.log(`Successfully edited site: ${site.name}`);
135-
reloadSites();
136-
}).catch(err => {
137-
console.error(`Error editing site: ${err}`);
138-
});
139-
};
140104

141-
const createSite = (site: Site) => {
142-
apiClient.POST('/api/secure-site', {
143-
body: siteToSchema(site)
144-
}).then(res => {
145-
const { data, error } = res;
146-
if (error) {
147-
console.error(`Failed to create site: ${error}`);
148-
return;
149-
}
150-
console.log(`Successfully created site: ${site.name}`);
151-
reloadSites();
152-
}).catch(err => {
153-
console.error(`Error creating site: ${err}`);
154-
});
155-
};
156105
return (
157106
<Container maxWidth='md' sx={{ mt: 4, mb: 4 }}>
158107
<Paper elevation={3} sx={{ p: 3 }}>

src/utils/siteUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { components } from '@/types/api';
2+
3+
export const siteToSchema = (site: Site): components['schemas']['Site'] => {
4+
return {
5+
name: site.name,
6+
latitude: site.latitude,
7+
longitude: site.longitude,
8+
status: siteStatusToSchema(site.status),
9+
address: site.address,
10+
cell_id: site.cell_id,
11+
color: site.color,
12+
boundary: site.boundary
13+
};
14+
};
15+
16+
export const siteStatusToSchema = (siteStatus: SiteStatus): components['parameters']['SiteStatus'] => {
17+
if (siteStatus === 'unknown') {
18+
throw new Error(`Invalid site status: ${siteStatus}`);
19+
} else {
20+
return siteStatus as components['parameters']['SiteStatus'];
21+
}
22+
};

0 commit comments

Comments
 (0)