diff --git a/src/pages/admin/admin.js b/src/pages/admin/admin.js index 42003d1e163ae13c4225697fbd1ede2db2e3cb97..58c236a41bd1cdea262bac4150ef298ad4cc9c43 100644 --- a/src/pages/admin/admin.js +++ b/src/pages/admin/admin.js @@ -60,11 +60,15 @@ const AdminPageView = () => { const [locations, setLocations] = useState([]); const { isOpen, onOpen, onClose } = useDisclosure(); const [newLocation, setNewLocation] = useState({ - title: "", - address: "", - latitude: 0, - longitude: 0, - spaces: 0, + Title: "", + Description: "", + city: "", + street_address: "", + postcode: "", + lat: 0, + lon: 0, + spaces_available: 0, + total_spaces: 0, }); // Load locations when the page is loaded @@ -74,51 +78,40 @@ const AdminPageView = () => { // Fetch locations from the server const loadLocations = async () => { - // const response = await fetch("/api/locations"); - // const data = await response.json(); - const data = [ - { - _id: 1, - title: "sd", - address: "sd", - latitude: 0, - longitude: 0, - spaces: 0, - }, - { - _id: 2, - title: "sd", - address: "sd", - latitude: 0, - longitude: 0, - spaces: 0, - } - ]; + const response = await fetch("/api/locations"); + const data = await response.json(); + setLocations(data); }; // Add a new location const addLocation = async (location) => { - const { title, address, latitude, longitude, spaces } = location; + // const { Title, Description, city, street_address, lat, lon, total_spaces, spaces_available } = location; console.log(location); try { - const response = await fetch("/api/locations", { + const response = await fetch("/location/parking-locations", { method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(newLocation), + headers: { + Authorization: `Bearer ${localStorage.getItem("token")}`, + }, + body: JSON.stringify(location), }); if (!response.ok) { throw new Error("Failed to add location"); } setNewLocation({ - title: title, - address: address, - latitude: latitude, - longitude: longitude, - spaces: spaces, + Title: "", + Description: "", + city: "", + street_address: "", + postcode: "", + lat: 0, + lon: 0, + spaces_available: 0, + total_spaces: 0, }); toast({ title: "Added new location.", @@ -141,7 +134,7 @@ const AdminPageView = () => { // Delete a location const deleteLocation = async (id) => { try { - const response = await fetch(`/api/locations/${id}`, { + const response = await fetch(`/location/parking-locations/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, @@ -213,19 +206,25 @@ const AdminPageView = () => { <div> <Text mr={4} fontSize={18}> <strong></strong> - <strong>Title:</strong> {location.title} + <strong>Title:</strong> {location.Title} </Text> <Text flex="1"> - <strong>Address:</strong> {location.address} + <strong>Description:</strong> {location.Description} </Text> <Text flex="1"> - <strong>Longitude:</strong> {location.longitude} + <strong>Address:</strong> {location.street_address} </Text> <Text flex="1"> - <strong>Latitude:</strong> {location.latitude} + <strong>Postcode:</strong> {location.postcode} </Text> <Text flex="1"> - <strong>Spaces:</strong> {location.spaces} + <strong>Longitude:</strong> {location.lon} + </Text> + <Text flex="1"> + <strong>Latitude:</strong> {location.lat} + </Text> + <Text flex="1"> + <strong>Total Spaces:</strong> {location.total_spaces} </Text> </div> <Button @@ -255,64 +254,84 @@ const AdminPageView = () => { <ModalCloseButton /> <ModalBody> <VStack spacing="4"> - <FormControl id="title"> + <FormControl id="Title"> <FormLabel>Title</FormLabel> <Input type="text" - value={newLocation.title} + value={newLocation.Title} + onChange={(e) => + setNewLocation({ ...newLocation, Title: e.target.value }) + } + /> + </FormControl> + <FormControl id="Description"> + <FormLabel>Description</FormLabel> + <Input + type="text" + value={newLocation.Description} + onChange={(e) => + setNewLocation({ ...newLocation, Description: e.target.value }) + } + /> + </FormControl> + <FormControl id="street_address"> + <FormLabel>Street Address</FormLabel> + <Input + type="text" + value={newLocation.street_address} onChange={(e) => - setNewLocation({ ...newLocation, title: e.target.value }) + setNewLocation({ ...newLocation, street_address: e.target.value }) } /> </FormControl> - <FormControl id="address"> - <FormLabel>Address</FormLabel> + <FormControl id="city"> + <FormLabel>City</FormLabel> <Input type="text" - value={newLocation.address} + value={newLocation.city} onChange={(e) => - setNewLocation({ ...newLocation, address: e.target.value }) + setNewLocation({ ...newLocation, city: e.target.value }) } /> </FormControl> - <FormControl id="latitude"> + <FormControl id="lat"> <FormLabel>Latitude</FormLabel> <Input type="number" step="0.000001" - value={newLocation.latitude} + value={newLocation.lat} onChange={(e) => setNewLocation({ ...newLocation, - latitude: parseFloat(e.target.value), + lat: parseFloat(e.target.value), }) } /> </FormControl> - <FormControl id="longitude"> + <FormControl id="lon"> <FormLabel>Longitude</FormLabel> <Input type="number" step="0.000001" - value={newLocation.longitude} + value={newLocation.lon} onChange={(e) => setNewLocation({ ...newLocation, - longitude: parseFloat(e.target.value), + lon: parseFloat(e.target.value), }) } /> </FormControl> - <FormControl id="spaces"> - <FormLabel>Spaces</FormLabel> + <FormControl id="total_spaces"> + <FormLabel>Total Spaces</FormLabel> <NumberInput - value={newLocation.spaces} + value={newLocation.total_spaces} onChange={(value) => { const intValue = parseInt(value); if (!isNaN(intValue) && intValue > 0) { setNewLocation({ ...newLocation, - spaces: intValue, + total_spaces: intValue, }); } } diff --git a/src/pages/parking_areas/parking_areas.js b/src/pages/parking_areas/parking_areas.js index 271b267b9abba9672bb5ebcaeb2f5361a3e3d40a..b71861ed8c0ceff5c6ce9c94a57a7d02bb0d658a 100644 --- a/src/pages/parking_areas/parking_areas.js +++ b/src/pages/parking_areas/parking_areas.js @@ -20,7 +20,6 @@ import { Spinner, Flex, VStack, - Badge, Heading, Grid, GridItem, @@ -51,84 +50,21 @@ const ParkingAreasPage = () => { const handleSearchClick = async () => { try { - // const response = await fetch( - // `${API_URL}/parking-areas?postcode=${postcode}&radius=${radius}` - // ); - const data = [ - { - id: 1, - title: "Test Location1", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.1245, - spaces: 100, - }, - { - id: 2, - title: "Test Location2", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.1245, - spaces: 54, - }, - { - id: 3, - title: "Test Location3", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.14, - spaces: 43, - }, - { - id: 4, - title: "Test Location4", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 0, - }, - { - id: 5, - title: "Test Location5", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 0, - }, - { - id: 6, - title: "Test Location6", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 12, - }, - { - id: 7, - title: "Test Location7", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 10, - }, - { - id: 8, - title: "Test Location8", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 0, - }, - { - id: 9, - title: "Test Location9", - address: "123 Address Road", - latitude: -0.111245, - longitude: -0.111245, - spaces: 3, - }, - ]; - // const data = await response.json(); + const response = await fetch( + `${API_URL}/location/parking-locations?postcode=${postcode}&radius=${radius}` + ); + // const data = [ + // { + // _id: 1, + + // Title: "Test Location1", + // street_address: "123 Address Road", + // postcode: "SSFU", + // city: "London", + // spaces_available: 100, + // }, + // ]; + const data = await response.json(); setParkingAreas(data); } catch (error) { console.error(error); @@ -154,7 +90,7 @@ const ParkingAreasPage = () => { try { const response = await fetch( - `${API_URL}/parking-areas/${selectedParkingArea.id}/book`, + `${API_URL}/parking-areas/${selectedParkingArea._id}/book`, { method: "POST", headers: { @@ -233,7 +169,7 @@ const ParkingAreasPage = () => { <Grid gap={6} templateColumns={['repeat(1, 1fr)', 'repeat(2, 1fr)', 'repeat(3, 1fr)', 'repeat(4, 1fr)',]}> {parkingAreas.map((parkingArea) => ( <GridItem - key={parkingArea.id} + key={parkingArea._id} p={4} border="1px solid" borderColor="gray.200" @@ -242,20 +178,24 @@ const ParkingAreasPage = () => { > <VStack > <Heading as="h2" fontSize="xl" mb={2} textAlign="center"> - {parkingArea.title} + {parkingArea.Title} </Heading> <Text fontSize="md" mb={2} textAlign="center"> - {parkingArea.address} + {parkingArea.street_address} + </Text> + <Text fontSize="md" mb={2} textAlign="center"> + {parkingArea.city} + </Text> + <Text fontSize="md" mb={2} textAlign="center"> + {parkingArea.postcode} </Text> - <Box> - <Badge colorScheme="gray" mr={2}> - LatLng: ({parkingArea.latitude}, {parkingArea.longitude}) - </Badge> - </Box> - {parkingArea.spaces === 0 ? ( + {parkingArea.spaces_available === 0 ? ( <Text color="gray.500">No spaces available</Text> ) : ( + <> + <Text>Spaces available: {parkingArea.spaces_available}</Text> <Button onClick={() => handleParkingAreaClick(parkingArea)} colorScheme="teal">Book a space</Button> + </> )} </VStack> </GridItem>