Skip to content

Commit 64112b0

Browse files
committed
Chapter 21 - Comment field reset
1 parent be44d40 commit 64112b0

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

chapter-21/package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter-21/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"main": "skillsharing_server.js",
55
"description": "Skill-sharing website example from Eloquent JavaScript",
66
"dependencies": {
7-
"serve-handler": "^6.1.3"
7+
"serve-handler": "^6.1.3",
8+
"uuid": "^8.3.2"
89
},
910
"license": "MIT",
1011
"bugs": "https://github.com/marijnh/Eloquent-JavaScript/issues",

chapter-21/public/skillsharing-client.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function elt(type, props, ...children) {
7171
function renderTalk(talk, dispatch) {
7272
return elt(
7373
'section',
74-
{ className: 'talk' },
74+
{ className: 'talk', id: talk.id },
7575
elt(
7676
'h2',
7777
null,
@@ -90,7 +90,7 @@ function renderTalk(talk, dispatch) {
9090
),
9191
elt('div', null, 'by ', elt('strong', null, talk.presenter)),
9292
elt('p', null, talk.summary),
93-
...talk.comments.map(renderComment),
93+
elt('section', { className: 'comments' }, ...talk.comments.map(renderComment)),
9494
elt(
9595
'form',
9696
{
@@ -109,7 +109,7 @@ function renderTalk(talk, dispatch) {
109109
}
110110

111111
function renderComment(comment) {
112-
return elt('p', { className: 'comment' }, elt('strong', null, comment.author), ': ', comment.message);
112+
return elt('p', { className: 'comment', id: comment.id }, elt('strong', null, comment.author), ': ', comment.message);
113113
}
114114

115115
function renderTalkForm(dispatch) {
@@ -155,14 +155,33 @@ var SkillShareApp = class SkillShareApp {
155155
this.dispatch = dispatch;
156156
this.talkDOM = elt('div', { className: 'talks' });
157157
this.dom = elt('div', null, renderUserField(state.user, dispatch), this.talkDOM, renderTalkForm(dispatch));
158+
this.talks = [];
158159
this.syncState(state);
159160
}
160161

161162
syncState(state) {
162163
if (state.talks != this.talks) {
163-
this.talkDOM.textContent = '';
164-
for (let talk of state.talks) {
165-
this.talkDOM.appendChild(renderTalk(talk, this.dispatch));
164+
// update / add talks
165+
for (let stateTalk of state.talks) {
166+
const talk = this.talks.find(({ id }) => id === stateTalk.id);
167+
if (talk) {
168+
if (talk.comments.length < stateTalk.comments.length) {
169+
const commentsContainer = this.talkDOM.querySelector(`#${stateTalk.id} .comments`);
170+
for (let i = talk.comments.length; i < stateTalk.comments.length; i++) {
171+
const comment = stateTalk.comments[i];
172+
commentsContainer.appendChild(renderComment(comment));
173+
}
174+
}
175+
} else {
176+
this.talkDOM.appendChild(renderTalk(stateTalk, this.dispatch));
177+
}
178+
}
179+
// remove deleted talks
180+
for (let talk of this.talks) {
181+
if (!state.talks.some(({ id }) => id === talk.id)) {
182+
const talkNode = document.getElementById(talk.id);
183+
this.talkDOM.removeChild(talkNode);
184+
}
166185
}
167186
this.talks = state.talks;
168187
}

chapter-21/skillsharing-server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createServer } from 'http';
22
import { writeFileSync } from 'fs';
33
import handler from 'serve-handler';
4+
import { v4 as uuidv4 } from 'uuid';
45
import Router from './router.js';
56
import database from './db.json' assert { type: 'json' };
67

@@ -78,7 +79,7 @@ router.add('PUT', talkPath, async (server, title, request) => {
7879
if (!talk || typeof talk.presenter != 'string' || typeof talk.summary != 'string') {
7980
return { status: 400, body: 'Bad talk data' };
8081
}
81-
server.talks[title] = { title, presenter: talk.presenter, summary: talk.summary, comments: [] };
82+
server.talks[title] = { id: uuidv4(), title, presenter: talk.presenter, summary: talk.summary, comments: [] };
8283
server.updated();
8384
return { status: 204 };
8485
});
@@ -95,7 +96,7 @@ router.add('POST', /^\/talks\/([^\/]+)\/comments$/, async (server, title, reques
9596
if (!comment || typeof comment.author != 'string' || typeof comment.message != 'string') {
9697
return { status: 400, body: 'Bad comment data' };
9798
} else if (title in server.talks) {
98-
server.talks[title].comments.push(comment);
99+
server.talks[title].comments.push({ id: uuidv4(), ...comment });
99100
server.updated();
100101
return { status: 204 };
101102
} else {

0 commit comments

Comments
 (0)