Browse Source

add show single in sidebar

dev
xmcp 7 years ago
parent
commit
50fcaf1e97
  1. 188
      src/Flows.js
  2. 12
      src/flows_api.js

188
src/Flows.js

@ -17,6 +17,41 @@ const PREVIEW_REPLY_COUNT=10;
window.LATEST_POST_ID=parseInt(localStorage['_LATEST_POST_ID'],10)||0; window.LATEST_POST_ID=parseInt(localStorage['_LATEST_POST_ID'],10)||0;
function load_single_meta(show_sidebar,token) {
return (pid)=>{
const color_picker=new ColorPicker();
show_sidebar(
'帖子详情',
<div className="box box-tip">
正在加载 #{pid}
</div>
);
Promise.all([
API.get_single(pid,token),
API.load_replies(pid,token,color_picker),
])
.then((res)=>{
const [single,replies]=res;
show_sidebar(
'帖子详情',
<FlowSidebar
info={single.data} replies={replies.data} attention={replies.attention}
token={token} show_sidebar={show_sidebar} color_picker={color_picker}
/>
)
})
.catch((e)=>{
console.trace(e);
show_sidebar(
'帖子详情',
<div className="box box-tip">
<a onClick={()=>load_single_meta(show_sidebar,token)}>重新加载</a>
</div>
);
})
};
}
function Reply(props) { function Reply(props) {
return ( return (
<div className={'flow-reply box'} style={props.info._display_color ? { <div className={'flow-reply box'} style={props.info._display_color ? {
@ -26,7 +61,7 @@ function Reply(props) {
<code className="box-id">#{props.info.cid}</code>&nbsp; <code className="box-id">#{props.info.cid}</code>&nbsp;
<Time stamp={props.info.timestamp} /> <Time stamp={props.info.timestamp} />
</div> </div>
<HighlightedText text={props.info.text} color_picker={props.color_picker} /> <HighlightedText text={props.info.text} color_picker={props.color_picker} show_pid={props.show_pid} />
</div> </div>
); );
} }
@ -51,96 +86,93 @@ function FlowItem(props) {
<code className="box-id">#{props.info.pid}</code>&nbsp; <code className="box-id">#{props.info.pid}</code>&nbsp;
<Time stamp={props.info.timestamp} /> <Time stamp={props.info.timestamp} />
</div> </div>
<HighlightedText text={props.info.text} color_picker={props.color_picker} /> <HighlightedText text={props.info.text} color_picker={props.color_picker} show_pid={props.show_pid} />
{props.info.type==='image' ? <p className="img"><img src={IMAGE_BASE+props.info.url} /></p> : null} {props.info.type==='image' ? <p className="img"><img src={IMAGE_BASE+props.info.url} /></p> : null}
{props.info.type==='audio' ? <AudioWidget src={AUDIO_BASE+props.info.url} /> : null} {props.info.type==='audio' ? <AudioWidget src={AUDIO_BASE+props.info.url} /> : null}
</div> </div>
); );
} }
class FlowItemRow extends PureComponent { class FlowSidebar extends PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
this.state={ this.state={
replies: [], attention: props.attention,
reply_status: 'done',
info: props.info, info: props.info,
attention: false, replies: props.replies,
loading_status: 'done',
}; };
this.color_picker=new ColorPicker(); this.color_picker=props.color_picker;
this.show_pid=load_single_meta(this.props.show_sidebar,this.props.token);
this.syncState=props.sync_state||(()=>{});
} }
componentDidMount() { load_replies() {
if(parseInt(this.state.info.reply,10)) {
this.load_replies();
}
}
load_replies(callback) {
console.log('fetching reply',this.state.info.pid);
this.setState({ this.setState({
reply_status: 'loading', loading_status: 'loading',
}); });
API.load_replies(this.state.info.pid,this.props.token) API.load_replies(this.state.info.pid,this.props.token,this.color_picker)
.then((json)=>{ .then((json)=>{
const replies=json.data
.sort((a,b)=>{
return parseInt(a.timestamp,10)-parseInt(b.timestamp,10);
})
.map((info)=>{
info._display_color=this.color_picker.get(info.name);
return info;
});
this.setState((prev,props)=>({ this.setState((prev,props)=>({
replies: replies, replies: json.data,
info: Object.assign({}, prev.info, { info: Object.assign({}, prev.info, {
reply: ''+replies.length, reply: ''+json.data.length,
}), }),
attention: !!json.attention, attention: !!json.attention,
reply_status: 'done', loading_status: 'done',
}),callback); }), ()=>{
this.syncState({
replies: this.state.replies,
attention: this.state.attention,
info: this.state.info,
});
});
}) })
.catch((e)=>{ .catch((e)=>{
console.trace(e); console.trace(e);
this.setState({ this.setState({
replies: [], replies: [],
reply_status: 'failed', loading_status: 'done',
},callback); });
}); });
} }
toggle_attention(callback) { toggle_attention() {
this.setState({
loading_status: 'loading',
});
const next_attention=!this.state.attention; const next_attention=!this.state.attention;
API.set_attention(this.state.info.pid,next_attention,this.props.token) API.set_attention(this.state.info.pid,next_attention,this.props.token)
.then((json)=>{ .then((json)=>{
this.setState({ this.setState({
loading_status: 'done',
attention: next_attention, attention: next_attention,
}, callback); });
this.syncState({
attention: next_attention,
});
}) })
.catch((e)=>{ .catch((e)=>{
this.setState({
loading_status: 'done'
});
alert('设置关注失败'); alert('设置关注失败');
console.trace(e); console.trace(e);
callback();
}); });
} }
reload_sidebar() { render() {
this.props.show_sidebar('帖子详情',<p className="box box-tip">加载中</p>); if(this.state.loading_status==='loading')
this.load_replies(this.show_sidebar.bind(this)); return (<p className="box box-tip">加载中</p>);
} return (
show_sidebar() {
this.props.show_sidebar(
'帖子详情',
<div className="flow-item-row sidebar-flow-item"> <div className="flow-item-row sidebar-flow-item">
<div className="box box-tip"> <div className="box box-tip">
<a onClick={this.reload_sidebar.bind(this)}>刷新回复</a> <a onClick={this.load_replies.bind(this)}>刷新回复</a>
{this.props.token && {this.props.token &&
<span> <span>
&nbsp;/&nbsp; &nbsp;/&nbsp;
<a onClick={()=>{ <a onClick={()=>{
this.props.show_sidebar('帖子详情',<p className="box box-tip">加载中</p>); this.toggle_attention();
this.toggle_attention(this.show_sidebar.bind(this));
}}> }}>
{this.state.attention ? {this.state.attention ?
<span><span className="icon icon-star-ok" />&nbsp;已关注</span> : <span><span className="icon icon-star-ok" />&nbsp;已关注</span> :
@ -150,34 +182,88 @@ class FlowItemRow extends PureComponent {
</span> </span>
} }
</div> </div>
<FlowItem info={this.state.info} color_picker={this.color_picker} attention={this.state.attention} /> <FlowItem info={this.state.info} color_picker={this.color_picker} attention={this.state.attention} show_pid={this.show_pid} />
{this.state.replies.map((reply)=>( {this.state.replies.map((reply)=>(
<LazyLoad key={reply.cid} offset={500} height="5em" overflow={true} once={true}> <LazyLoad key={reply.cid} offset={500} height="5em" overflow={true} once={true}>
<Reply info={reply} color_picker={this.color_picker} /> <Reply info={reply} color_picker={this.color_picker} show_pid={this.show_pid} />
</LazyLoad> </LazyLoad>
))} ))}
{this.props.token && {this.props.token &&
<ReplyForm pid={this.state.info.pid} token={this.props.token} on_complete={this.reload_sidebar.bind(this)} /> <ReplyForm pid={this.state.info.pid} token={this.props.token} on_complete={this.load_replies.bind(this)} />
} }
</div> </div>
)
}
}
class FlowItemRow extends PureComponent {
constructor(props) {
super(props);
this.state={
replies: [],
reply_status: 'done',
info: props.info,
attention: false,
};
this.color_picker=new ColorPicker();
this.show_pid=load_single_meta(this.props.show_sidebar,this.props.token);
}
componentDidMount() {
if(parseInt(this.state.info.reply,10)) {
this.load_replies();
}
}
load_replies(callback) {
console.log('fetching reply',this.state.info.pid);
this.setState({
reply_status: 'loading',
});
API.load_replies(this.state.info.pid,this.props.token,this.color_picker)
.then((json)=>{
this.setState((prev,props)=>({
replies: json.data,
info: Object.assign({}, prev.info, {
reply: ''+json.data.length,
}),
attention: !!json.attention,
reply_status: 'done',
}),callback);
})
.catch((e)=>{
console.trace(e);
this.setState({
replies: [],
reply_status: 'failed',
},callback);
});
}
show_sidebar() {
this.props.show_sidebar(
'帖子详情',
<FlowSidebar
info={this.state.info} replies={this.state.replies} attention={this.state.attention} sync_state={this.setState.bind(this)}
token={this.props.token} show_sidebar={this.props.show_sidebar} color_picker={this.color_picker}
/>
); );
} }
render() { render() {
// props.do_show_details
return ( return (
<div className="flow-item-row" onClick={(event)=>{ <div className="flow-item-row" onClick={(event)=>{
if(!CLICKABLE_TAGS[event.target.tagName.toLowerCase()]) if(!CLICKABLE_TAGS[event.target.tagName.toLowerCase()])
this.show_sidebar(); this.show_sidebar();
}}> }}>
<FlowItem info={this.state.info} color_picker={this.color_picker} attention={this.state.attention} /> <FlowItem info={this.state.info} color_picker={this.color_picker} attention={this.state.attention} show_pid={this.show_pid} />
<div className="flow-reply-row"> <div className="flow-reply-row">
{this.state.reply_status==='loading' && <div className="box box-tip">加载中</div>} {this.state.reply_status==='loading' && <div className="box box-tip">加载中</div>}
{this.state.reply_status==='failed' && {this.state.reply_status==='failed' &&
<div className="box box-tip"><a onClick={()=>{this.load_replies()}}>重新加载</a></div> <div className="box box-tip"><a onClick={()=>{this.load_replies()}}>重新加载</a></div>
} }
{this.state.replies.slice(0,PREVIEW_REPLY_COUNT).map((reply)=>( {this.state.replies.slice(0,PREVIEW_REPLY_COUNT).map((reply)=>(
<Reply key={reply.cid} info={reply} color_picker={this.color_picker} /> <Reply key={reply.cid} info={reply} color_picker={this.color_picker} show_pid={this.show_pid} />
))} ))}
{this.state.replies.length>PREVIEW_REPLY_COUNT && {this.state.replies.length>PREVIEW_REPLY_COUNT &&
<div className="box box-tip">还有 {this.state.replies.length-PREVIEW_REPLY_COUNT} </div> <div className="box box-tip">还有 {this.state.replies.length-PREVIEW_REPLY_COUNT} </div>

12
src/flows_api.js

@ -5,7 +5,7 @@ function token_param(token) {
} }
export const API={ export const API={
load_replies: (pid,token)=>{ load_replies: (pid,token,color_picker)=>{
return fetch( return fetch(
API_BASE+'/api.php?action=getcomment'+ API_BASE+'/api.php?action=getcomment'+
'&pid='+pid+ '&pid='+pid+
@ -15,6 +15,16 @@ export const API={
.then((json)=>{ .then((json)=>{
if(json.code!==0) if(json.code!==0)
throw new Error(json); throw new Error(json);
json.data=json.data
.sort((a,b)=>{
return parseInt(a.timestamp,10)-parseInt(b.timestamp,10);
})
.map((info)=>{
info._display_color=color_picker.get(info.name);
return info;
});
return json; return json;
}); });
}, },

Loading…
Cancel
Save