FIX 优化点赞、关注等功能

This commit is contained in:
Jasder 2020-03-27 17:30:16 +08:00
parent 8cd33ee65d
commit b29c80fb64
1 changed files with 49 additions and 63 deletions

View File

@ -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{
</ul>
<span className="df">
<span className="detail_tag_btn">
<a className="detail_tag_btn_name" onClick={()=>this.focusFunc(projectDetail && projectDetail.watched)}>
<img src={projectDetail && projectDetail.watched ? img_focused : img_focus} alt="" width="14px"/>
{projectDetail && projectDetail.watched ? '取消关注':'关注'}
<a className="detail_tag_btn_name" onClick={()=>this.focusFunc(watched)}>
<img src={watched ? img_focused : img_focus} alt="" width="14px"/>
{watched ? '取消关注':'关注'}
</a>
<span className="detail_tag_btn_count">{watchers_count}</span>
</span>
<span className="detail_tag_btn">
<a className="detail_tag_btn_name" onClick={()=>this.pariseFunc(projectDetail && projectDetail.praised)}>
<img src={projectDetail && projectDetail.praised ? img_parised : img_parise} width="13px" alt=""/>
{projectDetail && projectDetail.praised ? '取消点赞':'点赞'}
<a className="detail_tag_btn_name" onClick={()=>this.pariseFunc(praised)}>
<img src={praised ? img_parised : img_parise} width="13px" alt=""/>
{praised ? '取消点赞':'点赞'}
</a>
<span className="detail_tag_btn_count">{praises_count}</span>
</span>