-
Notifications
You must be signed in to change notification settings - Fork 4k
/
CommentSubscription.jsx
211 lines (195 loc) · 6.58 KB
/
CommentSubscription.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* 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"
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();
}
}}
>
{subscribed ? 'Unsubscribe' : 'Subscribe'}
</Button>
{subscribed ? (
<Button
id="subscription-settings-btn"
data-testid="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,
};