Make all displayed dates update live

This commit is contained in:
Drew DeVault 2017-09-10 21:29:43 +09:00
parent 83a281bee2
commit 6ce9402468
4 changed files with 80 additions and 75 deletions

View File

@ -18,7 +18,7 @@ import moment from 'moment';
import ToggleContainer from './toggle_container'; import ToggleContainer from './toggle_container';
import TorrentOptions from './torrent_options'; import TorrentOptions from './torrent_options';
import ws_send from '../socket'; import ws_send from '../socket';
import date from '../date'; import DateDisplay from './date';
class AddTorrent extends Component { class AddTorrent extends Component {
constructor() { constructor() {
@ -199,7 +199,7 @@ class AddTorrent extends Component {
const details = { const details = {
"comment": d => d, "comment": d => d,
"creation date": d => date(moment(new Date(d * 1000))), "creation date": d => <DateDisplay when={moment(new Date(d * 1000))} />,
"created by": d => d "created by": d => d
}; };

16
src/ui/date.js Normal file
View File

@ -0,0 +1,16 @@
import React, { Component } from 'react';
import formatDate from '../date';
export default class DateDisplay extends Component {
componentDidMount() {
setInterval(() => this.forceUpdate(), this.interval);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return <span>{formatDate(this.props.when)}</span>;
}
}

View File

@ -3,7 +3,7 @@ import { connect } from 'react-redux';
import moment from 'moment'; import moment from 'moment';
import { formatBitrate, formatAmount } from '../bitrate'; import { formatBitrate, formatAmount } from '../bitrate';
import { ws_disconnect } from '../socket'; import { ws_disconnect } from '../socket';
import date from '../date'; import DateDisplay from './date';
import Throttle from './throttle'; import Throttle from './throttle';
import { updateResource } from '../actions/resources'; import { updateResource } from '../actions/resources';
@ -25,17 +25,7 @@ const ratio = (up, down) => {
); );
}; };
class Server extends Component { function Server({ server, dispatch }) {
componentDidMount() {
this.interval = setInterval(this.forceUpdate, 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
const { server, dispatch } = this.props;
if (!server.id) { if (!server.id) {
return null; return null;
} }
@ -54,7 +44,7 @@ class Server extends Component {
</h3> </h3>
<dl> <dl>
<dt>Running since</dt> <dt>Running since</dt>
<dd>{date(moment(server.started))}</dd> <dd><DateDisplay when={moment(server.started)} /></dd>
<dt>Current network use</dt> <dt>Current network use</dt>
<dd> <dd>
{`${ {`${
@ -95,6 +85,5 @@ class Server extends Component {
</div> </div>
); );
} }
}
export default connect(state => ({ server: state.server }))(Server); export default connect(state => ({ server: state.server }))(Server);

View File

@ -15,7 +15,7 @@ import {
import moment from 'moment'; import moment from 'moment';
import TorrentOptions from './torrent_options'; import TorrentOptions from './torrent_options';
import ws_send from '../socket'; import ws_send from '../socket';
import date from '../date'; import DateDisplay from './date';
import selectTorrent, { import selectTorrent, {
EXCLUSIVE, EXCLUSIVE,
UNION, UNION,
@ -148,7 +148,7 @@ class Torrent extends Component {
<dt>Downloading to</dt> <dt>Downloading to</dt>
<dd>{torrent.path}</dd> <dd>{torrent.path}</dd>
<dt>Created</dt> <dt>Created</dt>
<dd>{date(moment(torrent.created))}</dd> <dd><DateDisplay when={moment(torrent.created)} /></dd>
</dl> </dl>
<TorrentOptions <TorrentOptions
id={torrent.id} id={torrent.id}
@ -195,7 +195,7 @@ class Torrent extends Component {
<dt>URL</dt> <dt>URL</dt>
<dd>{tracker.url}</dd> <dd>{tracker.url}</dd>
<dt>Last report</dt> <dt>Last report</dt>
<dd>{date(moment(tracker.last_report))}</dd> <dd><DateDisplay when={moment(tracker.last_report)} /></dd>
{tracker.error && <dt>Error</dt>} {tracker.error && <dt>Error</dt>}
{tracker.error && {tracker.error &&
<dd className="text-danger">{tracker.error}</dd>} <dd className="text-danger">{tracker.error}</dd>}