-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.html
More file actions
186 lines (177 loc) · 4.32 KB
/
Copy pathmain.html
File metadata and controls
186 lines (177 loc) · 4.32 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.1/redux.js" integrity="sha512-aU+9st6E3LYPknXJiOkhUXxYz/QbB1IDf1YUYzCCbgiwOCu2g/1pH+68ROdxC3clouCOVfO6u2g7qoB43rfmQg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<div id="subject"></div>
<div id="TOC"></div>
<div id="control"></div>
<div id="content"></div>
<script>
function subject(){
document.querySelector('#subject').innerHTML = `
<header>
<h1>REDUX WEB</h1>
Hello, Redux WEB!
</header>
`
}
function TOC() { //목록 나타낼 때
var state = store.getState(); // store의 state 불러오기
var i = 0;
var liTags = '';
while(i<state.contents.length) {
liTags = liTags + `
<li>
<a onclick="
event.preventDefault();
var action = {type:'SELECT', id:${state.contents[i].id}};
store.dispatch(action);
" href="${state.contents[i].id}">
${state.contents[i].title}
</a>
</li>
`;
i = i + 1;
}
// console.log(state);
document.querySelector('#TOC').innerHTML = `
<nav>
<ol>${liTags}</ol>
</nav>
`
}
function control() {
document.querySelector('#control').innerHTML = `
<ul>
<li><a onClick="
event.preventDefault();
store.dispatch({
type: 'CHANGE_MODE'
});
" href="/create">create</a></li>
<li><input onClick="
store.dispatch({
type:'DELETE'
});
" type="button" value="delete"/></li>
</ul>
`
}
function article() {
var state = store.getState(); // store의 state 불러오기
if(state.mode === 'create') {
document.querySelector('#content').innerHTML = `
<article>
<form onSubmit="
event.preventDefault();
var _title = this.title.value;
var _desc = this.desc.value;
store.dispatch({
type:'CREATE',
title: _title,
desc: _desc
})
">
<p>
<input type="text" name="title" placeholder="title">
</p>
<p>
<textarea name="desc" placeholder="description"></textarea>
</p>
<p>
<input type="submit" >
</p>
</form>
</article>
`
} else if(state.mode === 'read') {
var i = 0;
var aTitle, aDesc;
while(i < state.contents.length) {
if(state.contents[i].id === state.selected_id){
aTitle = state.contents[i].title;
aDesc = state.contents[i].desc;
break;
}
i = i + 1;
}
document.querySelector('#content').innerHTML = `
<article>
<h2>${aTitle}</h2>
${aDesc}
</article>
`
} else if (state.mode === 'welcome') {
document.querySelector('#content').innerHTML = `
<article>
<h2>Welcome</h2>
Hello, Redux!!!
</article>
`
}
}
function reducer(state, action){ //이전의 state 값, action: 호출된 이후의 값
if (state === undefined){ // state의 초기값 설정
return {
max_id: 2,
mode: 'create',
selected_id:null,
contents: [
{id: 1, title:'HTML', desc:'HTML is ..'},
{id: 2, title:'CSS', desc:'CSS is ..'}
]
}
}
var newState;
if(action.type === 'SELECT') {
newState = Object.assign(
{},
state,
{selected_id: action.id, mode:'read'}
);
} else if (action.type === 'CREATE') {
var newMaxID = state.max_id + 1;
var newContents = state.contents.concat(); // 배열을 복제
newContents.push({id: newMaxID,title:action.title, desc:action.desc});
newState = Object.assign({}, state, {
max_id : newMaxID,
contents: newContents,
mode : 'read'
})
} else if (action.type === 'DELETE') {
var newContents = [];
var i = 0;
while(i < state.contents.length) {
if(state.selected_id !== state.contents[i].id) {
newContents.push(
state.contents[i]
)
break;
}
i = i + 1;
}
newState = Object.assign({}, state, {
contents: newContents,
mode:'welcome'
})
} else if (action.type === 'CHANGE_MODE') {
newState = Object.assign({}, state, {
mode: 'create'
});
}
console.log(action);
return newState;
}
var store = Redux.createStore(reducer); // Redux를 사용하기 위해 Store 생성
store.subscribe(article);
store.subscribe(TOC);
store.subscribe(control);
subject();
TOC();
control();
article();
</script>
</body>
</html>