Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Make sure that we have a valid route that we can navigate to.
// If we don't have a valid route then open the url in the browser
const validRoutes = ['episodes', 'chapters', 'reactions', 'franchise'];
if (response && response.tab && !validRoutes.includes(response.tab)) {
Linking.openURL(`${response.scheme}${response.path}`);
return;
}
if (!visibleComponentId || !response.id) return;
let mediaId = response.id;
// Fetch id if it's a slug
if (!isNumeric(response.id)) {
try {
store.dispatch(toggleActivityIndicatorHOC(true));
const media = await Kitsu.findAll(type, {
filter: {
slug: response.id,
},
fields: {
anime: 'id',
manga: 'id',
},
});
mediaId = (media && media.length > 0) ? media[0].id : null;
} catch (e) {
console.log(`Failed to fetch ${type} with slug: ${response.id}`, e);
mediaId = null;
return;
} finally {
store.dispatch(toggleActivityIndicatorHOC(false));
}
const handleUnit = async (response, type) => {
const unitType = type === 'anime' ? 'episodes' : 'chapters';
let media = null;
let unit = null;
// Make sure we have valid inputs
if (!response || !response.id || !isNumeric(response.number)) return;
// Fetch the media first
store.dispatch(toggleActivityIndicatorHOC(true));
try {
// Check if we need to fetch media based on slug
if (!isNumeric(response.id)) {
const results = await Kitsu.findAll(type, {
filter: {
slug: response.id,
},
});
media = results && results.length > 0 && results[0];
} else {
// Just fetch media by the id
media = await Kitsu.find(type, response.id);
}
} catch (e) {
console.log(`Failed to fetch ${type} with id: ${response.id}`, e);
media = null;
}
// Make sure we have a valid media object
if (!media) {
export const fetchMediaReactions = (mediaId, mediaType, limit = 20, pageIndex = 0) => async (
dispatch,
getState,
) => {
dispatch({ type: types.FETCH_MEDIA_REACTIONS });
let data = [];
if (pageIndex > 0) {
data = [...getState().media.reactions[mediaId]];
}
try {
const reactions = await Kitsu.findAll('mediaReactions', {
filter: {
[`${mediaType}Id`]: mediaId,
},
include: 'user',
sort: '-upVotesCount',
});
data = [...data, ...reactions];
dispatch({
type: types.FETCH_MEDIA_REACTIONS_SUCCESS,
payload: {
mediaId,
reactions: data,
},
});
} catch (e) {
// console.log(e);
fetchComments = async () => {
try {
// We go ahead and fetch the comments so if the user wants to view detail
// they can do so without a refetch.
// This will order the comments by newest first.
const comments = await Kitsu.findAll('comments', {
filter: {
postId: this.state.post.id,
parentId: '_none',
},
fields: {
users: 'slug,avatar,name',
},
include: 'user,uploads',
sort: '-createdAt',
});
const processed = preprocessFeedPosts(comments);
const uniqueComments = uniqBy(processed, 'id');
// Store the comments in the right order (oldest - newest)
FeedCache.setComments(this.state.post.id, [...uniqueComments].reverse());
fetchReactions = async () => {
const { userId } = this.props;
this.setState({ loadingReactions: true });
try {
const reactions = await Kitsu.findAll('mediaReactions', {
filter: { userId },
include: 'anime,user,manga',
sort: 'upVotesCount',
});
this.setState({
reactions,
loadingReactions: false,
});
} catch (err) {
console.log('Unhandled error while retrieving reactions: ', err);
this.setState({
loadingReactions: false,
});
}
}
fetchLikes = async () => {
const { currentUser, comment } = this.props;
try {
const likes = await Kitsu.findAll('commentLikes', {
filter: {
commentId: comment.id,
userId: currentUser.id,
},
});
const like = likes.length && likes[0];
if (this.mounted) {
this.setState({ like, isLiked: !!like });
}
} catch (err) {
console.log('Error fetching likes: ', err);
}
}
fetchStats = async () => {
this.setState({ loadingStats: true });
try {
const stats = await Kitsu.findAll('stats', {
filter: {
user_id: this.props.userId,
}
});
this.setState({ stats, loadingStats: false });
} catch (err) {
console.log('Unhandled error while fetching stats:', err);
this.setState({ loadingStats: false });
}
}
export const fetchProfileFavorites = (userId, type = 'anime', limit = 20, pageIndex = 0) => async (
dispatch,
getState,
) => {
dispatch({
type: types.FETCH_USER_FAVORITES,
payload: {
type,
},
});
let data = [];
if (pageIndex > 0) {
data = [...getState().profile[type]];
}
try {
const favorites = await Kitsu.findAll('favorites', {
filter: {
userId,
itemType: capitalize(type),
},
page: {
limit,
offset: pageIndex * limit,
},
fields: {
favorites: 'favRank,id,item',
},
include: 'item',
});
data = [...data, ...favorites];
dispatch({
type: types.FETCH_USER_FAVORITES_SUCCESS,
const { userLibrary, librarySort } = getState().profile;
const { currentUser } = getState().user;
let data = userLibrary[options.userId][options.library][options.status].data;
dispatch({
type: types.FETCH_USER_LIBRARY_TYPE,
library: options.library,
status: options.status,
userId: options.userId,
refresh: options.refresh || false,
});
try {
const libraryEntries = await Kitsu.findAll('libraryEntries', {
fields: {
anime: 'titles,canonicalTitle,posterImage,episodeCount,startDate,endDate',
manga: 'titles,canonicalTitle,posterImage,chapterCount,startDate,endDate',
libraryEntries: 'anime,finishedAt,manga,notes,private,progress,ratingTwenty,reconsumeCount,startedAt,status',
},
filter,
include: 'anime,manga',
page: {
limit: options.limit,
offset: options.refresh ? 0 : data.length,
},
sort: getSortString(librarySort, options.library, currentUser),
});
if (options.refresh) {
data = libraryEntries;
loadMoreImports = async () => {
const { loadingMore, pageLimit, pageIndex, totalImports } = this.state;
const hasMore = totalImports / pageLimit > pageIndex;
if (!loadingMore && hasMore) {
const { accessToken, currentUser } = this.props;
const { id } = currentUser;
setToken(accessToken);
this.setState({ loadingMore: true });
try {
const imports = await Kitsu.findAll('listImports', {
filter: { user_id: id },
page: {
limit: pageLimit,
offset: pageIndex * pageLimit,
},
});
this.setState({
imports: this.state.imports.concat(imports),
pageIndex: pageIndex + 1,
loading: false,
loadingMore: false,
});
} catch (e) {
this.setState({
error: e,
loading: false,