55 */
66import _ from 'lodash' ;
77import React from 'react' ;
8+ import { connect } from 'react-redux' ;
89import PT from 'prop-types' ;
910import { fixStyle } from 'utils/contentful' ;
1011import { getService } from 'services/contentful' ;
@@ -22,6 +23,7 @@ import {
2223 config , Link , isomorphy ,
2324} from 'topcoder-react-utils' ;
2425import qs from 'qs' ;
26+ import LoginModal from 'components/Gigs/LoginModal' ;
2527// SVGs and assets
2628import GestureIcon from 'assets/images/icon-gesture.svg' ;
2729import ReadMoreArrow from 'assets/images/read-more-arrow.svg' ;
@@ -41,21 +43,30 @@ const DEFAULT_BANNER_IMAGE = 'https://images.ctfassets.net/piwi0eufbb2g/7v2hlDsV
4143const RANDOM_BANNERS = [ '6G8mjiTC1mzeSQ2YoUG1gB' , '1DnDD02xX1liHfSTf5Vsn8' , 'HQZ3mN0rR92CbNTkKTHJ5' , '1OLoX8ZsvjAnn4TdGbZESD' , '77jn01UGoQe2gqA7x0coQD' ] ;
4244const RANDOM_BANNER = RANDOM_BANNERS [ _ . random ( 0 , 4 ) ] ;
4345
44- export default class Article extends React . Component {
46+ class Article extends React . Component {
4547 componentDidMount ( ) {
4648 const { fields } = this . props ;
4749 this . setState ( {
4850 upvotes : fields . upvotes || 0 ,
4951 downvotes : fields . downvotes || 0 ,
52+ showLogin : false ,
53+ voting : false ,
5054 } ) ;
5155 }
5256
57+ // eslint-disable-next-line consistent-return
5358 updateVote ( type ) {
54- let userVotes = localStorage . getItem ( LOCAL_STORAGE_KEY ) ;
55- userVotes = userVotes ? JSON . parse ( userVotes ) : { } ;
5659 const {
57- id, spaceName, environment, preview,
60+ id, spaceName, environment, preview, auth ,
5861 } = this . props ;
62+ // check for auth?
63+ if ( ! auth ) {
64+ return this . setState ( {
65+ showLogin : true ,
66+ } ) ;
67+ }
68+ let userVotes = localStorage . getItem ( LOCAL_STORAGE_KEY ) ;
69+ userVotes = userVotes ? JSON . parse ( userVotes ) : { } ;
5970 const articleVote = userVotes [ id ] ;
6071 let { upvotes, downvotes } = this . state ;
6172 // Check if user alredy voted on this article?
@@ -93,17 +104,21 @@ export default class Article extends React.Component {
93104 }
94105 }
95106 // Store user action
107+ this . setState ( {
108+ voting : true ,
109+ } ) ;
96110 getService ( { spaceName, environment, preview } ) . articleVote ( id , {
97111 upvotes,
98112 downvotes,
99- } )
113+ } , auth . tokenV3 )
100114 . then ( ( ) => {
101115 // Only when Contentful enntry was succesfully updated
102116 // then we update the local store and the state
103117 localStorage . setItem ( LOCAL_STORAGE_KEY , JSON . stringify ( userVotes ) ) ;
104118 this . setState ( {
105119 upvotes,
106120 downvotes,
121+ voting : false ,
107122 } ) ;
108123 } ) ;
109124 }
@@ -115,7 +130,9 @@ export default class Article extends React.Component {
115130 const contentfulConfig = {
116131 spaceName, environment, preview,
117132 } ;
118- const { upvotes, downvotes } = this . state || { } ;
133+ const {
134+ upvotes, downvotes, showLogin, voting,
135+ } = this . state || { } ;
119136 let shareUrl ;
120137 if ( isomorphy . isClientSide ( ) ) {
121138 shareUrl = encodeURIComponent ( window . location . href ) ;
@@ -283,7 +300,7 @@ export default class Article extends React.Component {
283300 { /* Voting */ }
284301 < div className = { theme . actionContainer } >
285302 < div className = { theme . action } >
286- < div tabIndex = { 0 } role = "button" className = { theme . circleGreenIcon } onClick = { ( ) => this . updateVote ( 'up' ) } onKeyPress = { ( ) => this . updateVote ( 'up' ) } >
303+ < div tabIndex = { 0 } role = "button" className = { voting ? theme . circleGreenIconDisabled : theme . circleGreenIcon } onClick = { ( ) => this . updateVote ( 'up' ) } onKeyPress = { ( ) => this . updateVote ( 'up' ) } >
287304 < GestureIcon />
288305 </ div >
289306 < span >
@@ -293,7 +310,7 @@ export default class Article extends React.Component {
293310 </ span >
294311 </ div >
295312 < div className = { theme . action } >
296- < div tabIndex = { 0 } role = "button" className = { theme . circleRedIcon } onClick = { ( ) => this . updateVote ( 'down' ) } onKeyPress = { ( ) => this . updateVote ( 'down' ) } >
313+ < div tabIndex = { 0 } role = "button" className = { voting ? theme . circleRedIconDisabled : theme . circleRedIcon } onClick = { ( ) => this . updateVote ( 'down' ) } onKeyPress = { ( ) => this . updateVote ( 'down' ) } >
297314 < GestureIcon />
298315 </ div >
299316 < span > { downvotes } </ span >
@@ -380,6 +397,10 @@ export default class Article extends React.Component {
380397 ) : null
381398 }
382399 </ div >
400+ {
401+ // eslint-disable-next-line no-restricted-globals
402+ showLogin && < LoginModal retUrl = { isomorphy . isClientSide ( ) ? location . href : null } onCancel = { ( ) => this . setState ( { showLogin : false } ) } utmSource = "thrive_article" />
403+ }
383404 </ React . Fragment >
384405 ) ;
385406 }
@@ -388,6 +409,7 @@ export default class Article extends React.Component {
388409Article . defaultProps = {
389410 spaceName : null ,
390411 environment : null ,
412+ auth : null ,
391413} ;
392414
393415Article . propTypes = {
@@ -398,4 +420,16 @@ Article.propTypes = {
398420 preview : PT . bool . isRequired ,
399421 spaceName : PT . string ,
400422 environment : PT . string ,
423+ auth : PT . shape ( ) ,
401424} ;
425+
426+ function mapStateToProps ( state ) {
427+ const auth = state . auth && state . auth . profile ? { ...state . auth } : null ;
428+ return {
429+ auth,
430+ } ;
431+ }
432+
433+ export default connect (
434+ mapStateToProps ,
435+ ) ( Article ) ;
0 commit comments