Skip to content

Commit

Permalink
add redux
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolaus-B committed Mar 21, 2024
1 parent 4d406e2 commit 917f24f
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 25 deletions.
338 changes: 329 additions & 9 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
"preview": "vite preview"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.2",
"axios": "^1.6.8",
"modern-normalize": "^2.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-redux": "^9.1.0",
"styled-components": "^6.1.8"
},
"devDependencies": {
"@types/react": "^18.2.66",
Expand Down
22 changes: 15 additions & 7 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { useState } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css'
import { useEffect, useState } from "react";
import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
import { useDispatch } from "react-redux";
import { fetchCars } from "./redux/cars/operations";
// import { selectCars } from "./redux/cars/carsSelectors";

function App() {
const [count, setCount] = useState(0)
const [count, setCount] = useState(0);
const dispatch = useDispatch();
// const cars = useSelector(selectCars);
useEffect(() => {
dispatch(fetchCars());
}, [dispatch]);

return (
<>
Expand All @@ -29,7 +37,7 @@ function App() {
Click on the Vite and React logos to learn more
</p>
</>
)
);
}

export default App
export default App;
20 changes: 12 additions & 8 deletions src/main.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App.jsx";
import "./index.css";
import { store } from "./redux/store.js";
import { Provider } from "react-redux";

ReactDOM.createRoot(document.getElementById('root')).render(
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<App />
</React.StrictMode>,
)
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
);
1 change: 1 addition & 0 deletions src/redux/cars/carsSelectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectCars = (state) => state.cars.cars;
19 changes: 19 additions & 0 deletions src/redux/cars/carsSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createSlice } from "@reduxjs/toolkit";
import { fetchCars } from "./operations";

const carsSlice = createSlice({
name: "contacts",
initialState: {
cars: [],
isLoading: false,
error: null,
},
reducers: {},
extraReducers: (builder) => {
builder.addCase(fetchCars.fulfilled, (state, action) => {
state.cars = action.payload;
});
},
});

export const carsReducer = carsSlice.reducer;
17 changes: 17 additions & 0 deletions src/redux/cars/operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createAsyncThunk } from "@reduxjs/toolkit";
import axios from "axios";

axios.defaults.baseURL = "https://6571b270d61ba6fcc0134cff.mockapi.io";

export const fetchCars = createAsyncThunk(
"cars/fetchAll",
async (_, thunkAPI) => {
try {
const response = await axios.get("/phonebook/cars");

return response.data;
} catch (e) {
return thunkAPI.rejectWithValue(e.message);
}
}
);
8 changes: 8 additions & 0 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { configureStore } from "@reduxjs/toolkit";
import { carsReducer } from "./cars/carsSlice";

export const store = configureStore({
reducer: {
cars: carsReducer,
},
});

0 comments on commit 917f24f

Please sign in to comment.