import React from 'react'; import { Alert, Button, Modal, Checkbox, ButtonGroup } from 'react-bootstrap'; import { observer } from 'mobx-react'; import autobind from 'autobind-decorator'; import { computed, makeObservable, observable, ObservableMap } from 'mobx'; import expiredStorage from 'expired-storage'; import _ from 'lodash'; const SECONDS_IN_DAY = 86400; interface IUsageAgreement { alertMessage: JSX.Element; persistenceKey: string; clauses: JSX.Element[]; useCheckboxes?: boolean; expirationInDays?: number; } @observer export default class UsageAgreement extends React.Component< IUsageAgreement, {} > { @observable show: boolean = true; @observable modalShow: boolean = false; @observable checkedItems = observable.map(); constructor(props: IUsageAgreement) { super(props); makeObservable(this); } @computed get expirationInSeconds() { return this.props.expirationInDays ? this.props.expirationInDays * SECONDS_IN_DAY : undefined; } @computed get useCheckboxes() { return ( this.props.useCheckboxes === true || this.props.useCheckboxes === undefined ); } @computed get isAgreementComplete(): boolean { return ( this.useCheckboxes === false || (this.checkedItems.size === this.props.clauses.length && _.every( Array.from(this.checkedItems.values()), val => val === true )) ); } @autobind handleDismiss() { this.show = false; this.modalShow = false; new expiredStorage().setItem( this.props.persistenceKey, 'true', this.expirationInSeconds ); //localStorage.setItem(this.props.persistenceKey, 'true'); } @autobind handleHide() { this.show = false; } @autobind handleShow() { this.show = true; } @autobind handleModalShow() { this.modalShow = true; } @autobind handleModalHide() { this.modalShow = false; } @autobind handleChecked(item: string) { this.checkedItems.set(item, !this.checkedItems.get(item)); } render() { if (this.show) { return ( <> {this.props.alertMessage} Dismiss Agreement {this.useCheckboxes && this.props.clauses.map((label, i) => { return ( { this.handleChecked(`item${i}`); }} > {label} ); })} {this.props.useCheckboxes === false && (
    {this.props.clauses.map(label => { return
  • {label}
  • ; })}
)}
{this.isAgreementComplete ? ( ) : ( )}
); } else { return null; } } }