Redux Notes

Source from:

[1]https://www.youtube.com/watch?v=HOrA-uzZpw8
[2]https://github.com/paulnguyen-mn/redux-pure-js/blob/master/main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copy from: https://github.com/paulnguyen-mn/redux-pure-js/blob/master/main.js
console.log(window.Redux)

const { createStore } = window.Redux;

// SETUP REDUX STORE
// state
// reducer
// store
const initialState = JSON.parse(localStorage.getItem('hobby_list')) || [];

const hobbyReducer = (state = initialState, action) => {
switch (action.type) {
case 'ADD_HOBBY': {
const newList = [...state];
newList.push(action.payload);

return newList;
}
default:
return state;
}
}

const store = createStore(hobbyReducer);

// -----------------

// RENDER REDUX HOBBY LIST
const renderHobbyList = (hobbyList) => {
if (!Array.isArray(hobbyList) || hobbyList.length === 0) return;

const ulElement = document.querySelector('#hobbyListId');
if (!ulElement) return;

// reset previous content of ul
ulElement.innerHTML = '';

for (const hobby of hobbyList) {
const liElement = document.createElement('li');
liElement.textContent = hobby;

ulElement.appendChild(liElement);
}
}


// RENDER INITIAL HOBBY LIST
const initialHobbyList = store.getState();
console.log(initialHobbyList);
renderHobbyList(initialHobbyList);


// HANDLE FORM SUBMIT
const hobbyFormElement = document.querySelector('#hobbyFormId');
if (hobbyFormElement) {
const handleFormSubmit = (e) => {
// prevent browser from reloading
e.preventDefault();

const hobbyTextElement = hobbyFormElement.querySelector('#hobbyTextId');
if (!hobbyTextElement) return;

console.log('SUBMIT', hobbyTextElement.value);
const action = {
type: 'ADD_HOBBY',
payload: hobbyTextElement.value
};
store.dispatch(action);

// reset form
hobbyFormElement.reset();
};

hobbyFormElement.addEventListener('submit', handleFormSubmit);
}

store.subscribe(() => {
console.log('STATE UPDATE: ', store.getState());
const newHobbyList = store.getState();
renderHobbyList(newHobbyList);

localStorage.setItem('hobby_list', JSON.stringify(newHobbyList));
});