2019-09-02 14:32:57 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
|
|
|
|
|
|
import NewPostForm from './NewPostForm';
|
2019-09-02 19:26:34 +02:00
|
|
|
import Spinner from '../shared/Spinner';
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
import IBoard from '../../interfaces/IBoard';
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
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) {
|
|
|
|
|
this.setState({
|
|
|
|
|
title,
|
|
|
|
|
error: '',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onDescriptionChange(description) {
|
|
|
|
|
this.setState({
|
|
|
|
|
description,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async submitForm(e) {
|
|
|
|
|
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: 'You forgot to enter a title!',
|
|
|
|
|
isLoading: false,
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2019-09-03 18:37:27 +02:00
|
|
|
let res = await fetch('/posts', {
|
2019-09-02 14:32:57 +02:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
Accept: 'application/json',
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'X-CSRF-Token': authenticityToken,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
post: {
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
board_id: boardId,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
this.setState({isLoading: false});
|
|
|
|
|
|
2019-09-04 15:24:15 +02:00
|
|
|
if (res.status === 204) this.setState({success: 'Your post has been published!'});
|
|
|
|
|
else {
|
|
|
|
|
let data = await res.json();
|
|
|
|
|
this.setState({error: data.message});
|
|
|
|
|
}
|
2019-09-02 14:32:57 +02:00
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.setState({
|
|
|
|
|
error: 'An unknown error occurred, try again.'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { board, isLoggedIn } = this.props;
|
|
|
|
|
const {
|
|
|
|
|
showForm,
|
|
|
|
|
error,
|
|
|
|
|
success,
|
|
|
|
|
isLoading,
|
|
|
|
|
|
|
|
|
|
title,
|
|
|
|
|
description
|
|
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
|
|
return (
|
2019-09-03 12:58:44 +02:00
|
|
|
<div className="box sidebar-box newBoardContainer">
|
2019-09-02 14:32:57 +02:00
|
|
|
<span className="boardName">{board.name}</span>
|
|
|
|
|
<span className="boardDescription">{board.description}</span>
|
|
|
|
|
{
|
|
|
|
|
isLoggedIn ?
|
|
|
|
|
<button
|
|
|
|
|
onClick={this.toggleForm}
|
2019-09-03 18:55:37 +02:00
|
|
|
className={`submitBtn btn btn-${showForm ? 'outline-' : ''}dark`}>
|
2019-09-02 14:32:57 +02:00
|
|
|
{ showForm ? 'Cancel' : 'Submit feedback' }
|
|
|
|
|
</button>
|
|
|
|
|
:
|
2019-09-03 18:37:27 +02:00
|
|
|
<a href="/users/sign_in" className="btn btn-dark">
|
2019-09-02 14:32:57 +02:00
|
|
|
Log in / Sign up
|
|
|
|
|
</a>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
showForm ?
|
|
|
|
|
<NewPostForm
|
|
|
|
|
title={title}
|
|
|
|
|
description={description}
|
|
|
|
|
handleTitleChange={this.onTitleChange}
|
|
|
|
|
handleDescriptionChange={this.onDescriptionChange}
|
|
|
|
|
handleSubmit={this.submitForm}
|
|
|
|
|
/>
|
|
|
|
|
:
|
|
|
|
|
null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{ isLoading ? <Spinner /> : null }
|
|
|
|
|
{ error ? <span className="error">{error}</span> : null }
|
|
|
|
|
{ success ? <span className="success">{success}</span> : null }
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default NewPost;
|