2019-09-02 14:32:57 +02:00
|
|
|
import * as React from 'react';
|
2022-06-06 20:20:03 +02:00
|
|
|
import ReactMarkdown from 'react-markdown';
|
2022-06-05 11:40:43 +02:00
|
|
|
import I18n from 'i18n-js';
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
import NewPostForm from './NewPostForm';
|
2022-06-08 10:20:36 +02:00
|
|
|
import Spinner from '../common/Spinner';
|
2019-09-15 18:26:51 +02:00
|
|
|
import {
|
|
|
|
|
DangerText,
|
|
|
|
|
SuccessText,
|
2022-06-08 10:20:36 +02:00
|
|
|
} from '../common/CustomTexts';
|
|
|
|
|
import Button from '../common/Button';
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
import IBoard from '../../interfaces/IBoard';
|
2022-04-09 10:45:43 +02:00
|
|
|
import buildRequestHeaders from '../../helpers/buildRequestHeaders';
|
2022-05-01 18:00:38 +02:00
|
|
|
import HttpStatus from '../../constants/http_status';
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
board: IBoard;
|
|
|
|
|
isLoggedIn: boolean;
|
|
|
|
|
authenticityToken: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
|
showForm: boolean;
|
|
|
|
|
error: string;
|
|
|
|
|
success: string;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class NewPost extends React.Component<Props, State> {
|
2019-09-26 11:00:32 +02:00
|
|
|
constructor(props: Props) {
|
2019-09-02 14:32:57 +02:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
showForm: false,
|
|
|
|
|
error: '',
|
|
|
|
|
success: '',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
|
|
|
|
|
title: '',
|
|
|
|
|
description: '',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.toggleForm = this.toggleForm.bind(this);
|
|
|
|
|
this.onTitleChange = this.onTitleChange.bind(this);
|
|
|
|
|
this.onDescriptionChange = this.onDescriptionChange.bind(this);
|
|
|
|
|
this.submitForm = this.submitForm.bind(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toggleForm() {
|
|
|
|
|
this.setState({
|
|
|
|
|
showForm: !this.state.showForm,
|
|
|
|
|
error: '',
|
|
|
|
|
success: '',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 11:00:32 +02:00
|
|
|
onTitleChange(title: string) {
|
2019-09-02 14:32:57 +02:00
|
|
|
this.setState({
|
|
|
|
|
title,
|
|
|
|
|
error: '',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 11:00:32 +02:00
|
|
|
onDescriptionChange(description: string) {
|
2019-09-02 14:32:57 +02:00
|
|
|
this.setState({
|
|
|
|
|
description,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-26 11:00:32 +02:00
|
|
|
async submitForm(e: React.FormEvent) {
|
2019-09-02 14:32:57 +02:00
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
error: '',
|
|
|
|
|
success: '',
|
|
|
|
|
isLoading: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const boardId = this.props.board.id;
|
|
|
|
|
const { authenticityToken } = this.props;
|
|
|
|
|
const { title, description } = this.state;
|
|
|
|
|
|
|
|
|
|
if (title === '') {
|
|
|
|
|
this.setState({
|
2022-06-05 11:40:43 +02:00
|
|
|
error: I18n.t('board.new_post.no_title'),
|
2019-09-02 14:32:57 +02:00
|
|
|
isLoading: false,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2019-09-21 11:17:58 +02:00
|
|
|
const res = await fetch('/posts', {
|
2019-09-02 14:32:57 +02:00
|
|
|
method: 'POST',
|
2022-04-09 10:45:43 +02:00
|
|
|
headers: buildRequestHeaders(authenticityToken),
|
2019-09-02 14:32:57 +02:00
|
|
|
body: JSON.stringify({
|
|
|
|
|
post: {
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
board_id: boardId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
2019-09-21 11:17:58 +02:00
|
|
|
const json = await res.json();
|
2019-09-02 14:32:57 +02:00
|
|
|
this.setState({isLoading: false});
|
|
|
|
|
|
2022-05-01 18:00:38 +02:00
|
|
|
if (res.status === HttpStatus.Created) {
|
2019-09-06 14:36:26 +02:00
|
|
|
this.setState({
|
2022-06-05 11:40:43 +02:00
|
|
|
success: I18n.t('board.new_post.submit_success'),
|
2019-09-06 14:36:26 +02:00
|
|
|
|
|
|
|
|
title: '',
|
|
|
|
|
description: '',
|
|
|
|
|
});
|
2019-09-21 11:17:58 +02:00
|
|
|
|
|
|
|
|
setTimeout(() => (
|
|
|
|
|
window.location.href = `/posts/${json.id}`
|
|
|
|
|
), 1000);
|
2019-09-06 14:36:26 +02:00
|
|
|
} else {
|
2019-09-21 11:17:58 +02:00
|
|
|
this.setState({error: json.error});
|
2019-09-04 15:24:15 +02:00
|
|
|
}
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.setState({
|
2022-06-05 11:40:43 +02:00
|
|
|
error: I18n.t('board.new_post.submit_error')
|
2019-09-02 14:32:57 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { board, isLoggedIn } = this.props;
|
|
|
|
|
const {
|
|
|
|
|
showForm,
|
|
|
|
|
error,
|
|
|
|
|
success,
|
|
|
|
|
isLoading,
|
|
|
|
|
|
|
|
|
|
title,
|
|
|
|
|
description
|
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
2022-06-08 10:20:36 +02:00
|
|
|
<div className="newPostContainer sidebarBox">
|
2019-09-19 16:42:43 +02:00
|
|
|
<span className="boardTitle">{board.name}</span>
|
2022-06-06 20:20:03 +02:00
|
|
|
|
|
|
|
|
<ReactMarkdown
|
|
|
|
|
className="boardDescription"
|
|
|
|
|
disallowedTypes={['heading', 'image', 'html']}
|
|
|
|
|
unwrapDisallowed
|
|
|
|
|
>
|
|
|
|
|
{board.description}
|
|
|
|
|
</ReactMarkdown>
|
|
|
|
|
|
2019-09-02 14:32:57 +02:00
|
|
|
{
|
|
|
|
|
isLoggedIn ?
|
2019-09-16 12:22:30 +02:00
|
|
|
<Button
|
2019-09-02 14:32:57 +02:00
|
|
|
onClick={this.toggleForm}
|
2019-09-16 12:22:30 +02:00
|
|
|
className="submitBtn"
|
|
|
|
|
outline={showForm}>
|
2022-06-05 11:40:43 +02:00
|
|
|
{
|
|
|
|
|
showForm ?
|
|
|
|
|
I18n.t('board.new_post.cancel_button')
|
|
|
|
|
:
|
|
|
|
|
I18n.t('board.new_post.submit_button')
|
|
|
|
|
}
|
2019-09-16 12:22:30 +02:00
|
|
|
</Button>
|
2019-09-02 14:32:57 +02:00
|
|
|
:
|
2024-01-23 18:50:42 +01:00
|
|
|
<a href="/users/sign_in" className="btn btnPrimary">
|
2022-06-05 11:40:43 +02:00
|
|
|
{I18n.t('board.new_post.login_button')}
|
2019-09-02 14:32:57 +02:00
|
|
|
</a>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
showForm ?
|
|
|
|
|
<NewPostForm
|
|
|
|
|
title={title}
|
|
|
|
|
description={description}
|
|
|
|
|
handleTitleChange={this.onTitleChange}
|
|
|
|
|
handleDescriptionChange={this.onDescriptionChange}
|
|
|
|
|
handleSubmit={this.submitForm}
|
|
|
|
|
/>
|
|
|
|
|
:
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ isLoading ? <Spinner /> : null }
|
2019-09-15 18:26:51 +02:00
|
|
|
{ error ? <DangerText>{error}</DangerText> : null }
|
|
|
|
|
{ success ? <SuccessText>{success}</SuccessText> : null }
|
2019-09-02 14:32:57 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default NewPost;
|