import React, { PureComponent } from 'react'; import { Time } from './Common'; export class MessageViewer extends PureComponent { constructor(props) { super(props); this.state = { loading_status: 'idle', msg: [], }; } componentDidMount() { this.load(); } load() { if (this.state.loading_status === 'loading') return; this.setState( { loading_status: 'loading', }, () => { fetch( '/_api/v1/system_msg?user_token=' + encodeURIComponent(this.props.token) ) .then(get_json) .then((json) => { if (json.error) throw new Error(json.error); else this.setState({ loading_status: 'done', msg: json.result, }); }) .catch((err) => { console.error(err); alert('' + err); this.setState({ loading_status: 'failed', }); }); }, ); } render() { if (this.state.loading_status === 'loading') return
加载中……
; else if (this.state.loading_status === 'failed') return ( ); else if (this.state.loading_status === 'done') return this.state.msg.map((msg) => ({msg.content}