forked from shakacode/react-webpack-rails-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCommentScreen.jsx
140 lines (122 loc) · 3.83 KB
/
SimpleCommentScreen.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
import React from 'react';
import request from 'axios';
import Immutable from 'immutable';
import _ from 'lodash';
import ReactOnRails from 'react-on-rails';
import { IntlProvider, injectIntl } from 'react-intl';
import BaseComponent from 'libs/components/BaseComponent';
import SelectLanguage from 'libs/i18n/selectLanguage';
import { defaultMessages, defaultLocale } from 'libs/i18n/default';
import { translations } from 'libs/i18n/translations';
import CommentForm from '../CommentBox/CommentForm/CommentForm';
import CommentList from '../CommentBox/CommentList/CommentList';
import css from './SimpleCommentScreen.scss';
class SimpleCommentScreen extends BaseComponent {
constructor(props) {
super(props);
this.state = {
$$comments: Immutable.fromJS([]),
isSaving: false,
fetchCommentsError: null,
submitCommentError: null,
};
_.bindAll(this, 'fetchComments', 'handleCommentSubmit');
}
componentDidMount() {
this.fetchComments();
}
fetchComments() {
return (
request
.get('comments.json', { responseType: 'json' })
.then(res => this.setState({ $$comments: Immutable.fromJS(res.data.comments) }))
.catch(error => this.setState({ fetchCommentsError: error }))
);
}
handleCommentSubmit(comment) {
this.setState({ isSaving: true });
const requestConfig = {
responseType: 'json',
headers: ReactOnRails.authenticityHeaders(),
};
return (
request
.post('comments.json', { comment }, requestConfig)
.then(() => {
const { $$comments } = this.state;
const $$comment = Immutable.fromJS(comment);
this.setState({
$$comments: $$comments.unshift($$comment),
submitCommentError: null,
isSaving: false,
});
})
.catch(error => {
this.setState({
submitCommentError: error,
isSaving: false,
});
})
);
}
render() {
const { handleSetLocale, locale, intl } = this.props;
const { formatMessage } = intl;
const cssTransitionGroupClassNames = {
enter: css.elementEnter,
enterActive: css.elementEnterActive,
leave: css.elementLeave,
leaveActive: css.elementLeaveActive,
};
return (
<div className="commentBox container">
<h2>
{formatMessage(defaultMessages.comments)}
</h2>
{ SelectLanguage(handleSetLocale, locale) }
<ul>
<li>{formatMessage(defaultMessages.descriptionSupportMarkdown)}</li>
<li>{formatMessage(defaultMessages.descriptionDeleteRule)}</li>
<li>{formatMessage(defaultMessages.descriptionSubmitRule)}</li>
</ul>
<CommentForm
isSaving={this.state.isSaving}
actions={{ submitComment: this.handleCommentSubmit }}
error={this.state.submitCommentError}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
<CommentList
$$comments={this.state.$$comments}
error={this.state.fetchCommentsError}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
</div>
);
}
}
export default class I18nWrapper extends BaseComponent {
constructor(props) {
super(props);
this.state = {
locale: defaultLocale,
};
_.bindAll(this, 'handleSetLocale');
}
handleSetLocale(locale) {
this.setState({ locale });
}
render() {
const { locale } = this.state;
const messages = translations[locale];
const InjectedSimpleCommentScreen = injectIntl(SimpleCommentScreen);
return (
<IntlProvider locale={locale} key={locale} messages={messages}>
<InjectedSimpleCommentScreen
{...this.props}
locale={locale}
handleSetLocale={this.handleSetLocale}
/>
</IntlProvider>
);
}
}