Commit f6961248 authored by Pavel Mishakov's avatar Pavel Mishakov

93 done FRONT

parent a124cb34
...@@ -4,4 +4,5 @@ export default interface IUser { ...@@ -4,4 +4,5 @@ export default interface IUser {
password: string password: string
active: boolean active: boolean
token: string token: string
role: string
} }
\ No newline at end of file
import IUSer from "./IUser"; import IUser from "./IUser";
export default interface IUserGetDto { export default interface IUserGetDto {
_id: IUSer['_id'] _id: IUser['_id']
username: IUSer['username'] username: IUser['username']
token: IUSer['token'] token: IUser['token']
role: IUser['role']
} }
\ No newline at end of file
...@@ -55,7 +55,7 @@ export const productsSlice = createSlice({ ...@@ -55,7 +55,7 @@ export const productsSlice = createSlice({
}) })
.addCase(getProducts.fulfilled, (state, action) => { .addCase(getProducts.fulfilled, (state, action) => {
state.loadingProducts = false state.loadingProducts = false
state.products = action.payload.result as IProduct[] state.products = action.payload.result as IProduct[] || []
state.messageProducts = action.payload.message state.messageProducts = action.payload.message
}) })
.addCase(getProductById.rejected, (state) => { .addCase(getProductById.rejected, (state) => {
......
import { configureStore, ThunkAction, Action } from "@reduxjs/toolkit" import { configureStore, ThunkAction, Action, getDefaultMiddleware } from "@reduxjs/toolkit"
import { useDispatch } from 'react-redux' import { useDispatch } from 'react-redux'
import { productsSlice } from "./products/products.slice" import { productsSlice } from "./products/products.slice"
import { usersSlice } from "./users/users.slice" import { usersSlice } from "./users/users.slice"
const localStorageMiddleware = ({ getState }: any) => {
return (next: any) => (action: any) => {
const result = next(action);
localStorage.setItem('applicationState', JSON.stringify(getState()));
return result;
};
};
const reHydrateStore = () => {
if (localStorage.getItem('applicationState') !== null) {
return JSON.parse(localStorage.getItem('applicationState') || ''); // re-hydrate the store
}
};
const makeStore = () => { const makeStore = () => {
return configureStore({ return configureStore({
reducer: { reducer: {
products: productsSlice.reducer, products: productsSlice.reducer,
users: usersSlice.reducer users: usersSlice.reducer
} },
preloadedState: reHydrateStore(),
middleware: (mw) => mw().concat(localStorageMiddleware)
}) })
} }
export const store = makeStore()
const store = makeStore()
export type AppDispatch = typeof store.dispatch; export type AppDispatch = typeof store.dispatch;
export type AppStore = ReturnType<typeof makeStore>; export type AppStore = ReturnType<typeof makeStore>;
export type AppState = ReturnType<AppStore["getState"]>; export type AppState = ReturnType<AppStore["getState"]>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment