-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (51 loc) · 1.14 KB
/
script.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
import { createStore } from 'https://cdn.skypack.dev/redux';
const initState = 0;
function Reducer(state = initState, action){
switch(action.type){
case 'DEPOSITE':
return state + action.payload;
case 'WITHDRAW':
return state - action.payload;
default:
return state;
}
}
//Store
const store = window.store = createStore(Reducer);
// Actions
function actionDeposite(payload){
return{
type: 'DEPOSITE',
payload
}
}
function actionWithdraw(payload) {
return {
type: 'WITHDRAW',
payload
}
}
// DOM Events
const deposite = document.querySelector('#deposite')
const withdraw = document.querySelector('#withdraw')
// Events handler
deposite.onclick = function(){
store.dispatch(actionDeposite(
Math.floor(Math.random() * 101)
));
}
withdraw.onclick = function () {
store.dispatch(actionWithdraw(
Math.floor(Math.random() * 101)
));
}
// Listener
store.subscribe(() => {
Render();
})
// Render
function Render(){
const output = document.querySelector('#output');
output.innerText = store.getState();
}
Render();