Files
astuto/app/javascript/components/Board/NewPost.tsx

197 lines
4.3 KiB
TypeScript
Raw Normal View History

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';
import I18n from 'i18n-js';
2019-09-02 14:32:57 +02:00
import NewPostForm from './NewPostForm';
import Spinner from '../common/Spinner';
import {
DangerText,
SuccessText,
} from '../common/CustomTexts';
import Button from '../common/Button';
2019-09-02 14:32:57 +02:00
import IBoard from '../../interfaces/IBoard';
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> {
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,
});
}
onTitleChange(title: string) {
2019-09-02 14:32:57 +02:00
this.setState({
title,
error: '',
});
}
onDescriptionChange(description: string) {
2019-09-02 14:32:57 +02:00
this.setState({
description,
});
}
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({
error: I18n.t('board.new_post.no_title'),
2019-09-02 14:32:57 +02:00
isLoading: false,
});
return;
}
try {
const res = await fetch('/posts', {
2019-09-02 14:32:57 +02:00
method: 'POST',
headers: buildRequestHeaders(authenticityToken),
2019-09-02 14:32:57 +02:00
body: JSON.stringify({
post: {
title,
description,
board_id: boardId,
},
}),
});
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({
success: I18n.t('board.new_post.submit_success'),
2019-09-06 14:36:26 +02:00
title: '',
description: '',
});
setTimeout(() => (
window.location.href = `/posts/${json.id}`
), 1000);
2019-09-06 14:36:26 +02:00
} else {
this.setState({error: json.error});
}
2019-09-02 14:32:57 +02:00
} catch (e) {
this.setState({
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 (
<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 ?
<Button
2019-09-02 14:32:57 +02:00
onClick={this.toggleForm}
className="submitBtn"
outline={showForm}>
{
showForm ?
I18n.t('board.new_post.cancel_button')
:
I18n.t('board.new_post.submit_button')
}
</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">
{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 }
{ error ? <DangerText>{error}</DangerText> : null }
{ success ? <SuccessText>{success}</SuccessText> : null }
2019-09-02 14:32:57 +02:00
</div>
);
}
}
export default NewPost;