diff --git a/public/image.svg b/public/image.svg new file mode 100644 index 0000000..60db6cc --- /dev/null +++ b/public/image.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/public/index.html b/public/index.html index aa069f2..202c20b 100644 --- a/public/index.html +++ b/public/index.html @@ -2,7 +2,7 @@
- + { const [loggedIn, setLoggedIn] = useState(false); useEffect(() => { @@ -42,6 +42,6 @@ function App() { ); -} +}; export default App; diff --git a/src/Components/AddPlace.js b/src/Components/AddPlace.js index 9962005..463b826 100644 --- a/src/Components/AddPlace.js +++ b/src/Components/AddPlace.js @@ -1,8 +1,14 @@ import React, { useState } from 'react'; -import axios from 'axios'; +import { useDispatch, useSelector } from 'react-redux'; +import { addPlace } from '../Redux/places/addPlaceSlice'; +import { fetchPlaces } from '../Redux/places/placesSlice'; import '../AddPlace.css'; -function AddPlace() { +const AddPlace = () => { + const dispatch = useDispatch(); + const status = useSelector((state) => state.addPlace.status); + const error = useSelector((state) => state.addPlace.error); + const initialPlaceData = { description: '', photo: '', @@ -13,51 +19,48 @@ function AddPlace() { user_id: localStorage.getItem('userId'), }; const [placeData, setPlaceData] = useState(initialPlaceData); - const [status, setStatus] = useState('idle'); - const [error, setError] = useState(null); + const [errorMessage, setErrorMessage] = useState(''); const handleChange = (e) => { setPlaceData({ ...placeData, [e.target.name]: e.target.value }); }; - const handleSubmit = async (e) => { + const handleSubmit = (e) => { e.preventDefault(); - setStatus('loading'); - try { - const userId = localStorage.getItem('userId'); - await axios.post(`http://localhost:3000/api/v1/users/${userId}/places`, placeData); - setStatus('succeeded'); - setPlaceData(initialPlaceData); - } catch (error) { - setStatus('failed'); - setError(error.response.data); + const values = Object.values(placeData); + if (values.some((value) => !value)) { + setErrorMessage('Please fill in all fields'); + setTimeout(() => { + setErrorMessage(''); + }, 5000); // Clear error message after 5 seconds + return; } + dispatch(addPlace(placeData)) + .then(() => dispatch(fetchPlaces())); }; return (Are you sure?
+ + +Price per night:$ - {detailsPage.pricepernight} + {typeof detailsPage.pricepernight === 'string' ? parseFloat(detailsPage.pricepernight).toFixed(2) : detailsPage.pricepernight}
@@ -61,9 +61,8 @@ function DetailsPage() {Price:$ {' '} - {reservation.price} + {typeof reservation.price === 'string' ? parseFloat(reservation.price).toFixed(2) : reservation.price}