-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
77 lines (57 loc) · 1.51 KB
/
main.js
File metadata and controls
77 lines (57 loc) · 1.51 KB
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
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';
import { createStore } from 'redux';
let setStorage = function (key, val) {
window.localStorage.setItem(key, val);
};
let getStorage = function (key) {
return window.localStorage.getItem(key);
};
let setState = function (state) {
setStorage('state', JSON.stringify(state));
}
let getState = function () {
return JSON.parse(getStorage('state')) || '';
}
const header = (state = {cardIdx: 0}, action) => {
switch (action.type) {
case 'INCREMENT_CARD':
state = Object.assign({}, state, {
cardIdx: state.cardIdx + 1,
showQuestion: true
});
setState(state);
return state;
case 'SHOW_ANSWER':
state = Object.assign({}, state, {
showQuestion: false
});
setState(state);
return state;
case 'ADD_DATA':
console.log('data data', state, action);
return;
case 'SET_CARD_IDX':
state = Object.assign({}, state, {
cardIdx: +action.data.idx - 1,
showQuestion: true
});
setState(state);
return state;
default:
console.log('in swith set', getState());
return state = getState() || {
cardIdx: 0,
showQuestion: true
};
}
};
const store = createStore(header);
const render = () => {
ReactDOM.render(<App store={store}/>, document.getElementById('app')
);
};
store.subscribe(render);
render();
//ReactDOM.render(<App />, document.getElementById('app'))