* refactor: move trackEvents to a utilities folder so that it can be re-used * feat: add a function called trackCommentClicks and use it on the articlePage * feat: update the auth widget to take the correct properties * feat: add create account tracking method for hamburger * feat: add create account tracking method for sidebar left * feat: whoops, sidebar tracking info added * feat: add the feed card create account tracking * feat: whoops add the id to the dom * feat: add top nav create account tracking * feat: create account tracking for the registration modal * fix: callback instead of invoking + rename class * fix: js-prepend * feat: some showLogin event tracking * use the tracking propert * x - feed.jsx * fix: oops I removed hidden * track from the search login * feat: add tracking for follow button * fix: main_stories is only being called in view/articles and both are including the FollowButtons pack * comments track * feat: remove punc (.) * chore: cahnge working * chore: remove tracking-id from modal * feat: remove ecmascript features * refactor: rename some events * feat: referrer page * feat: add a secondary source for the follow buttons * feat: remove file that is not used * chore: rename key * feat: use @utilities * feat: add spec for create account clicks * feat: add key referrer * fix: tests * fix: oops * chore: add commented test * test for tracking * feat: update the change by 1 * feat: aggregate_failures * feat: add data-no-instant back * feat: try a different way of testing * refactor: change secondary_source to referring_souce * feat: change ahoy event test * feat: add a version number to ahoy tracking * feat: change the href to pathname * feat: reply comment tracking * fix: to be present syntax * feat: use href instead of pathname * chore: do not track tags and podcasts follow and remove referrer * feat: add readinglist * fix: remove referrer from test * feat: fail gracefully in instances where there is no tracking data
216 lines
6.8 KiB
JavaScript
216 lines
6.8 KiB
JavaScript
/* 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;
|
||
}
|
||
|
||
commentSubscriptionClick = (event) => {
|
||
this.setState({
|
||
subscriptionType: event.target.value,
|
||
});
|
||
};
|
||
|
||
render() {
|
||
const { subscriptionType, subscribed } = this.state;
|
||
const {
|
||
onSubscribe,
|
||
onUnsubscribe,
|
||
positionType = 'relative',
|
||
isLoggedIn,
|
||
} = this.props;
|
||
|
||
const CogIcon = () => (
|
||
<svg
|
||
xmlns="http://www.w3.org/2000/svg"
|
||
width="24"
|
||
height="24"
|
||
role="img"
|
||
aria-labelledby="ai2ols8ka2ohfp0z568lj68ic2du21s"
|
||
className="crayons-icon"
|
||
>
|
||
<title id="ai2ols8ka2ohfp0z568lj68ic2du21s">Preferences</title>
|
||
<path d="M12 1l9.5 5.5v11L12 23l-9.5-5.5v-11L12 1zm0 2.311L4.5 7.653v8.694l7.5 4.342 7.5-4.342V7.653L12 3.311zM12 16a4 4 0 110-8 4 4 0 010 8zm0-2a2 2 0 100-4 2 2 0 000 4z" />
|
||
</svg>
|
||
);
|
||
|
||
return (
|
||
<div className={positionType}>
|
||
<ButtonGroup
|
||
labelText="Comment subscription options"
|
||
ref={(element) => {
|
||
this.buttonGroupElement = element;
|
||
}}
|
||
>
|
||
<Button
|
||
variant="outlined"
|
||
data-tracking-name="comment_subscription_toggle"
|
||
onClick={(_event) => {
|
||
if (isLoggedIn) {
|
||
if (subscribed) {
|
||
onUnsubscribe(COMMENT_SUBSCRIPTION_TYPE.NOT_SUBSCRIBED);
|
||
this.setState({
|
||
subscriptionType: COMMENT_SUBSCRIPTION_TYPE.ALL,
|
||
});
|
||
} else {
|
||
onSubscribe(subscriptionType);
|
||
}
|
||
|
||
this.setState({ subscribed: !subscribed });
|
||
} else {
|
||
showLoginModal({
|
||
referring_source: 'comments',
|
||
trigger: 'comment_subscription',
|
||
});
|
||
}
|
||
}}
|
||
>
|
||
{subscribed ? 'Unsubscribe' : 'Subscribe'}
|
||
</Button>
|
||
{subscribed ? (
|
||
<Button
|
||
id="subscription-settings-btn"
|
||
data-testid="subscription-settings"
|
||
data-tracking-name="comment_subscription_settings"
|
||
variant="outlined"
|
||
icon={CogIcon}
|
||
contentType="icon"
|
||
/>
|
||
) : null}
|
||
</ButtonGroup>
|
||
{subscribed && (
|
||
<Dropdown
|
||
triggerButtonId="subscription-settings-btn"
|
||
dropdownContentId="subscription-settings-dropdown"
|
||
dropdownContentCloseButtonId="subscription-settings-done-btn"
|
||
data-repositioning-dropdown="true"
|
||
data-testid="subscriptions-panel"
|
||
className={`right-0 p-4 s:left-auto${
|
||
positionType === 'relative' ? ' w-full' : ''
|
||
}`}
|
||
ref={(element) => {
|
||
this.dropdownElement = element;
|
||
}}
|
||
onOpen={() => this.setState({ showOptions: true })}
|
||
onClose={() => this.setState({ showOptions: false })}
|
||
>
|
||
<div className="crayons-fields mb-5">
|
||
<FormField variant="radio">
|
||
<RadioButton
|
||
id="subscribe-all"
|
||
name="subscribe_comments"
|
||
value={COMMENT_SUBSCRIPTION_TYPE.ALL}
|
||
checked={subscriptionType === COMMENT_SUBSCRIPTION_TYPE.ALL}
|
||
onClick={this.commentSubscriptionClick}
|
||
/>
|
||
<label htmlFor="subscribe-all" className="crayons-field__label">
|
||
All comments
|
||
<p className="crayons-field__description">
|
||
You’ll receive notifications for all new comments.
|
||
</p>
|
||
</label>
|
||
</FormField>
|
||
|
||
<FormField variant="radio">
|
||
<RadioButton
|
||
id="subscribe-toplevel"
|
||
name="subscribe_comments"
|
||
value={COMMENT_SUBSCRIPTION_TYPE.TOP}
|
||
onClick={this.commentSubscriptionClick}
|
||
checked={subscriptionType === COMMENT_SUBSCRIPTION_TYPE.TOP}
|
||
/>
|
||
<label
|
||
htmlFor="subscribe-toplevel"
|
||
className="crayons-field__label"
|
||
>
|
||
Top-level comments
|
||
<p className="crayons-field__description">
|
||
You’ll receive notifications only for all new top-level
|
||
comments.
|
||
</p>
|
||
</label>
|
||
</FormField>
|
||
|
||
<FormField variant="radio">
|
||
<RadioButton
|
||
id="subscribe-author"
|
||
name="subscribe_comments"
|
||
value={COMMENT_SUBSCRIPTION_TYPE.AUTHOR}
|
||
onClick={this.commentSubscriptionClick}
|
||
checked={
|
||
subscriptionType === COMMENT_SUBSCRIPTION_TYPE.AUTHOR
|
||
}
|
||
/>
|
||
<label
|
||
htmlFor="subscribe-author"
|
||
className="crayons-field__label"
|
||
>
|
||
Post author comments
|
||
<p className="crayons-field__description">
|
||
You’ll receive notifications only if post author sends a new
|
||
comment.
|
||
</p>
|
||
</label>
|
||
</FormField>
|
||
</div>
|
||
|
||
<Button
|
||
id="subscription-settings-done-btn"
|
||
className="w-100"
|
||
onClick={() => {
|
||
onSubscribe(this.state.subscriptionType);
|
||
}}
|
||
>
|
||
Done
|
||
</Button>
|
||
</Dropdown>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
}
|
||
|
||
CommentSubscription.displayName = 'CommentSubscription';
|
||
|
||
CommentSubscription.propTypes = {
|
||
positionType: PropTypes.oneOf(['absolute', 'relative', 'static']).isRequired,
|
||
onSubscribe: PropTypes.func.isRequired,
|
||
onUnsubscribe: PropTypes.func.isRequired,
|
||
subscriptionType: PropTypes.oneOf(
|
||
Object.entries(COMMENT_SUBSCRIPTION_TYPE).map(([, value]) => value),
|
||
).isRequired,
|
||
isLoggedIn: PropTypes.bool.isRequired,
|
||
};
|