diff --git a/src/forge/Main/Detail.js b/src/forge/Main/Detail.js index 9aebb40a1..05376c6c0 100644 --- a/src/forge/Main/Detail.js +++ b/src/forge/Main/Detail.js @@ -139,30 +139,16 @@ class Detail extends Component{ watchers_count:undefined , praises_count:undefined , forked_count:undefined, + watched: false, + praised: false, http_url: undefined, author:undefined, - // current_user:undefined, - branchs:undefined, branchList:undefined, branchLastCommit:undefined, } } - getUserInfo=()=>{ - const url = `/users/me.json`; - axios.get(url).then(result=>{ - if(result && result.data.login){ - this.setState({ - current_user:result.data - }) - } - }).catch(error=>{ - console.log(error) - }) - } - - componentDidMount=()=>{ this.getDetail(); } @@ -187,7 +173,8 @@ class Detail extends Component{ isDeveloper:result.data.permission && result.data.permission === "Developer", http_url: result.data.clone_url, author:result.data.author, - + praised: result.data.praised, + watched: result.data.watched, watchers_count:result.data.watchers_count, praises_count:result.data.praises_count, forked_count:result.data.forked_count, @@ -202,51 +189,51 @@ class Detail extends Component{ // 关注和取消关注 focusFunc =(flag)=>{ const { project_id } = this.state; - if(!flag){ - const url = `/projects/${project_id}/watchers/follow.json`; - axios.post(url).then(result=>{ - if(result && result.data.status === 0){ - this.getDetail(); - } - }).catch(error=>{ - console.log(error); - }) - }else{ - const url = `/projects/${project_id}/watchers/unfollow.json`; - axios.delete(url).then(result=>{ - if(result && result.data.status === 0){ - this.getDetail(); - } - }).catch(error=>{ - console.log(error); - }) - } + axios({ + method: flag? 'delete' : 'post', + url: `/projects/${project_id}/watchers/${flag? 'unfollow' : 'follow'}.json` + }) + .then(result => { + if(result && result.data.status === 0){ + this.setWatchersCount(result.data.watchers_count, result.data.watched); + } + }) + .catch(error => { + console.log(error); + }); } // 点赞和取消点赞 pariseFunc=(flag)=>{ const { project_id } = this.state; - if(!flag){ - const url = `/projects/${project_id}/praise_tread/like.json`; - axios.post(url).then(result=>{ - if(result && result.data.status === 0){ - this.getDetail(); - } - }).catch(error=>{ - console.log(error); - }) - }else{ - const url = `/projects/${project_id}/praise_tread/unlike.json`; - axios.delete(url).then(result=>{ - if(result && result.data.status === 0){ - this.getDetail(); - } - }).catch(error=>{ - console.log(error); - }) - } + axios({ + method: flag? 'delete' : 'post', + url: `/projects/${project_id}/praise_tread/${flag? 'unlike' : 'like'}.json` + }) + .then(result => { + if(result && result.data.status === 0){ + this.setPraisesCount(result.data.praises_count, result.data.praised) + } + }) + .catch(error => { + console.log(error); + }); } + setWatchersCount = (count, is_watched) => { + this.setState({ + watched: is_watched, + watchers_count: count + }) + } + + setPraisesCount = (count, is_praised) => { + this.setState({ + praised: is_praised, + praises_count: count + }) + } + // fork项目 forkFunc=()=>{ this.setState({ @@ -290,8 +277,7 @@ class Detail extends Component{ } render(){ - - const { projectDetail , watchers_count , praises_count , forked_count , isSpin , isManager } = this.state; + const { projectDetail , watchers_count , praises_count , forked_count , isSpin , isManager, watched, praised } = this.state; const url = this.props.history.location.pathname; const urlArr= url.split("/"); const urlFlag = (urlArr.length === 3); @@ -328,16 +314,16 @@ class Detail extends Component{ - this.focusFunc(projectDetail && projectDetail.watched)}> - - {projectDetail && projectDetail.watched ? '取消关注':'关注'} + this.focusFunc(watched)}> + + {watched ? '取消关注':'关注'} {watchers_count} - this.pariseFunc(projectDetail && projectDetail.praised)}> - - {projectDetail && projectDetail.praised ? '取消点赞':'点赞'} + this.pariseFunc(praised)}> + + {praised ? '取消点赞':'点赞'} {praises_count}