2020-09-06 16:15:25 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { i18n } from '../i18next';
|
2020-09-09 00:48:17 +00:00
|
|
|
import { Post, Comment, SortType, UserDetailsResponse } from 'lemmy-js-client';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { UserDetailsView } from '../interfaces';
|
2020-09-09 00:48:17 +00:00
|
|
|
import { commentsToFlatNodes, setupTippy } from '../utils';
|
2020-09-06 16:15:25 +00:00
|
|
|
import { PostListing } from './post-listing';
|
|
|
|
import { CommentNodes } from './comment-nodes';
|
|
|
|
|
|
|
|
interface UserDetailsProps {
|
2020-09-09 00:48:17 +00:00
|
|
|
userRes: UserDetailsResponse;
|
2020-09-06 16:15:25 +00:00
|
|
|
page: number;
|
|
|
|
limit: number;
|
|
|
|
sort: SortType;
|
|
|
|
enableDownvotes: boolean;
|
|
|
|
enableNsfw: boolean;
|
|
|
|
view: UserDetailsView;
|
|
|
|
onPageChange(page: number): number | any;
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
interface UserDetailsState {}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
export class UserDetails extends Component<UserDetailsProps, UserDetailsState> {
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
// TODO needed here?
|
2020-09-06 16:15:25 +00:00
|
|
|
componentDidMount() {
|
|
|
|
setupTippy();
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:48:17 +00:00
|
|
|
// TODO wut?
|
|
|
|
// componentDidUpdate(lastProps: UserDetailsProps) {
|
|
|
|
// for (const key of Object.keys(lastProps)) {
|
|
|
|
// if (lastProps[key] !== this.props[key]) {
|
|
|
|
// this.fetchUserData();
|
|
|
|
// break;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{this.viewSelector(this.props.view)}
|
|
|
|
{this.paginator()}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
viewSelector(view: UserDetailsView) {
|
|
|
|
if (view === UserDetailsView.Overview || view === UserDetailsView.Saved) {
|
|
|
|
return this.overview();
|
2020-09-09 00:48:17 +00:00
|
|
|
} else if (view === UserDetailsView.Comments) {
|
2020-09-06 16:15:25 +00:00
|
|
|
return this.comments();
|
2020-09-09 00:48:17 +00:00
|
|
|
} else if (view === UserDetailsView.Posts) {
|
2020-09-06 16:15:25 +00:00
|
|
|
return this.posts();
|
2020-09-09 00:48:17 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
overview() {
|
2020-09-09 00:48:17 +00:00
|
|
|
const comments = this.props.userRes.comments.map((c: Comment) => {
|
2020-09-06 16:15:25 +00:00
|
|
|
return { type: 'comments', data: c };
|
|
|
|
});
|
2020-09-09 00:48:17 +00:00
|
|
|
const posts = this.props.userRes.posts.map((p: Post) => {
|
2020-09-06 16:15:25 +00:00
|
|
|
return { type: 'posts', data: p };
|
|
|
|
});
|
|
|
|
|
|
|
|
const combined: { type: string; data: Comment | Post }[] = [
|
|
|
|
...comments,
|
|
|
|
...posts,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Sort it
|
|
|
|
if (this.props.sort === SortType.New) {
|
|
|
|
combined.sort((a, b) => b.data.published.localeCompare(a.data.published));
|
|
|
|
} else {
|
|
|
|
combined.sort((a, b) => b.data.score - a.data.score);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{combined.map(i => (
|
|
|
|
<>
|
|
|
|
<div>
|
|
|
|
{i.type === 'posts' ? (
|
|
|
|
<PostListing
|
2020-09-09 00:48:17 +00:00
|
|
|
communities={[]}
|
2020-09-06 16:15:25 +00:00
|
|
|
key={(i.data as Post).id}
|
|
|
|
post={i.data as Post}
|
2020-09-09 00:48:17 +00:00
|
|
|
admins={this.props.userRes.admins}
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<CommentNodes
|
|
|
|
key={(i.data as Comment).id}
|
|
|
|
nodes={[{ comment: i.data as Comment }]}
|
2020-09-09 00:48:17 +00:00
|
|
|
admins={this.props.userRes.admins}
|
2020-09-06 16:15:25 +00:00
|
|
|
noBorder
|
|
|
|
noIndent
|
|
|
|
showCommunity
|
|
|
|
showContext
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
<hr class="my-3" />
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
comments() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<CommentNodes
|
2020-09-09 00:48:17 +00:00
|
|
|
nodes={commentsToFlatNodes(this.props.userRes.comments)}
|
|
|
|
admins={this.props.userRes.admins}
|
2020-09-06 16:15:25 +00:00
|
|
|
noIndent
|
|
|
|
showCommunity
|
|
|
|
showContext
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
posts() {
|
|
|
|
return (
|
|
|
|
<div>
|
2020-09-09 00:48:17 +00:00
|
|
|
{this.props.userRes.posts.map(post => (
|
2020-09-06 16:15:25 +00:00
|
|
|
<>
|
|
|
|
<PostListing
|
2020-09-09 00:48:17 +00:00
|
|
|
communities={[]}
|
2020-09-06 16:15:25 +00:00
|
|
|
post={post}
|
2020-09-09 00:48:17 +00:00
|
|
|
admins={this.props.userRes.admins}
|
2020-09-06 16:15:25 +00:00
|
|
|
showCommunity
|
|
|
|
enableDownvotes={this.props.enableDownvotes}
|
|
|
|
enableNsfw={this.props.enableNsfw}
|
|
|
|
/>
|
|
|
|
<hr class="my-3" />
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator() {
|
|
|
|
return (
|
|
|
|
<div class="my-2">
|
|
|
|
{this.props.page > 1 && (
|
|
|
|
<button
|
|
|
|
class="btn btn-secondary mr-1"
|
|
|
|
onClick={linkEvent(this, this.prevPage)}
|
|
|
|
>
|
|
|
|
{i18n.t('prev')}
|
|
|
|
</button>
|
|
|
|
)}
|
2020-09-09 00:48:17 +00:00
|
|
|
{this.props.userRes.comments.length + this.props.userRes.posts.length >
|
|
|
|
0 && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.nextPage)}
|
|
|
|
>
|
|
|
|
{i18n.t('next')}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage(i: UserDetails) {
|
|
|
|
i.props.onPageChange(i.props.page + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage(i: UserDetails) {
|
|
|
|
i.props.onPageChange(i.props.page - 1);
|
|
|
|
}
|
|
|
|
}
|