X-Git-Url: http://these/git/?a=blobdiff_plain;f=src%2Fshared%2Fcomponents%2Fpost%2Fpost-listing.tsx;h=f1f06c5869f123641f84edb82c449da788170702;hb=2b1af707c3df6126b3e6890106c03c60ad49b1be;hp=7eb289c81d1704399e24002dba78d64997e1118d;hpb=f61037f5d89f12818c8100f907a98b74e980112a;p=lemmy-ui.git diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 7eb289c..f1f06c5 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -11,6 +11,7 @@ import { CreatePostLike, CreatePostReport, DeletePost, + EditPost, FeaturePost, Language, LockPost, @@ -24,8 +25,8 @@ import { } from "lemmy-js-client"; import { getExternalHost, getHttpBase } from "../../env"; import { i18n } from "../../i18next"; -import { BanType, PostFormParams, PurgeType } from "../../interfaces"; -import { UserService, WebSocketService } from "../../services"; +import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces"; +import { UserService } from "../../services"; import { amAdmin, amCommunityCreator, @@ -43,13 +44,13 @@ import { mdNoImages, mdToHtml, mdToHtmlInline, - myAuth, + myAuthRequired, + newVote, numToSI, relTags, setupTippy, share, showScores, - wsClient, } from "../../utils"; import { Icon, PurgeWarning, Spinner } from "../common/icon"; import { MomentTime } from "../common/moment-time"; @@ -81,15 +82,25 @@ interface PostListingState { showBody: boolean; showReportDialog: boolean; reportReason?: string; - my_vote?: number; - score: number; - upvotes: number; - downvotes: number; + upvoteLoading: boolean; + downvoteLoading: boolean; + reportLoading: boolean; + blockLoading: boolean; + lockLoading: boolean; + deleteLoading: boolean; + removeLoading: boolean; + saveLoading: boolean; + featureCommunityLoading: boolean; + featureLocalLoading: boolean; + banLoading: boolean; + addModLoading: boolean; + addAdminLoading: boolean; + transferLoading: boolean; } interface PostListingProps { post_view: PostView; - duplicates?: PostView[]; + crossPosts?: PostView[]; moderators?: CommunityModeratorView[]; admins?: PersonView[]; allLanguages: Language[]; @@ -100,6 +111,22 @@ interface PostListingProps { enableDownvotes?: boolean; enableNsfw?: boolean; viewOnly?: boolean; + onPostEdit(form: EditPost): void; + onPostVote(form: CreatePostLike): void; + onPostReport(form: CreatePostReport): void; + onBlockPerson(form: BlockPerson): void; + onLockPost(form: LockPost): void; + onDeletePost(form: DeletePost): void; + onRemovePost(form: RemovePost): void; + onSavePost(form: SavePost): void; + onFeaturePost(form: FeaturePost): void; + onPurgePerson(form: PurgePerson): void; + onPurgePost(form: PurgePost): void; + onBanPersonFromCommunity(form: BanFromCommunity): void; + onBanPerson(form: BanPerson): void; + onAddModToCommunity(form: AddModToCommunity): void; + onAddAdmin(form: AddAdmin): void; + onTransferCommunity(form: TransferCommunity): void; } export class PostListing extends Component { @@ -108,7 +135,6 @@ export class PostListing extends Component { showRemoveDialog: false, showPurgeDialog: false, purgeType: PurgeType.Person, - purgeLoading: false, showBanDialog: false, banType: BanType.Community, removeData: false, @@ -120,35 +146,59 @@ export class PostListing extends Component { showMoreMobile: false, showBody: false, showReportDialog: false, - my_vote: this.props.post_view.my_vote, - score: this.props.post_view.counts.score, - upvotes: this.props.post_view.counts.upvotes, - downvotes: this.props.post_view.counts.downvotes, + upvoteLoading: false, + downvoteLoading: false, + purgeLoading: false, + reportLoading: false, + blockLoading: false, + lockLoading: false, + deleteLoading: false, + removeLoading: false, + saveLoading: false, + featureCommunityLoading: false, + featureLocalLoading: false, + banLoading: false, + addModLoading: false, + addAdminLoading: false, + transferLoading: false, }; constructor(props: any, context: any) { super(props, context); - this.handlePostLike = this.handlePostLike.bind(this); - this.handlePostDisLike = this.handlePostDisLike.bind(this); this.handleEditPost = this.handleEditPost.bind(this); this.handleEditCancel = this.handleEditCancel.bind(this); } componentWillReceiveProps(nextProps: PostListingProps) { - this.setState({ - my_vote: nextProps.post_view.my_vote, - upvotes: nextProps.post_view.counts.upvotes, - downvotes: nextProps.post_view.counts.downvotes, - score: nextProps.post_view.counts.score, - }); - if (this.props.post_view.post.id !== nextProps.post_view.post.id) { - this.setState({ imageExpanded: false }); + if (this.props !== nextProps) { + this.setState({ + upvoteLoading: false, + downvoteLoading: false, + purgeLoading: false, + reportLoading: false, + blockLoading: false, + lockLoading: false, + deleteLoading: false, + removeLoading: false, + saveLoading: false, + featureCommunityLoading: false, + featureLocalLoading: false, + banLoading: false, + addModLoading: false, + addAdminLoading: false, + transferLoading: false, + imageExpanded: false, + }); } } + get postView(): PostView { + return this.props.post_view; + } + render() { - const post = this.props.post_view.post; + const post = this.postView.post; return (
@@ -156,30 +206,29 @@ export class PostListing extends Component { <> {this.listing()} {this.state.imageExpanded && !this.props.hideImage && this.img} - {post.url && this.showBody && post.embed_title && ( + {post.url && this.state.showBody && post.embed_title && ( )} {this.showBody && this.body()} ) : ( -
- -
+ )}
); } body() { - const body = this.props.post_view.post.body; + const body = this.postView.post.body; return body ? (
{this.state.viewSource ? ( @@ -194,12 +243,11 @@ export class PostListing extends Component { } get img() { - const src = this.imageSrc; - return src ? ( + return this.imageSrc ? ( <>
@@ -207,7 +255,7 @@ export class PostListing extends Component { className="d-inline-block" onClick={linkEvent(this, this.handleImageExpandClick)} > - +
@@ -217,7 +265,7 @@ export class PostListing extends Component { } imgThumb(src: string) { - const post_view = this.props.post_view; + const post_view = this.postView; return ( { } get imageSrc(): string | undefined { - const post = this.props.post_view.post; + const post = this.postView.post; const url = post.url; const thumbnail = post.thumbnail_url; @@ -249,7 +297,7 @@ export class PostListing extends Component { } thumbnail() { - const post = this.props.post_view.post; + const post = this.postView.post; const url = post.url; const thumbnail = post.thumbnail_url; @@ -318,7 +366,7 @@ export class PostListing extends Component { } createdLine() { - const post_view = this.props.post_view; + const post_view = this.postView; const url = post_view.post.url; const body = post_view.post.body; return ( @@ -401,21 +449,25 @@ export class PostListing extends Component {
{showScores() ? (
- {numToSI(this.state.score)} + {numToSI(this.postView.counts.score)}
) : (
@@ -423,14 +475,18 @@ export class PostListing extends Component { {this.props.enableDownvotes && ( )}
@@ -438,7 +494,7 @@ export class PostListing extends Component { } get postLink() { - const post = this.props.post_view.post; + const post = this.postView.post; return ( { } postTitleLine() { - const post = this.props.post_view.post; + const post = this.postView.post; const url = post.url; return ( @@ -550,7 +606,7 @@ export class PostListing extends Component { } duplicatesLine() { - const dupes = this.props.duplicates; + const dupes = this.props.crossPosts; return dupes && dupes.length > 0 ? (
    <> @@ -572,7 +628,7 @@ export class PostListing extends Component { } commentsLine(mobile = false) { - const post = this.props.post_view.post; + const post = this.postView.post; return (
    @@ -606,7 +662,7 @@ export class PostListing extends Component { postActions(mobile = false) { // Possible enhancement: Priority+ pattern instead of just hard coding which get hidden behind the show more button. // Possible enhancement: Make each button a component. - const post_view = this.props.post_view; + const post_view = this.postView; return ( <> {this.saveButton} @@ -647,7 +703,7 @@ export class PostListing extends Component { } get commentsButton() { - const post_view = this.props.post_view; + const post_view = this.postView; return ( { } get unreadCount(): number | undefined { - const pv = this.props.post_view; + const pv = this.postView; return pv.unread_comments == pv.counts.comments || pv.unread_comments == 0 ? undefined : pv.unread_comments; @@ -690,37 +746,51 @@ export class PostListing extends Component {
    {this.props.enableDownvotes && ( )} @@ -730,7 +800,7 @@ export class PostListing extends Component { } get saveButton() { - const saved = this.props.post_view.saved; + const saved = this.postView.saved; const label = saved ? i18n.t("unsave") : i18n.t("save"); return ( ); } @@ -784,11 +858,11 @@ export class PostListing extends Component { return ( ); } @@ -807,7 +881,7 @@ export class PostListing extends Component { } get deleteButton() { - const deleted = this.props.post_view.post.deleted; + const deleted = this.postView.post.deleted; const label = !deleted ? i18n.t("delete") : i18n.t("restore"); return ( ); } @@ -856,7 +934,7 @@ export class PostListing extends Component { } get lockButton() { - const locked = this.props.post_view.post.locked; + const locked = this.postView.post.locked; const label = locked ? i18n.t("unlock") : i18n.t("lock"); return ( ); } get featureButton() { - const featuredCommunity = this.props.post_view.post.featured_community; + const featuredCommunity = this.postView.post.featured_community; const labelCommunity = featuredCommunity ? i18n.t("unfeature_from_community") : i18n.t("feature_in_community"); - const featuredLocal = this.props.post_view.post.featured_local; + const featuredLocal = this.postView.post.featured_local; const labelLocal = featuredLocal ? i18n.t("unfeature_from_local") : i18n.t("feature_in_local"); @@ -892,12 +974,18 @@ export class PostListing extends Component { data-tippy-content={labelCommunity} aria-label={labelCommunity} > - {" "} - Community + {this.state.featureCommunityLoading ? ( + + ) : ( + + + {i18n.t("community")} + + )} {amAdmin() && ( )} @@ -919,7 +1013,7 @@ export class PostListing extends Component { } get modRemoveButton() { - const removed = this.props.post_view.post.removed; + const removed = this.postView.post.removed; return ( ); } @@ -939,7 +1039,7 @@ export class PostListing extends Component { */ userActionsLine() { // TODO: make nicer - const post_view = this.props.post_view; + const post_view = this.postView; return ( this.state.showAdvanced && ( <> @@ -966,7 +1066,7 @@ export class PostListing extends Component { )} aria-label={i18n.t("unban")} > - {i18n.t("unban")} + {this.state.banLoading ? : i18n.t("unban")} ))} {!post_view.creator_banned_from_community && ( @@ -979,9 +1079,13 @@ export class PostListing extends Component { : i18n.t("appoint_as_mod") } > - {this.creatorIsMod_ - ? i18n.t("remove_as_mod") - : i18n.t("appoint_as_mod")} + {this.state.addModLoading ? ( + + ) : this.creatorIsMod_ ? ( + i18n.t("remove_as_mod") + ) : ( + i18n.t("appoint_as_mod") + )} )} @@ -1014,7 +1118,7 @@ export class PostListing extends Component { aria-label={i18n.t("yes")} onClick={linkEvent(this, this.handleTransferCommunity)} > - {i18n.t("yes")} + {this.state.transferLoading ? : i18n.t("yes")} )} )} @@ -1089,7 +1201,7 @@ export class PostListing extends Component { } removeAndBanDialogs() { - const post = this.props.post_view; + const post = this.postView; const purgeTypeText = this.state.purgeType == PurgeType.Post ? i18n.t("purge_post") @@ -1117,7 +1229,7 @@ export class PostListing extends Component { className="btn btn-secondary" aria-label={i18n.t("remove_post")} > - {i18n.t("remove_post")} + {this.state.removeLoading ? : i18n.t("remove_post")} )} @@ -1179,7 +1291,13 @@ export class PostListing extends Component { className="btn btn-secondary" aria-label={i18n.t("ban")} > - {i18n.t("ban")} {post.creator.name} + {this.state.banLoading ? ( + + ) : ( + + {i18n.t("ban")} {post.creator.name} + + )}
    @@ -1206,7 +1324,7 @@ export class PostListing extends Component { className="btn btn-secondary" aria-label={i18n.t("create_report")} > - {i18n.t("create_report")} + {this.state.reportLoading ? : i18n.t("create_report")} )} @@ -1235,7 +1353,7 @@ export class PostListing extends Component { className="btn btn-secondary" aria-label={purgeTypeText} > - {purgeTypeText} + {this.state.purgeLoading ? : { purgeTypeText }} )} @@ -1245,7 +1363,7 @@ export class PostListing extends Component { } mobileThumbnail() { - const post = this.props.post_view.post; + const post = this.postView.post; return post.thumbnail_url || (post.url && isImage(post.url)) ? (
    @@ -1262,7 +1380,7 @@ export class PostListing extends Component { } showMobilePreview() { - const body = this.props.post_view.post.body; + const body = this.postView.post.body; return !this.showBody && body ? (
    {body}
    ) : ( @@ -1320,97 +1438,10 @@ export class PostListing extends Component { private get myPost(): boolean { return ( - this.props.post_view.creator.id == + this.postView.creator.id == UserService.Instance.myUserInfo?.local_user_view.person.id ); } - - handlePostLike(event: any) { - event.preventDefault(); - if (!UserService.Instance.myUserInfo) { - this.context.router.history.push(`/login`); - } - - const myVote = this.state.my_vote; - const newVote = myVote == 1 ? 0 : 1; - - if (myVote == 1) { - this.setState({ - score: this.state.score - 1, - upvotes: this.state.upvotes - 1, - }); - } else if (myVote == -1) { - this.setState({ - score: this.state.score + 2, - upvotes: this.state.upvotes + 1, - downvotes: this.state.downvotes - 1, - }); - } else { - this.setState({ - score: this.state.score + 1, - upvotes: this.state.upvotes + 1, - }); - } - - this.setState({ my_vote: newVote }); - - const auth = myAuth(); - if (auth) { - const form: CreatePostLike = { - post_id: this.props.post_view.post.id, - score: newVote, - auth, - }; - - WebSocketService.Instance.send(wsClient.likePost(form)); - this.setState(this.state); - } - setupTippy(); - } - - handlePostDisLike(event: any) { - event.preventDefault(); - if (!UserService.Instance.myUserInfo) { - this.context.router.history.push(`/login`); - } - - const myVote = this.state.my_vote; - const newVote = myVote == -1 ? 0 : -1; - - if (myVote == 1) { - this.setState({ - score: this.state.score - 2, - upvotes: this.state.upvotes - 1, - downvotes: this.state.downvotes + 1, - }); - } else if (myVote == -1) { - this.setState({ - score: this.state.score + 1, - downvotes: this.state.downvotes - 1, - }); - } else { - this.setState({ - score: this.state.score - 1, - downvotes: this.state.downvotes + 1, - }); - } - - this.setState({ my_vote: newVote }); - - const auth = myAuth(); - if (auth) { - const form: CreatePostLike = { - post_id: this.props.post_view.post.id, - score: newVote, - auth, - }; - - WebSocketService.Instance.send(wsClient.likePost(form)); - this.setState(this.state); - } - setupTippy(); - } - handleEditClick(i: PostListing) { i.setState({ showEdit: true }); } @@ -1420,8 +1451,9 @@ export class PostListing extends Component { } // The actual editing is done in the receive for post - handleEditPost() { + handleEditPost(form: EditPost) { this.setState({ showEdit: false }); + this.props.onPostEdit(form); } handleShare(i: PostListing) { @@ -1443,61 +1475,44 @@ export class PostListing extends Component { handleReportSubmit(i: PostListing, event: any) { event.preventDefault(); - const auth = myAuth(); - const reason = i.state.reportReason; - if (auth && reason) { - const form: CreatePostReport = { - post_id: i.props.post_view.post.id, - reason, - auth, - }; - WebSocketService.Instance.send(wsClient.createPostReport(form)); - - i.setState({ showReportDialog: false }); - } + i.setState({ reportLoading: true }); + i.props.onPostReport({ + post_id: i.postView.post.id, + reason: i.state.reportReason ?? "", + auth: myAuthRequired(), + }); } - handleBlockUserClick(i: PostListing) { - const auth = myAuth(); - if (auth) { - const blockUserForm: BlockPerson = { - person_id: i.props.post_view.creator.id, - block: true, - auth, - }; - WebSocketService.Instance.send(wsClient.blockPerson(blockUserForm)); - } + handleBlockPersonClick(i: PostListing) { + i.setState({ blockLoading: true }); + i.props.onBlockPerson({ + person_id: i.postView.creator.id, + block: true, + auth: myAuthRequired(), + }); } handleDeleteClick(i: PostListing) { - const auth = myAuth(); - if (auth) { - const deleteForm: DeletePost = { - post_id: i.props.post_view.post.id, - deleted: !i.props.post_view.post.deleted, - auth, - }; - WebSocketService.Instance.send(wsClient.deletePost(deleteForm)); - } + i.setState({ deleteLoading: true }); + i.props.onDeletePost({ + post_id: i.postView.post.id, + deleted: !i.postView.post.deleted, + auth: myAuthRequired(), + }); } handleSavePostClick(i: PostListing) { - const auth = myAuth(); - if (auth) { - const saved = - i.props.post_view.saved == undefined ? true : !i.props.post_view.saved; - const form: SavePost = { - post_id: i.props.post_view.post.id, - save: saved, - auth, - }; - WebSocketService.Instance.send(wsClient.savePost(form)); - } + i.setState({ saveLoading: true }); + i.props.onSavePost({ + post_id: i.postView.post.id, + save: !i.postView.saved, + auth: myAuthRequired(), + }); } get crossPostParams(): PostFormParams { const queryParams: PostFormParams = {}; - const { name, url } = this.props.post_view.post; + const { name, url } = this.postView.post; queryParams.name = name; @@ -1514,7 +1529,7 @@ export class PostListing extends Component { } crossPostBody(): string | undefined { - const post = this.props.post_view.post; + const post = this.postView.post; const body = post.body; return body @@ -1546,56 +1561,41 @@ export class PostListing extends Component { handleModRemoveSubmit(i: PostListing, event: any) { event.preventDefault(); - - const auth = myAuth(); - if (auth) { - const form: RemovePost = { - post_id: i.props.post_view.post.id, - removed: !i.props.post_view.post.removed, - reason: i.state.removeReason, - auth, - }; - WebSocketService.Instance.send(wsClient.removePost(form)); - i.setState({ showRemoveDialog: false }); - } + i.setState({ removeLoading: true }); + i.props.onRemovePost({ + post_id: i.postView.post.id, + removed: !i.postView.post.removed, + auth: myAuthRequired(), + }); } handleModLock(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: LockPost = { - post_id: i.props.post_view.post.id, - locked: !i.props.post_view.post.locked, - auth, - }; - WebSocketService.Instance.send(wsClient.lockPost(form)); - } + i.setState({ lockLoading: true }); + i.props.onLockPost({ + post_id: i.postView.post.id, + locked: !i.postView.post.locked, + auth: myAuthRequired(), + }); } handleModFeaturePostLocal(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: FeaturePost = { - post_id: i.props.post_view.post.id, - feature_type: "Local", - featured: !i.props.post_view.post.featured_local, - auth, - }; - WebSocketService.Instance.send(wsClient.featurePost(form)); - } + i.setState({ featureLocalLoading: true }); + i.props.onFeaturePost({ + post_id: i.postView.post.id, + featured: !i.postView.post.featured_local, + feature_type: "Local", + auth: myAuthRequired(), + }); } handleModFeaturePostCommunity(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: FeaturePost = { - post_id: i.props.post_view.post.id, - feature_type: "Community", - featured: !i.props.post_view.post.featured_community, - auth, - }; - WebSocketService.Instance.send(wsClient.featurePost(form)); - } + i.setState({ featureCommunityLoading: true }); + i.props.onFeaturePost({ + post_id: i.postView.post.id, + featured: !i.postView.post.featured_community, + feature_type: "Community", + auth: myAuthRequired(), + }); } handleModBanFromCommunityShow(i: PostListing) { @@ -1636,26 +1636,19 @@ export class PostListing extends Component { handlePurgeSubmit(i: PostListing, event: any) { event.preventDefault(); - - const auth = myAuth(); - if (auth) { - if (i.state.purgeType == PurgeType.Person) { - const form: PurgePerson = { - person_id: i.props.post_view.creator.id, - reason: i.state.purgeReason, - auth, - }; - WebSocketService.Instance.send(wsClient.purgePerson(form)); - } else if (i.state.purgeType == PurgeType.Post) { - const form: PurgePost = { - post_id: i.props.post_view.post.id, - reason: i.state.purgeReason, - auth, - }; - WebSocketService.Instance.send(wsClient.purgePost(form)); - } - - i.setState({ purgeLoading: true }); + i.setState({ purgeLoading: true }); + if (i.state.purgeType == PurgeType.Person) { + i.props.onPurgePerson({ + person_id: i.postView.creator.id, + reason: i.state.purgeReason, + auth: myAuthRequired(), + }); + } else if (i.state.purgeType == PurgeType.Post) { + i.props.onPurgePost({ + post_id: i.postView.post.id, + reason: i.state.purgeReason, + auth: myAuthRequired(), + }); } } @@ -1667,88 +1660,70 @@ export class PostListing extends Component { i.setState({ banExpireDays: event.target.value }); } - handleModBanFromCommunitySubmit(i: PostListing) { + handleModBanFromCommunitySubmit(i: PostListing, event: any) { i.setState({ banType: BanType.Community }); - i.handleModBanBothSubmit(i); + i.handleModBanBothSubmit(i, event); } - handleModBanSubmit(i: PostListing) { + handleModBanSubmit(i: PostListing, event: any) { i.setState({ banType: BanType.Site }); - i.handleModBanBothSubmit(i); - } - - handleModBanBothSubmit(i: PostListing, event?: any) { - if (event) event.preventDefault(); - const auth = myAuth(); - if (auth) { - const ban = !i.props.post_view.creator_banned_from_community; - const person_id = i.props.post_view.creator.id; - const remove_data = i.state.removeData; - const reason = i.state.banReason; - const expires = futureDaysToUnixTime(i.state.banExpireDays); - - if (i.state.banType == BanType.Community) { - // If its an unban, restore all their data - if (ban == false) { - i.setState({ removeData: false }); - } - - const form: BanFromCommunity = { - person_id, - community_id: i.props.post_view.community.id, - ban, - remove_data, - reason, - expires, - auth, - }; - WebSocketService.Instance.send(wsClient.banFromCommunity(form)); - } else { - // If its an unban, restore all their data - const ban = !i.props.post_view.creator.banned; - if (ban == false) { - i.setState({ removeData: false }); - } - const form: BanPerson = { - person_id, - ban, - remove_data, - reason, - expires, - auth, - }; - WebSocketService.Instance.send(wsClient.banPerson(form)); - } + i.handleModBanBothSubmit(i, event); + } + + handleModBanBothSubmit(i: PostListing, event: any) { + event.preventDefault(); + i.setState({ banLoading: true }); - i.setState({ showBanDialog: false }); + const ban = !i.props.post_view.creator_banned_from_community; + // If its an unban, restore all their data + if (ban == false) { + i.setState({ removeData: false }); + } + const person_id = i.props.post_view.creator.id; + const remove_data = i.state.removeData; + const reason = i.state.banReason; + const expires = futureDaysToUnixTime(i.state.banExpireDays); + + if (i.state.banType == BanType.Community) { + const community_id = i.postView.community.id; + i.props.onBanPersonFromCommunity({ + community_id, + person_id, + ban, + remove_data, + reason, + expires, + auth: myAuthRequired(), + }); + } else { + i.props.onBanPerson({ + person_id, + ban, + remove_data, + reason, + expires, + auth: myAuthRequired(), + }); } } handleAddModToCommunity(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: AddModToCommunity = { - person_id: i.props.post_view.creator.id, - community_id: i.props.post_view.community.id, - added: !i.creatorIsMod_, - auth, - }; - WebSocketService.Instance.send(wsClient.addModToCommunity(form)); - i.setState(i.state); - } + i.setState({ addModLoading: true }); + i.props.onAddModToCommunity({ + community_id: i.postView.community.id, + person_id: i.postView.creator.id, + added: !i.creatorIsMod_, + auth: myAuthRequired(), + }); } handleAddAdmin(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: AddAdmin = { - person_id: i.props.post_view.creator.id, - added: !i.creatorIsAdmin_, - auth, - }; - WebSocketService.Instance.send(wsClient.addAdmin(form)); - i.setState(i.state); - } + i.setState({ addAdminLoading: true }); + i.props.onAddAdmin({ + person_id: i.postView.creator.id, + added: !i.creatorIsAdmin_, + auth: myAuthRequired(), + }); } handleShowConfirmTransferCommunity(i: PostListing) { @@ -1760,16 +1735,12 @@ export class PostListing extends Component { } handleTransferCommunity(i: PostListing) { - const auth = myAuth(); - if (auth) { - const form: TransferCommunity = { - community_id: i.props.post_view.community.id, - person_id: i.props.post_view.creator.id, - auth, - }; - WebSocketService.Instance.send(wsClient.transferCommunity(form)); - i.setState({ showConfirmTransferCommunity: false }); - } + i.setState({ transferLoading: true }); + i.props.onTransferCommunity({ + community_id: i.postView.community.id, + person_id: i.postView.creator.id, + auth: myAuthRequired(), + }); } handleShowConfirmTransferSite(i: PostListing) { @@ -1808,20 +1779,38 @@ export class PostListing extends Component { setupTippy(); } + handleUpvote(i: PostListing) { + i.setState({ upvoteLoading: true }); + i.props.onPostVote({ + post_id: i.postView.post.id, + score: newVote(VoteType.Upvote, i.props.post_view.my_vote), + auth: myAuthRequired(), + }); + } + + handleDownvote(i: PostListing) { + i.setState({ downvoteLoading: true }); + i.props.onPostVote({ + post_id: i.postView.post.id, + score: newVote(VoteType.Downvote, i.props.post_view.my_vote), + auth: myAuthRequired(), + }); + } + get pointsTippy(): string { const points = i18n.t("number_of_points", { - count: Number(this.state.score), - formattedCount: Number(this.state.score), + count: Number(this.postView.counts.score), + formattedCount: Number(this.postView.counts.score), }); const upvotes = i18n.t("number_of_upvotes", { - count: Number(this.state.upvotes), - formattedCount: Number(this.state.upvotes), + count: Number(this.postView.counts.upvotes), + formattedCount: Number(this.postView.counts.upvotes), }); const downvotes = i18n.t("number_of_downvotes", { - count: Number(this.state.downvotes), - formattedCount: Number(this.state.downvotes), + count: Number(this.postView.counts.downvotes), + formattedCount: Number(this.postView.counts.downvotes), }); return `${points} • ${upvotes} • ${downvotes}`; @@ -1829,7 +1818,7 @@ export class PostListing extends Component { get canModOnSelf_(): boolean { return canMod( - this.props.post_view.creator.id, + this.postView.creator.id, this.props.moderators, this.props.admins, undefined, @@ -1839,21 +1828,21 @@ export class PostListing extends Component { get canMod_(): boolean { return canMod( - this.props.post_view.creator.id, + this.postView.creator.id, this.props.moderators, this.props.admins ); } get canAdmin_(): boolean { - return canAdmin(this.props.post_view.creator.id, this.props.admins); + return canAdmin(this.postView.creator.id, this.props.admins); } get creatorIsMod_(): boolean { - return isMod(this.props.post_view.creator.id, this.props.moderators); + return isMod(this.postView.creator.id, this.props.moderators); } get creatorIsAdmin_(): boolean { - return isAdmin(this.props.post_view.creator.id, this.props.admins); + return isAdmin(this.postView.creator.id, this.props.admins); } }