Skip to content

Commit

Permalink
Fix components & slices errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hmunish committed Nov 6, 2023
1 parent 9aa5481 commit 8e6ac46
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import AddCar from "./components/car/AddCar";
import CarDetail from "./components/car/CarDetail";
import DeleteCar from "./components/car/DeleteCar";

function App() {
const App = () => {
const loginData = useSelector((state) => state.login);
const dispatch = useDispatch();
useEffect(() => {
Expand Down Expand Up @@ -45,6 +45,6 @@ function App() {
</Routes>
</main>
);
}
};

export default App;
4 changes: 0 additions & 4 deletions src/components/Slider.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import React from "react";
// import { useDispatch, useSelector } from "react-redux";
// import { nextCar } from "../redux/cars/carsSlice";
import styles from "../CSS/Slider.module.css";
import arrow from "../assets/arrow.png";

const Slider = () => {
// const sliderIndex = useSelector((state) => state.cars.value);
// const dispatch = useDispatch();
const slider = document.querySelector("#slider");
const slideLeft = () => {
const leftMargin = +window.getComputedStyle(slider).marginLeft.slice(0, -2);
Expand Down
6 changes: 5 additions & 1 deletion src/components/car/AddCar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { useDispatch, useSelector } from "react-redux";
import { addCar } from "../../redux/cars/carsSlice";

const AddCar = () => {
const { isLoading } = useSelector((state) => state.cars);
const { isLoading, isCreated, isCreatedError } = useSelector(
(state) => state.cars,
);
const { userId } = useSelector((state) => state.login);
const dispatch = useDispatch();

Expand Down Expand Up @@ -153,6 +155,8 @@ const AddCar = () => {
</li>
<input type="submit" value="Add Car" />
</ul>
{isCreated && <p>Car added successfully</p>}
{isCreatedError && <p>{isCreatedError}</p>}
</form>
</section>
);
Expand Down
9 changes: 7 additions & 2 deletions src/components/car/DeleteCar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { deleteCar, getCars } from "../../redux/cars/carsSlice";

const DeleteCar = () => {
const { cars, isLoading, isUpdated } = useSelector((state) => state.cars);
const { userId } = useSelector((state) => state.login);
const dispatch = useDispatch();

useEffect(() => {
Expand All @@ -15,7 +16,7 @@ const DeleteCar = () => {
}

const handleDeleteCar = (carId) => {
dispatch(deleteCar({ carId }));
dispatch(deleteCar({ userId, carId }));
};

if (isLoading) {
Expand All @@ -35,7 +36,11 @@ const DeleteCar = () => {
{cars.map((car) => (
<div className="delete-car-item" key={car.id}>
<p>{car.name}</p>
<button className="delete-btn" onClick={() => handleDeleteCar(car.id)} type="button">
<button
className="delete-btn"
onClick={() => handleDeleteCar(car.id)}
type="button"
>
Delete
</button>
</div>
Expand Down
16 changes: 8 additions & 8 deletions src/components/login/login.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Link, useLocation } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import Signup from '../signup/signup';
import { logIn } from '../../redux/login/loginSlice';
import { Link, useLocation } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import Signup from "../signup/signup";
import { logIn } from "../../redux/login/loginSlice";

function Login() {
const Login = () => {
const loginData = useSelector((state) => state.login);
const dispatch = useDispatch();
const location = useLocation();
Expand All @@ -15,9 +15,9 @@ function Login() {
};

// Checking if user is already login, if yes redirect to home page
if (loginData.isLogIn) window.location.pathname = '/';
if (loginData.isLogIn) window.location.pathname = "/";

if (location.pathname === '/signup') {
if (location.pathname === "/signup") {
return <Signup />;
}

Expand All @@ -39,6 +39,6 @@ function Login() {
</Link>
</section>
);
}
};

export default Login;
4 changes: 2 additions & 2 deletions src/components/reservation/AddReservation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSearchParams } from "react-router-dom";
import { getCars } from "../../redux/cars/carsSlice";
import { addReservation } from "../../redux/reservations/reservationSlice";

function AddReservation() {
const AddReservation = () => {
const { cars, isLoading } = useSelector((state) => state.cars);
const { isCreated, isError } = useSelector((state) => state.reservations);
const { userId } = useSelector((state) => state.login);
Expand Down Expand Up @@ -81,6 +81,6 @@ function AddReservation() {
</div>
</section>
);
}
};

export default AddReservation;
4 changes: 2 additions & 2 deletions src/components/reservation/ReservationList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDispatch, useSelector } from "react-redux";
import { getReservations } from "../../redux/reservations/reservationSlice";
import { getCars } from "../../redux/cars/carsSlice";

function ReservationList() {
const ReservationList = () => {
const { userId } = useSelector((state) => state.login);
const { reservations, isLoading, isError } = useSelector(
(state) => state.reservations,
Expand Down Expand Up @@ -47,6 +47,6 @@ function ReservationList() {
}

return <p>No reservations available</p>;
}
};

export default ReservationList;
19 changes: 16 additions & 3 deletions src/redux/cars/carsSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import axios from "axios";
import { getLocalStorageAuth } from "../../utility/helper";
import { API_URL } from "../../utility/globalVariable";

axios.defaults.headers.common.Authorization = getLocalStorageAuth();

const initialState = {
cars: [],
isLoading: false,
Expand All @@ -15,6 +13,8 @@ const initialState = {
isUpdated: false,
displayedCars: [],
removedCars: [],
isCreated: false,
isCreatedError: null,
};

export const getCars = createAsyncThunk("cars/getCars", async (_, thunkAPI) => {
Expand Down Expand Up @@ -63,6 +63,7 @@ export const addCar = createAsyncThunk(
{
headers: {
"Content-Type": "multipart/form-data",
Authorization: getLocalStorageAuth(),
},
},
);
Expand All @@ -80,6 +81,11 @@ export const deleteCar = createAsyncThunk(
try {
const response = await axios.delete(
`${API_URL}/users/${userId}/cars/${carId}`,
{
headers: {
Authorization: getLocalStorageAuth(),
},
},
{
data: {
id: carId,
Expand All @@ -100,6 +106,11 @@ export const getCarById = createAsyncThunk(
try {
const response = await axios.get(
`${API_URL}/users/${userId}/cars/${carId}`,
{
headers: {
Authorization: getLocalStorageAuth(),
},
},
);
return response;
} catch (error) {
Expand Down Expand Up @@ -146,11 +157,13 @@ const carsSlice = createSlice({
},
[addCar.fulfilled]: (state, action) => {
state.isLoading = false;
state.isCreated = true;
state.cars = action.payload.status.data;
},
[addCar.rejected]: (state, action) => {
state.isLoading = false;
state.error = action.payload;
state.isCreatedError = action.payload;
state.isCreated = false;
},
[getCarById.pending]: (state) => {
state.isLoading = true;
Expand Down

0 comments on commit 8e6ac46

Please sign in to comment.