forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21_2_comment_field_resets.js
More file actions
76 lines (71 loc) · 2.26 KB
/
21_2_comment_field_resets.js
File metadata and controls
76 lines (71 loc) · 2.26 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
// This isn't a stand-alone file, only a redefinition of the main
// component from skillsharing/public/skillsharing_client.js
class Talk {
constructor(talk, dispatch) {
this.comments = elt("div");
this.dom = elt(
"section", {className: "talk"},
elt("h2", null, talk.title, " ", elt("button", {
type: "button",
onclick: () => dispatch({type: "deleteTalk",
talk: talk.title})
}, "Delete")),
elt("div", null, "by ",
elt("strong", null, talk.presenter)),
elt("p", null, talk.summary),
this.comments,
elt("form", {
onsubmit(event) {
event.preventDefault();
let form = event.target;
dispatch({type: "newComment",
talk: talk.title,
message: form.elements.comment.value});
form.reset();
}
}, elt("input", {type: "text", name: "comment"}), " ",
elt("button", {type: "submit"}, "Add comment")));
this.syncState(talk);
}
syncState(talk) {
this.talk = talk;
this.comments.textContent = "";
for (let comment of talk.comments) {
this.comments.appendChild(renderComment(comment));
}
}
}
class SkillShareApp {
constructor(state, dispatch) {
this.dispatch = dispatch;
this.talkDOM = elt("div", {className: "talks"});
this.talkMap = Object.create(null);
this.dom = elt("div", null,
renderUserField(state.user, dispatch),
this.talkDOM,
renderTalkForm(dispatch));
this.syncState(state);
}
syncState(state) {
if (state.talks == this.talks) return;
this.talks = state.talks;
for (let talk of state.talks) {
let cmp = this.talkMap[talk.title];
if (cmp && cmp.talk.presenter == talk.presenter &&
cmp.talk.summary == talk.summary) {
cmp.syncState(talk);
} else {
if (cmp) cmp.dom.remove();
cmp = new Talk(talk, this.dispatch);
this.talkMap[talk.title] = cmp;
this.talkDOM.appendChild(cmp.dom);
}
}
for (let title of Object.keys(this.talkMap)) {
if (!state.talks.some(talk => talk.title == title)) {
this.talkMap[title].dom.remove();
delete this.talkMap[title];
}
}
}
}