You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

50 lines
1.5 KiB

import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { FilterSliceState, Sort, SortPropertyEnum } from './types';
const initialState: FilterSliceState = {
searchValue: '',
categoryId: 0,
currentPage: 1,
sort: {
name: 'популярности',
sortProperty: SortPropertyEnum.RATING_DESC,
},
};
const filterSlice = createSlice({
name: 'filters',
initialState,
reducers: {
setCategoryId(state, action: PayloadAction<number>) {
state.categoryId = action.payload;
},
setSearchValue(state, action: PayloadAction<string>) {
state.searchValue = action.payload;
},
setSort(state, action: PayloadAction<Sort>) {
state.sort = action.payload;
},
setCurrentPage(state, action: PayloadAction<number>) {
state.currentPage = action.payload;
},
setFilters(state, action: PayloadAction<FilterSliceState>) {
if (Object.keys(action.payload).length) {
state.currentPage = Number(action.payload.currentPage);
state.categoryId = Number(action.payload.categoryId);
state.sort = action.payload.sort;
} else {
state.currentPage = 1;
state.categoryId = 0;
state.sort = {
name: 'популярности',
sortProperty: SortPropertyEnum.RATING_DESC,
};
}
},
},
});
export const { setCategoryId, setSort, setCurrentPage, setFilters, setSearchValue } =
filterSlice.actions;
export default filterSlice.reducer;