-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
51 lines (41 loc) · 1.24 KB
/
store.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
44
45
46
47
48
49
50
51
// global state of our app i.e. our global data state - store
//this is kind of where the business logic of our app is kept
function createStore(reducer) {
let currentState = reducer(undefined, {});
return {
getState: () => currentState,
dispatch: (action) => {
currentState = reducer(currentState, action);
},
};
}
const initialState = {
favourites: [],
};
//reducer is a pure function
function addFavouritesReducer(state = initialState, action) {
switch (action.type) {
case "ADD_FAVOURITE": {
const addedFavourite = action.payload.favourite;
const favourites = [...state.favourites, addedFavourite];
return { favourites };
}
case "REMOVE_FAVOURITE": {
const removedFavourite = action.payload.favourite;
const favourites = state.favourites.filter(
(favourite) => favourite.id !== removedFavourite.id
);
return { favourites };
}
default:
return state;
}
}
// const action = {
// type: "ADD_FAVOURITE",
// payload: { favourite: { title: "story1", id: 1 } },
// };
const store = createStore(addFavouritesReducer);
// store.dispatch(action);
// console.log(store.getState());
export default store;