|
| 1 | +// eslint-disable-next-line max-classes-per-file |
| 2 | +import React from 'react'; |
| 3 | +import request from 'axios'; |
| 4 | +import Immutable from 'immutable'; |
| 5 | +import _ from 'lodash'; |
| 6 | +import ReactOnRails from 'react-on-rails'; |
| 7 | +import { IntlProvider, injectIntl } from 'react-intl'; |
| 8 | +import BaseComponent from 'libs/components/BaseComponent'; |
| 9 | +import SelectLanguage from 'libs/i18n/selectLanguage'; |
| 10 | +import { defaultMessages, defaultLocale } from 'libs/i18n/default'; |
| 11 | +import { translations } from 'libs/i18n/translations'; |
| 12 | + |
| 13 | +import { Turbo } from '@hotwired/turbo-rails'; |
| 14 | +import CommentForm from '../CommentBox/CommentForm/CommentForm'; |
| 15 | +import css from './HotwiredCommentScreen.module.scss'; |
| 16 | + |
| 17 | +class HotwiredCommentForm extends BaseComponent { |
| 18 | + constructor(props) { |
| 19 | + super(props); |
| 20 | + |
| 21 | + this.state = { |
| 22 | + isSaving: false, |
| 23 | + submitCommentError: null, |
| 24 | + }; |
| 25 | + |
| 26 | + _.bindAll(this, 'handleCommentSubmit'); |
| 27 | + } |
| 28 | + |
| 29 | + componentDidMount() { |
| 30 | + } |
| 31 | + |
| 32 | + handleCommentSubmit(comment) { |
| 33 | + this.setState({ isSaving: true }); |
| 34 | + |
| 35 | + const requestConfig = { |
| 36 | + responseType: 'text/vnd.turbo-stream.html', |
| 37 | + headers: ReactOnRails.authenticityHeaders(), |
| 38 | + }; |
| 39 | + |
| 40 | + return request |
| 41 | + .post('comments.turbo_stream', { comment }, requestConfig) |
| 42 | + .then(r => r.data) |
| 43 | + .then(html => { |
| 44 | + Turbo.renderStreamMessage(html) |
| 45 | + }) |
| 46 | + .then(() => { |
| 47 | + const { $$comments } = this.state; |
| 48 | + const $$comment = Immutable.fromJS(comment); |
| 49 | + |
| 50 | + this.setState({ |
| 51 | + $$comments: $$comments.unshift($$comment), |
| 52 | + submitCommentError: null, |
| 53 | + isSaving: false, |
| 54 | + }); |
| 55 | + }) |
| 56 | + .catch((error) => { |
| 57 | + this.setState({ |
| 58 | + submitCommentError: error, |
| 59 | + isSaving: false, |
| 60 | + }); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + render() { |
| 65 | + const { handleSetLocale, locale, intl } = this.props; |
| 66 | + const cssTransitionGroupClassNames = { |
| 67 | + enter: css.elementEnter, |
| 68 | + enterActive: css.elementEnterActive, |
| 69 | + exit: css.elementLeave, |
| 70 | + exitActive: css.elementLeaveActive, |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="commentBox prose max-w-none prose-a:text-sky-700 prose-li:my-0"> |
| 75 | + {SelectLanguage(handleSetLocale, locale)} |
| 76 | + |
| 77 | + <CommentForm |
| 78 | + isSaving={this.state.isSaving} |
| 79 | + actions={{ submitComment: this.handleCommentSubmit }} |
| 80 | + error={{ error: this.state.submitCommentError, nodeRef: React.createRef(null) }} |
| 81 | + cssTransitionGroupClassNames={cssTransitionGroupClassNames} |
| 82 | + /> |
| 83 | + |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export default class I18nWrapper extends BaseComponent { |
| 90 | + constructor(props) { |
| 91 | + super(props); |
| 92 | + |
| 93 | + this.state = { |
| 94 | + locale: defaultLocale, |
| 95 | + }; |
| 96 | + |
| 97 | + _.bindAll(this, 'handleSetLocale'); |
| 98 | + } |
| 99 | + |
| 100 | + handleSetLocale(locale) { |
| 101 | + this.setState({ locale }); |
| 102 | + } |
| 103 | + |
| 104 | + render() { |
| 105 | + const { locale } = this.state; |
| 106 | + const messages = translations[locale]; |
| 107 | + const InjectedHotwiredCommentForm = injectIntl(HotwiredCommentForm); |
| 108 | + |
| 109 | + return ( |
| 110 | + <IntlProvider locale={locale} key={locale} messages={messages}> |
| 111 | + <InjectedHotwiredCommentForm |
| 112 | + /* eslint-disable-next-line react/jsx-props-no-spreading */ |
| 113 | + {...this.props} |
| 114 | + locale={locale} |
| 115 | + handleSetLocale={this.handleSetLocale} |
| 116 | + /> |
| 117 | + </IntlProvider> |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments