mirror of
https://github.com/wassname/talk.git
synced 2026-07-02 23:39:20 +08:00
Automatically close stream when passed closedAt date
This commit is contained in:
@@ -19,6 +19,7 @@ import cn from 'classnames';
|
||||
|
||||
import {getTopLevelParent, attachCommentToParent} from '../graphql/utils';
|
||||
import AllCommentsPane from './AllCommentsPane';
|
||||
import AutomaticAssetClosure from '../containers/AutomaticAssetClosure';
|
||||
|
||||
import styles from './Stream.css';
|
||||
|
||||
@@ -141,6 +142,7 @@ class Stream extends React.Component {
|
||||
|
||||
return (
|
||||
<div id="stream" className={styles.root}>
|
||||
<AutomaticAssetClosure assetId={asset.id} closedAt={asset.closedAt}/>
|
||||
{comment &&
|
||||
<Button
|
||||
cStyle="darkGrey"
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import {gql} from 'react-apollo';
|
||||
|
||||
const FRAGMENT = gql`
|
||||
fragment CoralEmbedStream_AutomaticAssetClosure_Fragment on Asset {
|
||||
id
|
||||
isClosed
|
||||
}
|
||||
`;
|
||||
|
||||
function getFragmentId(assetId) {
|
||||
return `Asset_${assetId}`;
|
||||
}
|
||||
|
||||
class AutomaticAssetClosure extends React.Component {
|
||||
static contextTypes = {
|
||||
client: PropTypes.object.isRequired,
|
||||
};
|
||||
|
||||
timer = null;
|
||||
|
||||
componentWillMount() {
|
||||
this.setupTimer(this.props.assetId, this.props.closedAt);
|
||||
}
|
||||
|
||||
componentWillReceiveProps(next) {
|
||||
if (
|
||||
this.props.assetId !== next.assetId ||
|
||||
this.props.closedAt !== next.closedAt
|
||||
) {
|
||||
this.setupTimer(next.assetId, next.closedAt);
|
||||
}
|
||||
}
|
||||
|
||||
closeAsset(assetId) {
|
||||
this.context.client.writeFragment({
|
||||
fragment: FRAGMENT,
|
||||
id: getFragmentId(assetId),
|
||||
data: {
|
||||
isClosed: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
setupTimer(assetId, closedAt) {
|
||||
clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
|
||||
if (assetId && closedAt) {
|
||||
const asset = this.context.client.readFragment({
|
||||
fragment: FRAGMENT,
|
||||
id: getFragmentId(assetId),
|
||||
});
|
||||
|
||||
if (!asset.isClosed && closedAt) {
|
||||
const diff = (new Date(closedAt) - new Date());
|
||||
if (diff >= 0) {
|
||||
this.timer = setTimeout(() => this.closeAsset(assetId), diff);
|
||||
} else {
|
||||
this.closeAsset(assetId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
AutomaticAssetClosure.PropTypes = {
|
||||
assetId: PropTypes.string,
|
||||
closedAt: PropTypes.string,
|
||||
};
|
||||
|
||||
export default AutomaticAssetClosure;
|
||||
@@ -250,6 +250,7 @@ const fragments = {
|
||||
id
|
||||
title
|
||||
url
|
||||
closedAt
|
||||
isClosed
|
||||
created_at
|
||||
settings {
|
||||
|
||||
Reference in New Issue
Block a user