|
| 1 | +import { React, useState, useEffect } from "react"; |
| 2 | +import request from "axios"; |
| 3 | +import Immutable from "immutable"; |
| 4 | +import _ from "lodash"; |
| 5 | +import ReactOnRails from "react-on-rails"; |
| 6 | +import { IntlProvider, injectIntl } from "react-intl"; |
| 7 | +import BaseComponent from 'libs/components/BaseComponent'; |
| 8 | +import SelectLanguage from "libs/i18n/selectLanguage"; |
| 9 | +import { defaultMessages, defaultLocale } from "libs/i18n/default"; |
| 10 | +import { translations } from "libs/i18n/translations"; |
| 11 | + |
| 12 | +import CommentForm from "../CommentBox/CommentForm/CommentForm"; |
| 13 | +import CommentList from "../CommentBox/CommentList/CommentList"; |
| 14 | +import css from "../SimpleCommentScreen/SimpleCommentScreen.scss"; |
| 15 | + |
| 16 | +function SimpleHooksCommentScreen() { |
| 17 | + const [comments, setComments] = useState(Immutable.fromJS([])); |
| 18 | + const [isSaving, setIsSaving] = useState(false); |
| 19 | + const [fetchCommentsError, setFetchCommentsError] = useState(null); |
| 20 | + const [submitCommentError, setSubmitCommentError] = usestate(null); |
| 21 | + |
| 22 | + useEffect(() => { |
| 23 | + this.fetchComments(); |
| 24 | + }); |
| 25 | + |
| 26 | + function fetchComments() { |
| 27 | + return request |
| 28 | + .get("comments.json", { responseType: "json" }) |
| 29 | + .then(res => |
| 30 | + setComments(Immutable.fromJS(res.data.comments)) |
| 31 | + ) |
| 32 | + .catch(error => setFetchCommentsError(error)); |
| 33 | + } |
| 34 | + |
| 35 | + function handleCommentSubmit(comment) { |
| 36 | + setIsSaving(true); |
| 37 | + |
| 38 | + const requestConfig = { |
| 39 | + responseType: "json", |
| 40 | + headers: ReactOnRails.authenticityHeaders() |
| 41 | + }; |
| 42 | + |
| 43 | + return request |
| 44 | + .post("comments.json", { comment }, requestConfig) |
| 45 | + .then(() => { |
| 46 | + const $$comment = Immutable.fromJS(comment); |
| 47 | + setComments(comments.unshift($$comment)); |
| 48 | + setSubmitCommentError(null); |
| 49 | + setIsSaving(false); |
| 50 | + }) |
| 51 | + .catch(error => { |
| 52 | + setSubmitCommentError(error) |
| 53 | + setIsSaving(false); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + const { handleSetLocale, locale, intl } = this.props; |
| 59 | + const { formatMessage } = intl; |
| 60 | + const cssTransitionGroupClassNames = { |
| 61 | + enter: css.elementEnter, |
| 62 | + enterActive: css.elementEnterActive, |
| 63 | + leave: css.elementLeave, |
| 64 | + leaveActive: css.elementLeaveActive |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="commentBox container"> |
| 69 | + <h2>{formatMessage(defaultMessages.comments)}</h2> |
| 70 | + {SelectLanguage(handleSetLocale, locale)} |
| 71 | + <ul> |
| 72 | + <li>{formatMessage(defaultMessages.descriptionSupportMarkdown)}</li> |
| 73 | + <li>{formatMessage(defaultMessages.descriptionDeleteRule)}</li> |
| 74 | + <li>{formatMessage(defaultMessages.descriptionSubmitRule)}</li> |
| 75 | + </ul> |
| 76 | + <CommentForm |
| 77 | + isSaving={isSaving} |
| 78 | + actions={{ submitComment: handleCommentSubmit }} |
| 79 | + error={submitCommentError} |
| 80 | + cssTransitionGroupClassNames={cssTransitionGroupClassNames} |
| 81 | + /> |
| 82 | + <CommentList |
| 83 | + $$comments={comments} |
| 84 | + error={fetchCommentsError} |
| 85 | + cssTransitionGroupClassNames={cssTransitionGroupClassNames} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +export default class I18nWrapper extends BaseComponent { |
| 92 | + constructor(props) { |
| 93 | + super(props); |
| 94 | + |
| 95 | + this.state = { |
| 96 | + locale: defaultLocale |
| 97 | + }; |
| 98 | + |
| 99 | + _.bindAll(this, "handleSetLocale"); |
| 100 | + } |
| 101 | + |
| 102 | + handleSetLocale(locale) { |
| 103 | + this.setState({ locale }); |
| 104 | + } |
| 105 | + |
| 106 | + render() { |
| 107 | + const { locale } = this.state; |
| 108 | + const messages = translations[locale]; |
| 109 | + const InjectedSimpleHooksCommentScreen = injectIntl(SimpleHooksCommentScreen); |
| 110 | + |
| 111 | + return ( |
| 112 | + <IntlProvider locale={locale} key={locale} messages={messages}> |
| 113 | + <InjectedSimpleHooksCommentScreen |
| 114 | + {...this.props} |
| 115 | + locale={locale} |
| 116 | + handleSetLocale={this.handleSetLocale} |
| 117 | + /> |
| 118 | + </IntlProvider> |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments