Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Place πŸ‘¨β€πŸ’» #14

Merged
merged 21 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
47e5719
Add addPlaceReducer to store.js
tsheporamantso Feb 29, 2024
f9beb1e
Add addPlaceSlice to Redux places
tsheporamantso Feb 29, 2024
2c99c70
Rearranged imports and updated addPlaceSlice in Redux
tsheporamantso Mar 1, 2024
969a903
Fix typo in addPlaceReducer key
tsheporamantso Mar 1, 2024
10d52e0
Add AddPlace component to routes
tsheporamantso Mar 1, 2024
f769dff
Add AddPlace component
tsheporamantso Mar 1, 2024
bd20221
Update link to Add Place in Navigation
tsheporamantso Mar 1, 2024
d6813ef
Remove unused code and images from Home component
tsheporamantso Mar 1, 2024
45a55eb
Refactor AddPlace component to include form container and error messa…
tsheporamantso Mar 1, 2024
a545f0f
Commented out unnecessary code in Navigation.js
tsheporamantso Mar 1, 2024
7e147e3
Remove unnecessary h4 tag in Layout component
tsheporamantso Mar 1, 2024
e82a4bf
Import Styling component
tsheporamantso Mar 1, 2024
dc4779c
Add styles for AddPlace form
tsheporamantso Mar 1, 2024
dea2cdb
Correct styleling errors
tsheporamantso Mar 1, 2024
d3d3e17
Merge branch 'dev' into add-place
tsheporamantso Mar 1, 2024
68bfb36
Resolve lyout.js conflicts
tsheporamantso Mar 1, 2024
6329706
Resolve Navigation.js conflicts
tsheporamantso Mar 1, 2024
229990b
Merge branch 'add-place' of github.com:tsheporamantso/final-capstone-…
tsheporamantso Mar 1, 2024
de1506e
Add className to Add Place button
tsheporamantso Mar 1, 2024
a8b838e
Update button style in AddPlace.css
tsheporamantso Mar 1, 2024
c47539b
Update navigation links and add icons
tsheporamantso Mar 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/AddPlace.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.form-container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: #f5f5f5;
border-radius: 5px;
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.1);
width: 50vw;
padding: 5vw;
}

.form-container h2 {
font-size: 35px;
text-align: center;
}

.form-container input {
width: 100%;
padding: 12px 20px;
display: inline-block;
border: 1px solid #dededf;
border-radius: 5px;
}

.button {
width: 100%;
border: rgb(155, 155, 163) 2px solid;
padding: 10px;
border-radius: 5px;
color: #000;
}

.button:hover {
background-color: rgb(168, 219, 25);
color: #fff;
border: none;
}
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Layout from './Components/Layout';
import PlaceList from './Components/PlaceList';
import MyReservations from './Components/MyReservations';
import './App.css';
import AddPlace from './Components/AddPlace';

function App() {
const [loggedIn, setLoggedIn] = useState(false);
Expand All @@ -29,6 +30,7 @@ function App() {
>
<Route path="placelist" element={<PlaceList />} />
<Route path="myreservations" element={<MyReservations />} />
<Route path="addPlace" element={<AddPlace />} />
</Route>
) : null}
</Routes>
Expand Down
63 changes: 63 additions & 0 deletions src/Components/AddPlace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import React, { useState } from 'react';
import axios from 'axios';
import '../AddPlace.css';

function AddPlace() {
const [placeData, setPlaceData] = useState({
description: '',
photo: '',
location: '',
rate: '',
address: '',
user_id: localStorage.getItem('userId'),
});
const [status, setStatus] = useState('idle');
const [error, setError] = useState(null);

const handleChange = (e) => {
setPlaceData({ ...placeData, [e.target.name]: e.target.value });
};

const handleSubmit = async (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');
} catch (error) {
setStatus('failed');
setError(error.response.data);
console.error('Failed to add place:', error);
}
};

return (
<div className="form-container">
<h2>Add Place</h2>
<input type="text" name="description" placeholder="Description" value={placeData.description} onChange={handleChange} />
<br />
<input type="text" name="photo" placeholder="Photo URL" value={placeData.photo} onChange={handleChange} />
<br />
<input type="text" name="location" placeholder="Location" value={placeData.location} onChange={handleChange} />
<br />
<input type="number" name="rate" placeholder="Rate" value={placeData.rate} onChange={handleChange} />
<br />
<input type="text" name="address" placeholder="Address" value={placeData.address} onChange={handleChange} />
<br />
{' '}
{/* Add address input */}
{status === 'failed' && (
<div className="error-message">
Error:
{' '}
{error}
</div>
)}
<button className="button" type="submit" onClick={handleSubmit} disabled={status === 'loading'}>Add Place</button>
</div>
);
}

export default AddPlace;
40 changes: 40 additions & 0 deletions src/Redux/places/addPlaceSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

export const addPlace = createAsyncThunk(
'addPlace/addPlace',
async (placeData, thunkAPI) => {
try {
const userId = localStorage.getItem('userId');
const response = await axios.post(`http://localhost:3000/api/v1/users/${userId}/places`, placeData);
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.response.data);
}
},
);

const addPlaceSlice = createSlice({
name: 'addPlace',
initialState: {
status: 'idle',
error: null,
},
reducers: {},
extraReducers: (builder) => {
builder
.addCase(addPlace.pending, (state) => {
state.status = 'loading';
})
.addCase(addPlace.fulfilled, (state) => {
state.status = 'succeeded';
state.error = null;
})
.addCase(addPlace.rejected, (state, action) => {
state.status = 'failed';
state.error = action.payload;
});
},
});

export default addPlaceSlice.reducer;
3 changes: 2 additions & 1 deletion src/Redux/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// store.js
import { configureStore } from '@reduxjs/toolkit';
import placesReducer from './places/placesSlice';
import addPlaceReducer from './places/addPlaceSlice';

export default configureStore({
reducer: {
places: placesReducer,
addPlace: addPlaceReducer,
},
});
Loading