Skip to content

Commit

Permalink
Feat(JS) Added Redux & PersistGate
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorSvertoka committed Oct 10, 2023
1 parent 62f25ae commit aa3fcdc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
11 changes: 5 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom/client';

import { BrowserRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from './redux/store';
// import { PersistGate } from 'redux-persist/integration/react';
import { persistor, store } from './redux/store';
import { PersistGate } from 'redux-persist/integration/react';

import App from './components/App/App';
import './index.css';
Expand All @@ -14,9 +13,9 @@ root.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter basename="/rental-car">
{/* <PersistGate loading={null} persistor={persistor}> */}
<App />
{/* </PersistGate> */}
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</BrowserRouter>
</Provider>
</React.StrictMode>
Expand Down
20 changes: 20 additions & 0 deletions src/redux/filtersSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createSlice } from '@reduxjs/toolkit';

const filterInitialState = {
brand: '',
price: '',
mileage: { from: '', to: '' },
};

const filtersSlice = createSlice({
name: 'filters',
initialState: filterInitialState,
reducers: {
setAdvertsFilter(state, action) {
return (state = { ...state, ...action.payload });
},
},
});

export const filtersReducer = filtersSlice.reducer;
export const { setAdvertsFilter } = filtersSlice.actions;
24 changes: 23 additions & 1 deletion src/redux/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,33 @@ export const carsApi = createApi({
baseQuery: fetchBaseQuery({
baseUrl: 'https://6522edaef43b1793841510f0.mockapi.io/',
}),
tagTypes: ['Advert'],
endpoints: builder => ({
getCarsByPage: builder.query({
query: (page = 1) => `advert?page=${page}&limit=8`,
}),
getAdverts: builder.query({
query: () => '/advert',
providesTags: ['Advert'],
}),
getAdvertById: builder.query({
query: id => `/advert/${id}`,
providesTags: ['Advert'],
}),
updateFavoriteAdvertById: builder.mutation({
query: fields => ({
url: `/advert/${fields.id}`,
method: 'PUT',
body: fields,
}),
invalidatesTags: ['Advert'],
}),
}),
});

export const { useGetCarsByPageQuery } = carsApi;
export const {
useGetCarsByPageQuery,
useGetAdvertsQuery,
useGetAdvertByIdQuery,
useUpdateFavoriteAdvertByIdMutation,
} = carsApi;
1 change: 1 addition & 0 deletions src/redux/selectors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const selectAdvertsFilter = state => state.filters;
42 changes: 34 additions & 8 deletions src/redux/store.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import { configureStore } from '@reduxjs/toolkit';
import { setupListeners } from '@reduxjs/toolkit/query';
import { configureStore, combineReducers } from '@reduxjs/toolkit';
import storage from 'redux-persist/lib/storage';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
REGISTER,
} from 'redux-persist';

import { filtersReducer } from './filtersSlice';
import { carsApi } from './operations';

export const store = configureStore({
reducer: {
[carsApi.reducerPath]: carsApi.reducer,
},
const reducers = combineReducers({
[carsApi.reducerPath]: carsApi.reducer,
filters: filtersReducer,
});

const persistConfig = {
key: 'root',
version: 1,
storage,
};

const persistedReducer = persistReducer(persistConfig, reducers);

export const store = configureStore({
reducer: persistedReducer,
middleware: getDefaultMiddleware =>
getDefaultMiddleware().concat(carsApi.middleware),
getDefaultMiddleware({
serializableCheck: {
ignoredActions: [FLUSH, REHYDRATE, PAUSE, PERSIST, PURGE, REGISTER],
},
}).concat(carsApi.middleware),
});

setupListeners(store.dispatch);
export let persistor = persistStore(store);

0 comments on commit aa3fcdc

Please sign in to comment.