import * as React from 'react'; import { Badge } from 'react-bootstrap'; import { Subscription } from 'rxjs'; import { ObservableLike } from '../../../WebRx'; export interface CountFooterContentProps { count: ObservableLike; suffix?: string; } export interface CountFooterContentComponentProps extends CountFooterContentProps, React.HTMLProps {} export interface CountFooterState { count: number; } export class CountFooterContent extends React.Component< CountFooterContentComponentProps, CountFooterState > { public static displayName = 'CountFooterContent'; private countChangedSub = Subscription.EMPTY; componentDidMount() { this.subscribeToCount(this.props.count); } componentDidUpdate(prevProps: Readonly) { if (prevProps.count !== this.props.count) { this.subscribeToCount(this.props.count); } } componentWillUnmount() { this.unsubscribeFromCount(); } render() { const count = this.state == null ? 0 : this.state.count || 0; return (
{count} {this.wxr.renderConditional( String.isNullOrEmpty(this.props.suffix) === false, () => ( {this.props.suffix} ), )}
); } protected unsubscribeFromCount() { this.countChangedSub = Subscription.unsubscribe(this.countChangedSub); } protected subscribeToCount(count: ObservableLike) { this.unsubscribeFromCount(); this.countChangedSub = this.wx.whenAny(count, x => x).subscribe(x => { this.setState((prevState, props) => { return { count: x || 0, }; }); }); } }