|
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { |
| 3 | + Box, |
| 4 | + Container, |
| 5 | + Paper, |
| 6 | + Typography, |
| 7 | + Button, |
| 8 | + Fab, |
| 9 | + List, |
| 10 | + ListItem, |
| 11 | + ListItemText, |
| 12 | +} from '@mui/material'; |
| 13 | +import { Add as AddIcon } from '@mui/icons-material'; |
| 14 | +import { apiClient } from '@/utils/fetch'; |
| 15 | + |
| 16 | +const parseSitesFromJSON = (jsonString: string): Site[] => { |
| 17 | + try { |
| 18 | + const parsed = JSON.parse(jsonString); |
| 19 | + |
| 20 | + if (!Array.isArray(parsed.sites)) { |
| 21 | + throw new Error("Invalid format: 'sites' should be an array"); |
| 22 | + } |
| 23 | + |
| 24 | + const sites: Site[] = parsed.sites.map((site: any): Site => { |
| 25 | + return { |
| 26 | + name: site.name, |
| 27 | + latitude: site.latitude, |
| 28 | + longitude: site.longitude, |
| 29 | + status: site.status as SiteStatus, |
| 30 | + address: site.address, |
| 31 | + cell_id: site.cell_id, |
| 32 | + color: site.color, |
| 33 | + boundary: site.boundary?.map((point: any) => ({ |
| 34 | + lat: point.lat, |
| 35 | + lng: point.lng |
| 36 | + })) ?? [], |
| 37 | + }; |
| 38 | + }); |
| 39 | + |
| 40 | + return sites; |
| 41 | + } catch (error) { |
| 42 | + console.error("Failed to parse sites JSON:", error); |
| 43 | + return []; |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | + |
| 48 | +export default function NewEditSite() { |
| 49 | + const [sites, setSites] = useState<Site[]>([]); |
| 50 | + const handleEdit = (siteName: string) => { |
| 51 | + console.log(`Edit site with ID: ${siteName}`); |
| 52 | + }; |
| 53 | + |
| 54 | + const handleDelete = (siteName: string) => { |
| 55 | + console.log(`Delete site with ID: ${siteName}`); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleAdd = () => { |
| 59 | + console.log('Add new site'); |
| 60 | + }; |
| 61 | + |
| 62 | + const reloadSites = () => { |
| 63 | + apiClient |
| 64 | + .GET('/api/public-sites') |
| 65 | + .then(res => { |
| 66 | + const { data, error } = res; |
| 67 | + if (error || !data) { |
| 68 | + console.log(`Unable to query sites: ${error}`); |
| 69 | + return; |
| 70 | + } |
| 71 | + setSites(parseSitesFromJSON(JSON.stringify(data))); |
| 72 | + }) |
| 73 | + .catch(err => { |
| 74 | + return <div></div>; |
| 75 | + }); |
| 76 | + }; |
| 77 | + useEffect(() => { |
| 78 | + reloadSites(); |
| 79 | + }); |
| 80 | + |
| 81 | + return ( |
| 82 | + <Container maxWidth="md" sx={{ mt: 4, mb: 4 }}> |
| 83 | + <Paper elevation={3} sx={{ p: 3 }}> |
| 84 | + <Typography variant="h4" component="h1" gutterBottom> |
| 85 | + Site Management |
| 86 | + </Typography> |
| 87 | + |
| 88 | + <List> |
| 89 | + {sites.map((site) => ( |
| 90 | + <ListItem |
| 91 | + key={site.name} |
| 92 | + sx={{ |
| 93 | + border: '1px solid #e0e0e0', |
| 94 | + borderRadius: 1, |
| 95 | + mb: 2, |
| 96 | + bgcolor: 'background.paper', |
| 97 | + }} |
| 98 | + > |
| 99 | + <ListItemText |
| 100 | + primary={ |
| 101 | + <Typography variant="h6" component="div"> |
| 102 | + {site.name} |
| 103 | + </Typography> |
| 104 | + } |
| 105 | + sx={{ flexGrow: 1 }} |
| 106 | + /> |
| 107 | + <Box sx={{ display: 'flex', gap: 1, ml: 2 }}> |
| 108 | + <Button |
| 109 | + variant="contained" |
| 110 | + color="warning" |
| 111 | + onClick={() => handleEdit(site.name)} |
| 112 | + sx={{ |
| 113 | + backgroundColor: '#d4af37', |
| 114 | + color: 'black', |
| 115 | + '&:hover': { |
| 116 | + backgroundColor: '#b8941f', |
| 117 | + }, |
| 118 | + }} |
| 119 | + > |
| 120 | + Edit |
| 121 | + </Button> |
| 122 | + <Button |
| 123 | + variant="contained" |
| 124 | + color="error" |
| 125 | + onClick={() => handleDelete(site.name)} |
| 126 | + sx={{ |
| 127 | + backgroundColor: '#d32f2f', |
| 128 | + '&:hover': { |
| 129 | + backgroundColor: '#b71c1c', |
| 130 | + }, |
| 131 | + }} |
| 132 | + > |
| 133 | + Delete |
| 134 | + </Button> |
| 135 | + </Box> |
| 136 | + </ListItem> |
| 137 | + ))} |
| 138 | + </List> |
| 139 | + </Paper> |
| 140 | + |
| 141 | + {/* Floating Action Button */} |
| 142 | + <Fab |
| 143 | + color="primary" |
| 144 | + aria-label="add" |
| 145 | + onClick={handleAdd} |
| 146 | + sx={{ |
| 147 | + position: 'fixed', |
| 148 | + bottom: 16, |
| 149 | + right: 16, |
| 150 | + backgroundColor: '#4caf50', |
| 151 | + '&:hover': { |
| 152 | + backgroundColor: '#388e3c', |
| 153 | + }, |
| 154 | + }} |
| 155 | + > |
| 156 | + <AddIcon /> |
| 157 | + </Fab> |
| 158 | + </Container> |
| 159 | + ); |
| 160 | +} |
0 commit comments