@@ -2,22 +2,29 @@ $(() => {
22 $ ( document ) . on ( 'click' , '.vote-button' , async evt => {
33 const $tgt = $ ( evt . target ) . is ( 'button' ) ? $ ( evt . target ) : $ ( evt . target ) . parents ( 'button' ) ;
44 const $post = $tgt . parents ( '.post' ) ;
5- const $up = $post . find ( '.post--votes' ) . find ( '.js-upvote-count' ) ;
6- const $down = $post . find ( '.post--votes' ) . find ( '.js-downvote-count' ) ;
5+
6+ const $container = $post . find ( ".post--votes" ) ;
7+
8+ const $up = $container . find ( '.js-upvote-count' ) ;
9+ const $down = $container . find ( '.js-downvote-count' ) ;
710 const voteType = $tgt . data ( 'vote-type' ) ;
811 const voted = $tgt . hasClass ( 'is-active' ) ;
912
1013 if ( voted ) {
1114 const voteId = $tgt . attr ( 'data-vote-id' ) ;
15+
1216 const resp = await fetch ( `/votes/${ voteId } ` , {
1317 method : 'DELETE' ,
1418 credentials : 'include' ,
1519 headers : { 'X-CSRF-Token' : QPixel . csrfToken ( ) }
1620 } ) ;
21+
1722 const data = await resp . json ( ) ;
23+
1824 if ( data . status === 'OK' ) {
1925 $up . text ( `+${ data . upvotes } ` ) ;
2026 $down . html ( `−${ data . downvotes } ` ) ;
27+ $container . attr ( "title" , `Score: ${ data . score } ` ) ;
2128 $tgt . removeClass ( 'is-active' )
2229 . removeAttr ( 'data-vote-id' ) ;
2330 }
@@ -34,10 +41,13 @@ $(() => {
3441 headers : { 'Content-Type' : 'application/json' , 'X-CSRF-Token' : QPixel . csrfToken ( ) } ,
3542 body : JSON . stringify ( { post_id : $post . data ( 'post-id' ) , vote_type : voteType } )
3643 } ) ;
44+
3745 const data = await resp . json ( ) ;
46+
3847 if ( data . status === 'modified' || data . status === 'OK' ) {
3948 $up . text ( `+${ data . upvotes } ` ) ;
4049 $down . html ( `−${ data . downvotes } ` ) ;
50+ $container . attr ( "title" , `Score: ${ data . score } ` ) ;
4151 $tgt . addClass ( 'is-active' )
4252 . attr ( 'data-vote-id' , data . vote_id ) ;
4353
0 commit comments