forked from vladilenm/redux-course-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (61 loc) · 1.91 KB
/
index.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
import './styles.css'
import {applyMiddleware, createStore, compose} from 'redux'
import {composeWithDevTools} from "redux-devtools-extension"
import thunk from 'redux-thunk'
import logger from 'redux-logger'
import {rootReducer} from '../redux/rootReducer'
import {increment, decrement, asyncIncrement, changeTheme} from "../redux/actions"
const counter = document.getElementById('counter')
const addBtn = document.getElementById("add")
const subBtn = document.getElementById("sub")
const asyncBtn = document.getElementById("async")
const themeBtn = document.getElementById("theme");
// function logger(state) {
// return function(next) {
// return function(action) {
// console.log('Prev State', state.getState())
// console.log('Action', action)
// const newState = next(action)
// console.log("New State", newState)
// return newState
// }
// }
// }
// const store = createStore(
// rootReducer,
// // 0,
// compose(
// applyMiddleware(thunk, logger),
// window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
// )
// )
const store = createStore(
rootReducer, composeWithDevTools(
applyMiddleware(thunk, logger)
)
)
addBtn.addEventListener('click', () => {
store.dispatch(increment())
})
subBtn.addEventListener("click", () => {
store.dispatch(decrement())
})
asyncBtn.addEventListener("click", () => {
store.dispatch(asyncIncrement())
})
themeBtn.addEventListener('click', () => {
const newTheme = document.body.classList.contains('light')
? 'dark'
: 'light'
store.dispatch(changeTheme(newTheme))
// document.body.classList.toggle('dark')
})
store.subscribe(() => {
const state = store.getState()
counter.textContent = state.counter
document.body.className = state.theme.value;
[addBtn, subBtn, themeBtn, asyncBtn].forEach(btn => {
btn.disabled = state.theme.disabled
})
})
store.dispatch({ type: "INIT_APPLICATION" })