forked from shakacode/react-webpack-rails-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentBox.jsx
126 lines (117 loc) · 4.12 KB
/
CommentBox.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
import React from 'react';
import PropTypes from 'prop-types';
import Immutable from 'immutable';
import _ from 'lodash';
import { injectIntl } from 'react-intl';
import BaseComponent from 'libs/components/BaseComponent';
import SelectLanguage from 'libs/i18n/selectLanguage';
import { defaultMessages, defaultLocale } from 'libs/i18n/default';
import CommentForm from './CommentForm/CommentForm';
import CommentList, { commentPropTypes } from './CommentList/CommentList';
import css from './CommentBox.module.scss';
class CommentBox extends BaseComponent {
static propTypes = {
pollInterval: PropTypes.number.isRequired,
actions: PropTypes.shape({
fetchComments: PropTypes.func,
}),
data: PropTypes.shape({
isFetching: PropTypes.func,
isSaving: PropTypes.bool,
submitCommentError: PropTypes.string,
$$comments: PropTypes.arrayOf(commentPropTypes),
}).isRequired,
// eslint-disable-next-line react/forbid-prop-types
intl: PropTypes.objectOf(PropTypes.any).isRequired,
};
constructor() {
super();
_.bindAll(this, ['refreshComments']);
this.cable = null;
}
subscribeChannel() {
const { messageReceived } = this.props.actions;
const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://';
const cableUrl = `${protocol}${window.location.hostname}:${window.location.port}/cable`;
// ActionCable is a global added through webpack.providePlugin
// eslint-disable-next-line no-undef
this.cable = ActionCable.createConsumer(cableUrl);
/* eslint no-console: ["error", { allow: ["log"] }] */
this.cable.subscriptions.create(
{ channel: 'CommentsChannel' },
{
connected: () => {
console.log('connected');
},
disconnected: () => {
console.log('disconnected');
},
received: (comment) => {
messageReceived(Immutable.fromJS(comment));
},
},
);
}
componentDidMount() {
const { fetchComments } = this.props.actions;
fetchComments();
this.subscribeChannel();
}
componentWillUnmount() {
this.cable.subscriptions.remove({ channel: 'CommentsChannel' });
}
refreshComments() {
const { fetchComments } = this.props.actions;
fetchComments();
}
render() {
const { actions, data, intl } = this.props;
const { formatMessage } = intl;
const cssTransitionGroupClassNames = {
enter: css.elementEnter,
enterActive: css.elementEnterActive,
exit: css.elementLeave,
exitActive: css.elementLeaveActive,
};
const locale = data.get('locale') || defaultLocale;
/* eslint-disable no-script-url */
return (
<div className="commentBox prose max-w-none prose-a:text-sky-700 prose-li:my-0">
<h2>
{formatMessage(defaultMessages.comments)}
{data.get('isFetching') && formatMessage(defaultMessages.loading)}
</h2>
{SelectLanguage(actions.setLocale, locale)}
<ul>
<li>
{(data.get('isFetching') && <br />) || (
<button
className="m-0 text-sky-700 hover:underline"
onClick={this.refreshComments}
type="button"
>
{formatMessage(defaultMessages.descriptionForceRefrech)}
</button>
)}
</li>
<li>{formatMessage(defaultMessages.descriptionSupportMarkdown)}</li>
<li>{formatMessage(defaultMessages.descriptionDeleteRule)}</li>
<li>{formatMessage(defaultMessages.descriptionSubmitRule)}</li>
<li>{formatMessage(defaultMessages.descriptionSeeActionCable)}</li>
</ul>
<CommentForm
isSaving={data.get('isSaving')}
error={{ error: data.get('submitCommentError'), nodeRef: React.createRef(null) }}
actions={actions}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
<CommentList
$$comments={data.get('$$comments')}
error={data.get('fetchCommentError')}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
</div>
);
}
}
export default injectIntl(CommentBox);