@@ -71,7 +71,7 @@ function elt(type, props, ...children) {
71
71
function renderTalk ( talk , dispatch ) {
72
72
return elt (
73
73
'section' ,
74
- { className : 'talk' } ,
74
+ { className : 'talk' , id : talk . id } ,
75
75
elt (
76
76
'h2' ,
77
77
null ,
@@ -90,7 +90,7 @@ function renderTalk(talk, dispatch) {
90
90
) ,
91
91
elt ( 'div' , null , 'by ' , elt ( 'strong' , null , talk . presenter ) ) ,
92
92
elt ( 'p' , null , talk . summary ) ,
93
- ...talk . comments . map ( renderComment ) ,
93
+ elt ( 'section' , { className : 'comments' } , ...talk . comments . map ( renderComment ) ) ,
94
94
elt (
95
95
'form' ,
96
96
{
@@ -109,7 +109,7 @@ function renderTalk(talk, dispatch) {
109
109
}
110
110
111
111
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 ) ;
113
113
}
114
114
115
115
function renderTalkForm ( dispatch ) {
@@ -155,14 +155,33 @@ var SkillShareApp = class SkillShareApp {
155
155
this . dispatch = dispatch ;
156
156
this . talkDOM = elt ( 'div' , { className : 'talks' } ) ;
157
157
this . dom = elt ( 'div' , null , renderUserField ( state . user , dispatch ) , this . talkDOM , renderTalkForm ( dispatch ) ) ;
158
+ this . talks = [ ] ;
158
159
this . syncState ( state ) ;
159
160
}
160
161
161
162
syncState ( state ) {
162
163
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
+ }
166
185
}
167
186
this . talks = state . talks ;
168
187
}
0 commit comments