Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { React, useEffect, useReducer } from 'react';
import request from 'axios';
import Immutable from 'immutable';
import _ from 'lodash';
Expand All @@ -11,100 +11,96 @@ 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');
import css from '../SimpleCommentScreen/SimpleCommentScreen.scss';


const initialState = {
comments: Immutable.fromJS([]),
isSaving: false,
fetchCommentsError: null,
submitCommentError: null,
};
function commentsReducer(state, action) {
switch (action.type) {
case 'Add_Comment': return ({ ...state, comments: action.payload });
case 'Set_Is_Saving': return ({ ...state, isSaving: action.payload });
case 'Set_Fetch_Comment_Error': return ({ ...state, fetchCommentsError: action.payload });
case 'Submit_Comment_Error': return ({ ...state, submitCommentError: action.payload });
default: return state;
}

componentDidMount() {
this.fetchComments();
}
function SimpleHooksCommentScreen() {
const [commentsList, dispatch] = useReducer(commentsReducer, initialState);

function fetchComments() {
return request
.get('comments.json', { responseType: 'json' })
.then(res =>
dispatch({ type: 'Add_Comment', payload: Immutable.fromJS(res.data.comments) }))
.catch(error => dispatch({ type: 'Set_Fetch_Comment_Error', payload: error }));
}

fetchComments() {
return (
request
.get('comments.json', { responseType: 'json' })
.then(res => this.setState({ $$comments: Immutable.fromJS(res.data.comments) }))
.catch(error => this.setState({ fetchCommentsError: error }))
);
}
useEffect(() => {
fetchComments();
});


handleCommentSubmit(comment) {
this.setState({ isSaving: true });
function handleCommentSubmit(comment) {
dispatch({ type: 'Set_Is_Saving', payload: 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,
});
})
);
return request
.post('comments.json', { comment }, requestConfig)
.then(() => {
const $$comment = Immutable.fromJS(comment);
dispatch({ type: 'Add_Comment', payload: comment.unshift($$comment) });
// setComments(comments.unshift($$comment));
dispatch({ type: 'Submit_Comment_Error', payload: null });
// setIsSaving(false);
dispatch({ type: 'Set_Is_Saving', payload: false });
})
.catch(error => {
dispatch({ type: 'Submit_Comment_Error', payload: error });
dispatch({ type: 'Set_Is_Saving', payload: 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>
);
}
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={commentsList.isSaving}
actions={{ submitComment: handleCommentSubmit }}
error={commentsList.submitCommentError}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
<CommentList
$$comments={commentsList.comments}
error={commentsList.fetchCommentsError}
cssTransitionGroupClassNames={cssTransitionGroupClassNames}
/>
</div>
);
}

export default class I18nWrapper extends BaseComponent {
Expand All @@ -125,11 +121,11 @@ export default class I18nWrapper extends BaseComponent {
render() {
const { locale } = this.state;
const messages = translations[locale];
const InjectedSimpleCommentScreen = injectIntl(SimpleCommentScreen);
const InjectedSimpleHooksCommentScreen = injectIntl(SimpleHooksCommentScreen);

return (
<IntlProvider locale={locale} key={locale} messages={messages}>
<InjectedSimpleCommentScreen
<InjectedSimpleHooksCommentScreen
{...this.props}
locale={locale}
handleSetLocale={this.handleSetLocale}
Expand Down