Files
astuto/app/javascript/components/Post/PostP.tsx

192 lines
5.6 KiB
TypeScript
Raw Normal View History

2019-09-12 15:51:45 +02:00
import * as React from 'react';
import I18n from 'i18n-js';
2019-09-12 15:51:45 +02:00
import IPost from '../../interfaces/IPost';
import IPostStatus from '../../interfaces/IPostStatus';
2019-09-21 12:54:57 +02:00
import IBoard from '../../interfaces/IBoard';
2019-09-12 15:51:45 +02:00
import PostUpdateList from './PostUpdateList';
2019-09-30 16:54:37 +02:00
import LikeList from './LikeList';
import ActionBox from './ActionBox';
2019-09-30 16:54:37 +02:00
import LikeButton from '../../containers/LikeButton';
2019-09-21 12:54:57 +02:00
import PostBoardSelect from './PostBoardSelect';
2019-09-12 15:51:45 +02:00
import PostStatusSelect from './PostStatusSelect';
2019-09-21 12:54:57 +02:00
import PostBoardLabel from '../shared/PostBoardLabel';
2019-09-12 18:03:19 +02:00
import PostStatusLabel from '../shared/PostStatusLabel';
import Comments from '../../containers/Comments';
import { MutedText } from '../shared/CustomTexts';
2019-09-30 16:54:37 +02:00
import { LikesState } from '../../reducers/likesReducer';
2019-10-01 19:15:03 +02:00
import { CommentsState } from '../../reducers/commentsReducer';
import { PostStatusChangesState } from '../../reducers/postStatusChangesReducer';
import friendlyDate, { fromRailsStringToJavascriptDate } from '../../helpers/datetime';
2019-09-12 15:51:45 +02:00
interface Props {
postId: number;
post: IPost;
2019-09-30 16:54:37 +02:00
likes: LikesState;
followed: boolean;
2019-10-01 19:15:03 +02:00
comments: CommentsState;
postStatusChanges: PostStatusChangesState;
2019-09-21 12:54:57 +02:00
boards: Array<IBoard>;
2019-09-12 15:51:45 +02:00
postStatuses: Array<IPostStatus>;
isLoggedIn: boolean;
isPowerUser: boolean;
userFullName: string;
2019-09-30 16:54:37 +02:00
userEmail: string;
2019-09-12 15:51:45 +02:00
authenticityToken: string;
requestPost(postId: number): void;
2019-09-30 16:54:37 +02:00
requestLikes(postId: number): void;
requestFollow(postId: number): void;
requestPostStatusChanges(postId: number): void;
2019-09-21 12:54:57 +02:00
changePostBoard(
postId: number,
newBoardId: number,
authenticityToken: string,
): void;
2019-09-12 15:51:45 +02:00
changePostStatus(
postId: number,
newPostStatusId: number,
userFullName: string,
userEmail: string,
authenticityToken: string,
): void;
submitFollow(
postId: number,
isFollow: boolean,
2019-09-12 15:51:45 +02:00
authenticityToken: string,
): void;
}
class PostP extends React.Component<Props> {
componentDidMount() {
const {postId} = this.props;
this.props.requestPost(postId);
this.props.requestLikes(postId);
this.props.requestFollow(postId);
this.props.requestPostStatusChanges(postId);
2019-09-12 15:51:45 +02:00
}
render() {
const {
post,
2019-09-30 16:54:37 +02:00
likes,
followed,
2019-10-01 19:15:03 +02:00
comments,
postStatusChanges,
2019-09-21 12:54:57 +02:00
boards,
2019-09-12 15:51:45 +02:00
postStatuses,
2019-09-20 18:43:24 +02:00
isLoggedIn,
2019-09-12 15:51:45 +02:00
isPowerUser,
userFullName,
2019-09-30 16:54:37 +02:00
userEmail,
2019-09-12 15:51:45 +02:00
authenticityToken,
2019-09-21 12:54:57 +02:00
changePostBoard,
2019-09-12 15:51:45 +02:00
changePostStatus,
submitFollow,
2019-09-12 15:51:45 +02:00
} = this.props;
const postUpdates = [
...comments.items.filter(comment => comment.isPostUpdate === true),
...postStatusChanges.items,
].sort(
(a, b) =>
fromRailsStringToJavascriptDate(a.updatedAt) < fromRailsStringToJavascriptDate(b.updatedAt) ? 1 : -1
);
2019-09-12 15:51:45 +02:00
return (
<div className="pageContainer">
<div className="sidebar">
2019-10-01 19:15:03 +02:00
<PostUpdateList
postUpdates={postUpdates}
postStatuses={postStatuses}
2019-10-01 19:15:03 +02:00
areLoading={comments.areLoading}
error={comments.error}
/>
2019-09-30 16:54:37 +02:00
<LikeList
likes={likes.items}
areLoading={likes.areLoading}
error={likes.error}
/>
<ActionBox
followed={followed}
submitFollow={() => submitFollow(post.id, !followed, authenticityToken)}
isLoggedIn={isLoggedIn}
/>
</div>
2019-09-12 15:51:45 +02:00
<div className="postAndCommentsContainer">
<div className="postContainer">
2019-09-25 11:50:23 +02:00
<div className="postHeader">
2019-09-30 16:54:37 +02:00
<LikeButton
postId={post.id}
likesCount={likes.items.length}
liked={likes.items.find(like => like.email === userEmail) ? 1 : 0}
isLoggedIn={isLoggedIn}
authenticityToken={authenticityToken}
/>
2019-09-25 11:50:23 +02:00
<h2>{post.title}</h2>
{
isPowerUser && post ?
<a href={`/admin/posts/${post.id}`} data-turbolinks="false">
{I18n.t('post.edit_button')}
</a>
:
null
2019-09-25 11:50:23 +02:00
}
</div>
{
isPowerUser && post ?
2019-09-21 12:54:57 +02:00
<div className="postSettings">
<PostBoardSelect
boards={boards}
selectedBoardId={post.boardId}
handleChange={
newBoardId => changePostBoard(post.id, newBoardId, authenticityToken)
}
/>
<PostStatusSelect
postStatuses={postStatuses}
selectedPostStatusId={post.postStatusId}
handleChange={
newPostStatusId =>
changePostStatus(post.id, newPostStatusId, userFullName, userEmail, authenticityToken)
2019-09-21 12:54:57 +02:00
}
/>
</div>
:
2019-09-21 12:54:57 +02:00
<div className="postInfo">
<PostBoardLabel
{...boards.find(board => board.id === post.boardId)}
/>
<PostStatusLabel
{...postStatuses.find(postStatus => postStatus.id === post.postStatusId)}
/>
</div>
}
<p className="postDescription">{post.description}</p>
<MutedText>{friendlyDate(post.createdAt)}</MutedText>
</div>
<Comments
postId={this.props.postId}
2019-09-20 18:43:24 +02:00
isLoggedIn={isLoggedIn}
2019-09-25 11:50:23 +02:00
isPowerUser={isPowerUser}
2019-10-02 15:26:32 +02:00
userEmail={userEmail}
authenticityToken={authenticityToken}
/>
</div>
2019-09-12 15:51:45 +02:00
</div>
);
}
}
export default PostP;