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

Backend integration update #14

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
550 changes: 429 additions & 121 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@
]
},
"devDependencies": {
"@babel/core": "^7.23.9",
"@babel/eslint-parser": "^7.23.10",
"@babel/plugin-syntax-jsx": "^7.23.3",
"@babel/preset-react": "^7.23.3",
"@babel/core": "^7.21.3",
"@babel/eslint-parser": "^7.21.3",
"@babel/plugin-syntax-jsx": "^7.18.6",
"@babel/preset-react": "^7.18.6",
"autoprefixer": "^10.4.17",
"eslint": "^8.56.0",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"postcss": "^8.4.35",
"stylelint": "^13.13.1",
Expand Down
6 changes: 6 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<title>BookCar App</title>
</head>
<body>
<div id="root" class="flex h-screen"></div>
<div id="root" class="flex h-screen overflow-hidden"></div>
</body>
</html>
49 changes: 49 additions & 0 deletions src/app/redux/AppDataSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

export const fetchAppData = createAsyncThunk('cars/fetchAppData', async () => {
const response = await axios.get('http://localhost:4000/api/v1/initial_data');
return response.data;
});

const initialState = {
appData: {
cars: [],
cities: [],
engine_types: [],
},
loading: false,
error: null,
};

const appDataSlice = createSlice({
name: 'cars',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchAppData.pending, (state) => ({
...state,
loading: true,
error: null,
}))
.addCase(fetchAppData.fulfilled, (state, action) => ({
...state,
loading: false,
error: null,
appData: {
...state.appData,
cars: action.payload.data.cars,
cities: action.payload.data.cities,
engine_type: action.payload.data.engine_types,
},
}))
.addCase(fetchAppData.rejected, (state, action) => ({
...state,
loading: false,
error: action.error.message,
}));
},
});

export default appDataSlice.reducer;
37 changes: 0 additions & 37 deletions src/app/redux/CarsSlice.js

This file was deleted.

44 changes: 44 additions & 0 deletions src/app/redux/carDetailsSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

// Async thunk for fetching car details
export const fetchCarDetails = createAsyncThunk(
'carDetails/fetchCarDetails',
async (id) => {
const response = await axios.get(`http://localhost:4000/api/v1/cars/${id}`);
return response.data;
},
);

// Initial state
const initialState = {
carDetails: null,
status: 'idle',
error: null,
};

// Slice
const carDetailsSlice = createSlice({
name: 'carDetails',
initialState,
reducers: {},
extraReducers: (builder) => {
builder
.addCase(fetchCarDetails.pending, (state) => ({
...state,
status: 'loading',
}))
.addCase(fetchCarDetails.fulfilled, (state, action) => ({
...state,
status: 'succeeded',
carDetails: action.payload.data,
}))
.addCase(fetchCarDetails.rejected, (state, action) => ({
...state,
status: 'failed',
error: action.error.message,
}));
},
});

export default carDetailsSlice.reducer;
6 changes: 4 additions & 2 deletions src/app/store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { configureStore } from '@reduxjs/toolkit';
import carsReducer from './redux/CarsSlice';
import AppDataSlice from './redux/AppDataSlice';
import carDetailsSlice from './redux/carDetailsSlice';

const store = configureStore({
reducer: {
cars: carsReducer,
appData: AppDataSlice,
carDetails: carDetailsSlice,
},
});

Expand Down
30 changes: 30 additions & 0 deletions src/components/CarList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';

function CarList({ cars }) {
return (
<div>
{cars?.map((car) => (
<Link key={car.id} to={`/cars/${car.id}`} className="block mb-4">
<img src={car.image_url} alt={car.name} className="w-full h-64 object-cover" />
<h2 className="text-xl font-bold mt-2">{car.name}</h2>
<p>{car.description}</p>
</Link>
))}
</div>
);
}

CarList.propTypes = {
cars: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
image_url: PropTypes.string.isRequired,
}),
).isRequired,
};

export default CarList;
6 changes: 3 additions & 3 deletions src/components/RequireAuth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Navigate, Outlet } from 'react-router-dom';
import PropTypes from 'prop-types';

const RequireAuth = ({ allowedRoles }) => {
const isLogin = true;
const role = 'user';
const isLogin = false;
const role = '';

if (!isLogin) {
return <Navigate to="/login" />;
} if (allowedRoles && !allowedRoles.includes(role)) {
} if (!allowedRoles.includes(role)) {
return <Navigate to="/unathorised" />;
}
return <Outlet />;
Expand Down
4 changes: 2 additions & 2 deletions src/components/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { NavLink, Link } from 'react-router-dom';
import Logo from 'assets/imgs/transparent-logo.png';

const SideNavBar = () => {
const isLogin = true;
const role = 'admin';
const isLogin = false;
const role = '';
return (
<aside className="flex flex-col items-center w-60 text-dark-blue" aria-label="Sidebar">
<div className="pb-12 pt-4">
Expand Down
20 changes: 14 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,26 @@ const router = createBrowserRouter(
children: [
{
path: 'reservations',
element: <MyResevation />,
},
{
path: 'reservations/new',
element: <AddReserve />,
children: [
{
index: true,
element: <MyResevation />,
},
{
path: 'new',
element: <AddReserve />,
},
],
},
],
},
{
path: 'cars',
element: <Home />,
children: [
{
index: true,
element: <Home />,
},
{
path: ':id',
element: <CarDetails />,
Expand Down
Loading
Loading