1
0
mirror of https://github.com/hyperspacedev/hyperspace synced 2025-02-11 09:10:48 +01:00

Refactor toggle and Mastodon URL methods

This commit is contained in:
Marquis Kurt 2019-12-22 13:36:52 -05:00
parent e5cfaf0a44
commit 40f1c8abd4
No known key found for this signature in database
GPG Key ID: 725636D259F5402D

View File

@ -60,7 +60,7 @@ import ShareMenu from "./PostShareMenu";
import { emojifyString } from "../../utilities/emojis"; import { emojifyString } from "../../utilities/emojis";
import { PollOption } from "../../types/Poll"; import { PollOption } from "../../types/Poll";
const log = (post: Status, msg = '') => { const log = (post: Status, msg = "") => {
let { let {
replies_count, replies_count,
reblogs_count, reblogs_count,
@ -68,7 +68,7 @@ const log = (post: Status, msg = '') => {
favourited, favourited,
reblogged, reblogged,
reblog reblog
} = post } = post;
console.log(msg, { console.log(msg, {
replies_count, replies_count,
reblogs_count, reblogs_count,
@ -76,8 +76,8 @@ const log = (post: Status, msg = '') => {
favourited, favourited,
reblogged, reblogged,
reblog reblog
}) });
} };
interface IPostProps { interface IPostProps {
post: Status; post: Status;
@ -519,131 +519,39 @@ export class Post extends React.Component<any, IPostState> {
} }
} }
/**
* Get the post's URL
* @param post The post to get the URL from
* @returns A string containing the post's URI
*/
getMastodonUrl(post: Status) { getMastodonUrl(post: Status) {
let url = ""; return post.reblog ? post.reblog.uri : post.uri;
if (post.reblog) {
url = post.reblog.uri;
} else {
url = post.uri;
}
return url;
} }
toggleFavorited(post: Status) { /**
let _this = this; * Toggle the status of a post's action and update the state
log(post, 'before un/favorite') * @param type Either the "reblog" or "favorite" action
let { favourites_count, reblog } = post * @param post The post to toggle the status of
if (post.favourited) { */
this.client togglePostStatus(type: "reblog" | "favourite", post: Status) {
.post(`/statuses/${post.id}/unfavourite`) const shouldUndo = post.favourited || post.reblogged;
.then((resp: any) => { let requestBuilder = `/statuses/${post.id}/${
let post: Status = resp.data; shouldUndo ? "un" : ""
if (post.favourites_count === favourites_count) { }${type}`;
post.favourites_count-- this.client
} .post(requestBuilder)
if (post.reblog !== reblog) { .then((resp: any) => {
post.reblog = reblog this.setState({ post: resp.data as Status });
} })
log(post, 'after unfavorite') .catch((err: Error) => {
this.setState({ post }); this.props.enqueueSnackbar(
}) `Couldn't ${shouldUndo ? "un" : ""}${type} post: ${
.catch((err: Error) => { err.name
_this.props.enqueueSnackbar( }`,
`Couldn't unfavorite post: ${err.name}`, { variant: "error" }
{ );
variant: "error" console.error(err.message);
} });
);
console.log(err.message);
});
} else {
this.client
.post(`/statuses/${post.id}/favourite`)
.then((resp: any) => {
let post: Status = resp.data;
if (post.reblog !== reblog) {
post.reblog = reblog
}
if (post.favourites_count === favourites_count) {
post.favourites_count++
}
log(post, 'after favorite')
this.setState({ post });
})
.catch((err: Error) => {
_this.props.enqueueSnackbar(
`Couldn't favorite post: ${err.name}`,
{
variant: "error"
}
);
console.log(err.message);
});
}
}
toggleReblogged(post: Status) {
log(post, 'before un/reblog')
let { reblogs_count, reblogged, favourited, reblog } = post
if (post.reblogged) {
this.client
.post(`/statuses/${post.id}/unreblog`)
.then((resp: any) => {
let post: Status = resp.data;
if (post.reblogs_count === reblogs_count) {
post.reblogs_count--
}
if (post.reblogged === reblogged) {
post.reblogged = !reblogged
}
if (post.favourited !== favourited) {
post.favourited = favourited
}
if (post.reblog === reblog) {
post.reblog = null
}
log(post, 'after unreblog')
this.setState({ post });
})
.catch((err: Error) => {
this.props.enqueueSnackbar(
`Couldn't unboost post: ${err.name}`,
{
variant: "error"
}
);
console.log(err.message);
});
} else {
this.client
.post(`/statuses/${post.id}/reblog`)
.then((resp: any) => {
let post: Status = resp.data;
if (post.reblogs_count === reblogs_count) {
post.reblogs_count++
}
if (post.reblogged === reblogged) {
post.reblogged = !reblogged
}
if (post.favourited !== favourited) {
post.favourited = favourited
}
if (post.reblog === null) {
post.reblog = reblog
}
log(post, 'after reblog')
this.setState({ post });
})
.catch((err: Error) => {
this.props.enqueueSnackbar(
`Couldn't boost post: ${err.name}`,
{
variant: "error"
}
);
console.log(err.message);
});
}
} }
showDeleteDialog() { showDeleteDialog() {
@ -764,7 +672,9 @@ export class Post extends React.Component<any, IPostState> {
</Typography> </Typography>
<Tooltip title="Favorite"> <Tooltip title="Favorite">
<IconButton <IconButton
onClick={() => this.toggleFavorited(post)} onClick={() =>
this.togglePostStatus("favourite", post)
}
> >
<FavoriteIcon <FavoriteIcon
className={ className={
@ -786,7 +696,9 @@ export class Post extends React.Component<any, IPostState> {
</Typography> </Typography>
<Tooltip title="Boost"> <Tooltip title="Boost">
<IconButton <IconButton
onClick={() => this.toggleReblogged(post)} onClick={() =>
this.togglePostStatus("reblog", post)
}
> >
<AutorenewIcon <AutorenewIcon
className={ className={