Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 });
}
} catch (err) {
console.log('Error toggling like: ', err);
const { isLiked, postLikesCount } = this.state;
this.setState({
onImportButtonPressed = async () => {
const { accessToken, currentUser, navigation } = this.props;
const item = navigation.state.params.item;
setToken(accessToken);
const kind = item.title === 'MyAnimeList' ? 'my-anime-list' : 'anilist';
this.setState({ loading: true });
try {
const res = await Kitsu.create('listImports', {
user: { id: currentUser.id },
inputText: this.state.username,
strategy: 'greater',
kind,
});
this.setState({
showModal: true,
loading: false,
errMessage: res.errorMessage,
});
} catch (e) {
// TODO: we may have crashes here.
// console.log('failed import', e);
this.setState({
showModal: true,
errMessage: e && e[0].title,
export const createUser = (data, componentId) => async (dispatch, getState) => {
dispatch({ type: types.CREATE_USER });
const { username, email, password } = data;
const { id, gender } = getState().auth.fbuser;
const userObj = {
name: username,
email,
password,
};
if (id) {
userObj.facebookId = id;
userObj.gender = gender;
}
try {
await Kitsu.create('users', userObj);
loginUser(data, componentId, 'signup')(dispatch, getState);
// TODO: Add user object to redux
dispatch({ type: types.CREATE_USER_SUCCESS, payload: {} });
dispatch({ type: types.CLEAR_FBUSER });
} catch (e) {
dispatch({ type: types.CREATE_USER_FAIL, payload: e });
}
};
onBlockUser = async (user) => {
const { currentUser, accessToken } = this.props;
const { id } = user;
// Don't allow us to block ourselves!
if (currentUser.id == user.id) return;
setToken(accessToken);
Keyboard.dismiss();
this.setState({ loading: true, searchState: {} });
try {
await Kitsu.create('blocks', {
blocked: { id },
user: { id: currentUser.id },
});
this.fetchUserBlocks();
} catch (e) {
this.onError(e);
}
};
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);
const { isLiked, likesCount } = this.state;
this.setState({
createLibraryEntry = async (options) => {
const { mediaId, mediaType } = this.props;
try {
this.setState({ loadingLibrary: true });
const record = await Kitsu.create('libraryEntries', {
...options,
[mediaType]: {
id: mediaId,
type: mediaType
},
user: {
id: this.props.currentUser.id,
type: 'users',
},
}, {
include: 'anime,manga',
});
KitsuLibrary.onLibraryEntryCreate(record, mediaType, KitsuLibraryEventSource.MEDIA_PAGE);
this.setState({ libraryEntry: record, loadingLibrary: false });
} catch (err) {
console.log('Error creating library entry:', err);
createFollow = async (userId) => {
try {
this.setState({ isLoadingFollow: true });
const record = await Kitsu.create('follows', {
follower: {
id: this.props.currentUser.id,
type: 'users',
},
followed: {
id: userId,
type: 'users',
},
});
this.setState({ follow: record, isLoadingFollow: false });
} catch (err) {
console.log('Error creating follow:', err);
}
}
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 });
}
}
onMoreButtonOptionsSelected = async (button) => {
const { currentUser, userId } = this.props;
const { profile } = this.state;
switch (button) {
case 'Block': {
await Kitsu.create('blocks', {
blocked: { id: userId },
user: { id: currentUser.id },
});
break;
}
case 'share': {
const id = (profile && profile.slug) || userId;
if (isNull(id)) return;
const url = `${kitsuConfig.kitsuUrl}/users/${id}`;
const key = Platform.select({ ios: 'url', android: 'message' });
Share.share({ [key]: url });
break;
}
case 'cover': {
if (!profile || !profile.coverImage) return;
const coverURL = profile.coverImage.original ||
onMoreButtonOptionsSelected = async (option) => {
const { mediaId, mediaType, currentUser } = this.props;
const { media } = this.state;
switch (option) {
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;