-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathMapSlice.js
43 lines (37 loc) · 937 Bytes
/
MapSlice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { createSlice } from '@reduxjs/toolkit';
const initialState = {
location: {
country: '',
city: '',
},
center: {
latitude: 2.5768,
longitude: 25.1601,
},
zoom: 3.01,
};
export const mapSlice = createSlice({
name: 'map',
initialState,
reducers: {
setCenter: (state, action) => {
const { latitude, longitude } = action.payload || {};
if (latitude && longitude) {
state.center = { ...state.center, latitude, longitude };
}
},
setZoom: (state, action) => {
if (typeof action.payload === 'number') {
state.zoom = action.payload;
}
},
setLocation: (state, action) => {
const { country = '', city = '' } = action.payload || {};
state.location = { country, city };
},
clearData: (state) => {
return initialState;
},
},
});
export const { setCenter, setZoom, setLocation, clearData } = mapSlice.actions;