Skip to content

Commit 2deaff3

Browse files
committed
feat: create functions for editing and creating Site model via API calls
1 parent 90758fe commit 2deaff3

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

src/admin/ListSites.tsx

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,26 @@ 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';
1516

16-
const parseSitesFromJSON = (jsonString: string): Site[] => {
17+
const parseSitesFromJSON = (jsonString: string): components['schemas']['Site'][] => {
1718
try {
1819
const parsed = JSON.parse(jsonString);
1920

2021
if (!Array.isArray(parsed.sites)) {
2122
throw new Error("Invalid format: 'sites' should be an array");
2223
}
2324

24-
const sites: Site[] = parsed.sites.map((site: any): Site => {
25+
const sites: components['schemas']['Site'][] = parsed.sites.map((site: any): components['schemas']['Site'] => {
2526
return {
2627
name: site.name,
2728
latitude: site.latitude,
2829
longitude: site.longitude,
29-
status: site.status as SiteStatus,
30+
status: site.status,
3031
address: site.address,
3132
cell_id: site.cell_id,
3233
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,
3835
};
3936
});
4037

@@ -46,13 +43,17 @@ const parseSitesFromJSON = (jsonString: string): Site[] => {
4643
};
4744

4845
export default function ListSites() {
49-
const [sites, setSites] = useState<Site[]>([]);
46+
const [sites, setSites] = useState<components['schemas']['Site'][]>([]);
5047
const handleEdit = (siteName: string) => {
5148
console.log(`Edit site with ID: ${siteName}`);
5249
};
5350

5451
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+
}
5657
};
5758

5859
const handleAdd = () => {
@@ -78,6 +79,53 @@ export default function ListSites() {
7879
reloadSites();
7980
});
8081

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+
81129
return (
82130
<Container maxWidth='md' sx={{ mt: 4, mb: 4 }}>
83131
<Paper elevation={3} sx={{ p: 3 }}>
@@ -158,3 +206,4 @@ export default function ListSites() {
158206
</Container>
159207
);
160208
}
209+
}

0 commit comments

Comments
 (0)