cities filter
This commit is contained in:
36
src/features/citySelect/citySelectSlice.ts
Normal file
36
src/features/citySelect/citySelectSlice.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import {City, noCity} from "../../types/city";
|
||||
import {createSlice, PayloadAction} from "@reduxjs/toolkit";
|
||||
|
||||
export interface CitySelectState {
|
||||
cities: City[];
|
||||
selectedCity: City;
|
||||
initialized: boolean;
|
||||
}
|
||||
|
||||
const initialState: CitySelectState = {
|
||||
cities: [],
|
||||
selectedCity: noCity,
|
||||
initialized: false
|
||||
}
|
||||
|
||||
|
||||
export const citySelect = createSlice({
|
||||
name: 'citySelect',
|
||||
initialState,
|
||||
reducers: {
|
||||
initializeCitySelect: (state, action: PayloadAction<City[]>) => {
|
||||
state.cities = action.payload;
|
||||
state.initialized = true;
|
||||
if (state.cities.length > 0) state.selectedCity = state.cities[0];
|
||||
},
|
||||
selectCity: (state, action: PayloadAction<{ cityId: number }>) => {
|
||||
let selectedCity = state.cities.find(city => city.id == action.payload.cityId);
|
||||
if (!selectedCity) return;
|
||||
state.selectedCity = selectedCity;
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
export const {initializeCitySelect, selectCity} = citySelect.actions;
|
||||
export default citySelect.reducer;
|
||||
Reference in New Issue
Block a user