Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
toggleLike = async () => {
try {
const { currentUser } = this.props;
let { like, isLiked, post, postLikesCount } = this.state;
// Optimistically update our UI
this.setState({
isLiked: !isLiked,
postLikesCount: isLiked ? postLikesCount - 1 : postLikesCount + 1,
});
if (like) {
await Kitsu.destroy('postLikes', like.id);
FeedCache.deleteLike(post.id);
this.setState({ like: null });
} else {
like = await Kitsu.create('postLikes', {
post: {
id: post.id,
type: 'posts',
},
user: {
id: currentUser.id,
type: 'users',
},
});
FeedCache.setLike(post.id, like);
this.setState({ like });
}
handleFollowing = async () => {
const { userId } = this.props;
const isCurrentUser = isIdForCurrentUser(userId, this.props.currentUser);
if (isCurrentUser) { // Edit
this.setState({ editModalVisible: true });
} else if (this.state.follow) { // Destroy
this.setState({ isLoadingFollow: true });
await Kitsu.destroy('follows', this.state.follow.id);
this.setState({ follow: null, isLoadingFollow: false });
} else { // Create
await this.createFollow(userId);
}
}
async handleVote() {
const { reaction, currentUser } = this.props;
const { vote, upVotesCount, hasVoted } = this.state;
try {
if (hasVoted) {
this.setState({ upVotesCount: upVotesCount - 1, hasVoted: false });
await Kitsu.destroy('mediaReactionVotes', vote.id);
this.setState({ vote: null });
} else {
this.setState({ upVotesCount: upVotesCount + 1, hasVoted: true });
const vote = await Kitsu.create('mediaReactionVotes', {
mediaReaction: {
id: reaction.id
},
user: {
id: currentUser.id
}
});
this.setState({ vote });
}
} catch (error) {
console.log('Error handling reaction vote:', error);
this.setState({ upVotesCount, hasVoted });
deletePost = async () => {
try {
const { post } = this.state;
this.setState({ isDeleted: true });
await Kitsu.destroy('posts', post.id);
} catch (err) {
console.log('Error deleting post:', err);
this.setState({ isDeleted: false });
Alert.alert('Sorry', 'There was an issue deleting the post.', [
{ text: 'OK', onPress: null },
]);
}
};
onMainButtonOptionsSelected = async (option) => {
const { libraryEntry } = this.state;
const { mediaType } = this.props;
switch (option) {
case 'current':
case 'planned':
case 'completed':
case 'on_hold':
case 'dropped': {
const data = { status: option };
libraryEntry ? await this.updateLibraryEntry(data) : await this.createLibraryEntry(data);
break;
}
case 'remove':
this.setState({ loadingLibrary: true });
await Kitsu.destroy('libraryEntries', libraryEntry.id);
KitsuLibrary.onLibraryEntryDelete(libraryEntry.id, mediaType, libraryEntry.status, KitsuLibraryEventSource.MEDIA_PAGE);
this.setState({ libraryEntry: null, loadingLibrary: false });
break;
default:
console.log('unhandled option selected:', option);
break;
}
}
toggleLike = async () => {
try {
const { likesCount, isLiked, like } = this.state;
const { comment, currentUser } = this.props;
this.setState({
isLiked: !isLiked,
likesCount: isLiked ? likesCount - 1 : likesCount + 1,
});
if (like) {
await Kitsu.destroy('commentLikes', like.id);
this.setState({ like: null });
} else {
const record = await Kitsu.create('commentLikes', {
comment: {
id: comment.id,
type: 'comments',
},
user: {
id: currentUser.id,
type: 'users',
},
});
this.setState({ like: record });
}
} catch (err) {
console.log('Error toggling like: ', err);
export const deleteUserLibraryEntry = (id, libraryType, libraryStatus) => async (dispatch, getState) => {
const { currentUser } = getState().user;
if (!currentUser || !currentUser.id) return;
try {
await Kitsu.destroy('libraryEntries', id);
dispatch(onLibraryEntryDelete(id, currentUser.id, libraryType, libraryStatus));
KitsuLibrary.onLibraryEntryDelete(id, libraryType, libraryStatus, KitsuLibraryEventSource.STORE);
} catch (e) {
throw e;
}
};
onDisconnectButtonPressed = async () => {
const { accessToken } = this.props;
const { linkedAccount } = this.state;
setToken(accessToken);
this.setState({ loading: true });
try {
await Kitsu.destroy('linkedAccounts', linkedAccount.id);
this.setState({
hasAccount: false,
loading: false,
});
} catch (e) {
this.setState({
error: e,
loading: false,
});
}
}
case 'add': {
const record = await Kitsu.create('favorites', {
item: {
id: mediaId,
type: mediaType,
},
user: {
id: currentUser.id,
type: 'users',
},
});
this.setState({ favorite: record });
break;
}
case 'remove':
await Kitsu.destroy('favorites', this.state.favorite.id);
this.setState({ favorite: null });
break;
case 'share': {
const id = (media && media.slug) || mediaId;
if (isNull(id) || isNull(mediaType)) return;
const url = `${kitsuConfig.kitsuUrl}/${mediaType}/${id}`;
const key = Platform.select({ ios: 'url', android: 'message' });
Share.share({ [key]: url });
break;
}
case 'cover': {
if (!media || !media.coverImage) return;
const coverURL = media.coverImage.original ||
media.coverImage.large ||
media.coverImage.medium ||
media.coverImage.small ||