/* global showLoginModal */ import { h, Component } from 'preact'; import PropTypes from 'prop-types'; import { Button, ButtonGroup, Dropdown, FormField, RadioButton, } from '@crayons'; export const COMMENT_SUBSCRIPTION_TYPE = Object.freeze({ ALL: 'all_comments', TOP: 'top_level_comments', AUTHOR: 'only_author_comments', NOT_SUBSCRIBED: 'not_subscribed', }); export class CommentSubscription extends Component { constructor(props) { const { subscriptionType } = props; super(props); const subscribed = subscriptionType && (subscriptionType.length > 0 && subscriptionType) !== COMMENT_SUBSCRIPTION_TYPE.NOT_SUBSCRIBED; const initialState = { subscriptionType: subscribed ? subscriptionType : COMMENT_SUBSCRIPTION_TYPE.ALL, subscribed, showOptions: false, }; this.state = initialState; } componentDidUpdate() { const { showOptions } = this.state; if (showOptions) { window.addEventListener('scroll', this.dropdownPlacementHandler); this.dropdownPlacementHandler(); } else { window.removeEventListener('scroll', this.dropdownPlacementHandler); } } componentWillUnmount() { window.removeEventListener('scroll', this.dropdownPlacementHandler); } dropdownPlacementHandler = () => { const { base: element } = this.dropdownElement; // Reset the top before doing any calculations element.style.bottom = ''; const { bottom: dropDownBottom } = element.getBoundingClientRect(); const { height } = this.buttonGroupElement.base.getBoundingClientRect(); if ( Math.sign(dropDownBottom) === -1 || dropDownBottom > window.innerHeight ) { // The 4 pixels is the box shadow from the drop down. element.style.bottom = `${height + 4}px`; } }; commentSubscriptionClick = (event) => { this.setState({ subscriptionType: event.target.value, }); }; render() { const { subscriptionType, subscribed } = this.state; const { onSubscribe, onUnsubscribe, positionType = 'relative', isLoggedIn, } = this.props; const CogIcon = () => ( ); return (