Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
onEditProfile = async (changes) => {
try {
this.setState({ editModalVisible: false, loading: true });
const { userId } = this.props;
const data = await Kitsu.update('users', {
id: userId,
...changes,
}, { include: 'waifu' });
// Bust the cover cache
if (data && data.coverImage) {
const bustedCovers = this.applyCacheBust(data.coverImage);
data.coverImage = bustedCovers;
}
this.setState({ profile: data });
await fetchCurrentUser();
} catch (err) {
console.log('Error updating user:', err);
} finally {
this.setState({ loading: false });
let newPost = null;
try {
if (isEditing) {
// Update the order of images
// This can be done because we don't allow additions/deletions when editing
if (!isEmpty(uploads)) {
await Promise.all(uploads.map((upload, index) => (
Kitsu.update('uploads', {
id: upload.id,
uploadOrder: index + 1,
})
)));
}
// Update post
newPost = await Kitsu.update('posts', {
id: post.id,
content: trimmed,
nsfw,
spoiler,
embedUrl,
}, {
// We need to get the new uploads with their new order
include: 'uploads',
});
// Link post relationships
newPost.user = post.user;
newPost.targetUser = post.targetUser;
newPost.spoiledUnit = post.spoiledUnit;
newPost.media = post.media;
newPost.targetGroup = post.targetGroup;
const changes = {};
// Rating
if (currentUser.ratingSystem !== ratingSystem) {
changes.ratingSystem = ratingSystem;
}
// Media title preferences
if (lowerCase(currentUser.titleLanguagePreference) !== titleLanguagePreference) {
changes.titleLanguagePreference = titleLanguagePreference;
}
// Only update user if we have changes
if (!isEmpty(changes)) {
// Update the rating system
await Kitsu.update('users', { id: currentUser.id, ...changes });
// Fetch the new user object
await fetchCurrentUser();
}
// Update the user library
fetchUserLibrary({ userId: currentUser.id, refresh: true });
}
this.setState({ saving: false });
if (navigation && navigateBackOnSave) navigation.goBack();
};
) => async (dispatch, getState) => {
const { userLibrary } = getState().profile;
const { currentUser } = getState().user;
if (!currentUser || !currentUser.id || !userLibrary[currentUser.id]) return;
const libraryEntries = userLibrary[currentUser.id][libraryType][libraryStatus].data;
const previousLibraryEntry = libraryEntries.find(({ id }) => id === newLibraryEntry.id);
try {
const updateEntry = { ...newLibraryEntry };
// optimistically update state
onLibraryEntryUpdate(currentUser.id, libraryType, libraryStatus, updateEntry)(dispatch, getState);
const record = await Kitsu.update('libraryEntries', updateEntry);
KitsuLibrary.onLibraryEntryUpdate(previousLibraryEntry, record, libraryType, KitsuLibraryEventSource.STORE);
} catch (e) {
throw e;
}
};
rateEntry = async (ratingTwenty) => {
const { library } = this.state;
const entry = library[this.carousel.currentIndex];
const media = getMedia(entry);
try {
this.setLibraryEntryLoading();
const record = await Kitsu.update('libraryEntries', {
id: entry.id,
ratingTwenty,
[media.type]: {
id: media.id,
},
user: {
id: this.props.currentUser.id,
},
}, {
include: this._requestIncludeFields,
});
KitsuLibrary.onLibraryEntryUpdate(entry, record, media.type, KitsuLibraryEventSource.QUICK_UPDATE);
this.updateLibraryEntry(record);
} catch (error) {
console.log('Error rating library entry:', error);
}
async (error, fbdata) => {
if (!error) {
const token = getState().auth.tokens.access_token;
const currentUser = getState().user.currentUser;
setToken(token);
try {
await Kitsu.update('users', { id: currentUser.id, facebookId: fbdata.id });
dispatch({ type: types.CONNECT_FBUSER_SUCCESS, payload: fbdata.id });
} catch (e) {
dispatch({ type: types.CONNECT_FBUSER_FAIL, payload: 'Failed to connect Facebook user' });
console.log(e);
}
} else {
console.log(error);
dispatch({ type: types.CONNECT_FBUSER_FAIL, payload: 'Failed to connect Facebook user' });
}
},
);
addToWatchlist = async () => {
const { currentIndex, topMedia } = this.state;
const { accessToken, userId, type } = this.props;
const libraryEntryId = topMedia[currentIndex].libraryEntryId;
const id = topMedia[currentIndex].id;
setToken(accessToken);
this.setState({ loadingWtW: true });
try {
let response = null;
if (libraryEntryId) {
response = await Kitsu.update('libraryEntries', {
status: 'planned',
id: libraryEntryId,
[type]: {
id,
type,
},
user: {
id: userId,
},
});
} else {
response = await Kitsu.create('libraryEntries', {
status: 'planned',
[type]: {
id,
type,
export const completeOnboarding = () => async (dispatch, getState) => {
dispatch({ type: types.COMPLETE_ONBOARDING });
const { user, auth } = getState();
const { id } = user.currentUser;
const token = auth.tokens.access_token;
setToken(token);
try {
await Kitsu.update('users', { id, status: 'registered' });
dispatch({ type: types.COMPLETE_ONBOARDING_SUCCESS });
NavigationActions.showMainApp();
} catch (e) {
dispatch({ type: types.COMPLETE_ONBOARDING_FAIL, payload: e });
}
};
await Promise.all(uploads.map((upload, index) => (
Kitsu.update('uploads', {
id: upload.id,
uploadOrder: index + 1,
})
)));
}
onSavePrivacySettings = async () => {
const { shareToGlobal } = this.state;
const { accessToken, currentUser } = this.props;
setToken(accessToken);
this.setState({ loading: true });
try {
await Kitsu.update('users', { id: currentUser.id, shareToGlobal });
this.setState({
shareToGlobal,
loading: false,
});
} catch (e) {
this.setState({
error: 'Failed to set privacy settings',
loading: false,
});
}
};