diff --git a/src/containers/profileContainer.js b/src/containers/profileContainer.js index 158769784..0b856bd71 100644 --- a/src/containers/profileContainer.js +++ b/src/containers/profileContainer.js @@ -20,7 +20,7 @@ import { } from '../providers/hive/dhive'; // Ecency providers -import { getIsFavorite, addFavorite, removeFavorite } from '../providers/ecency/ecency'; +import { checkFavorite, addFavorite, deleteFavorite } from '../providers/ecency/ecency'; // Utilitites import { getRcPower, getVotingPower } from '../utils/manaBar'; @@ -260,45 +260,49 @@ class ProfileContainer extends Component { }; _fetchProfile = async (username = null, isProfileAction = false) => { - const { username: _username, isFollowing, isMuted, isOwnProfile } = this.state; + try { + const { username: _username, isFollowing, isMuted, isOwnProfile } = this.state; - if (username) { - const { currentAccount } = this.props; - let _isFollowing; - let _isMuted; - let isFavorite; - let follows; + if (username) { + const { currentAccount } = this.props; + let _isFollowing; + let _isMuted; + let isFavorite; + let follows; - if (!isOwnProfile) { - const res = await getRelationship(currentAccount.name, username); - _isFollowing = res && res.follows; + if (!isOwnProfile) { + const res = await getRelationship(currentAccount.name, username); + _isFollowing = res && res.follows; + _isMuted = res && res.ignores; + isFavorite = await checkFavorite(username) + } - _isMuted = res && res.ignores; + try { + follows = await getFollows(username); + } catch (err) { + follows = null; + } - getIsFavorite(username, currentAccount.name).then((isFav) => { - isFavorite = isFav; - }); - } - - try { - follows = await getFollows(username); - } catch (err) { - follows = null; - } - - if (isProfileAction && isFollowing === _isFollowing && isMuted === _isMuted) { - this._fetchProfile(_username, true); - } else { - this.setState({ - follows, - isFollowing: _isFollowing, - isMuted: _isMuted, - isFavorite, - isReady: true, - isProfileLoading: false, - }); + if (isProfileAction && isFollowing === _isFollowing && isMuted === _isMuted) { + this._fetchProfile(_username, true); + } else { + this.setState({ + follows, + isFollowing: _isFollowing, + isMuted: _isMuted, + isFavorite, + isReady: true, + isProfileLoading: false, + }); + } } } + catch(error){ + console.warn("Failed to fetch complete profile data", error); + Alert.alert(intl.formatMessage({ + id: 'alert.fail', + }), error.message || error.toString()) + } }; _loadProfile = async (username = null) => { @@ -341,7 +345,7 @@ class ProfileContainer extends Component { }; _handleOnFavoritePress = (isFavorite = false) => { - const { currentAccount, dispatch, intl } = this.props; + const { dispatch, intl } = this.props; const { username } = this.state; let favoriteAction; @@ -350,12 +354,12 @@ class ProfileContainer extends Component { }); if (isFavorite) { - favoriteAction = removeFavorite; + favoriteAction = deleteFavorite; } else { favoriteAction = addFavorite; } - favoriteAction(currentAccount.name, username).then(() => { + favoriteAction(username).then(() => { dispatch( toastNotification( intl.formatMessage({ @@ -364,6 +368,15 @@ class ProfileContainer extends Component { ), ); this.setState({ isFavorite: !isFavorite, isProfileLoading: false }); + }) + .catch((error)=>{ + console.warn("Failed to perform favorite action") + Alert.alert( + intl.formatMessage({ + id: 'alert.fail', + }), + error.message || error.toString() + ) }); }; diff --git a/src/providers/ecency/ecency.ts b/src/providers/ecency/ecency.ts index dfb71bc1c..9209a0a8d 100644 --- a/src/providers/ecency/ecency.ts +++ b/src/providers/ecency/ecency.ts @@ -176,43 +176,70 @@ export const addReport = (url) => */ /** - * @params current username + * Fetches user favourites + * @returns array of favourite accounts */ -export const getFavorites = (username) => - api - .get(`/favorites/${username}`) - .then((resp) => resp.data) - .catch((error) => bugsnag.notify(error)); +export const getFavorites = async () => { + try{ + const response = await ecencyApi.post(`/private-api/favorites`) + return response.data; + } catch(error) { + console.warn("Failed to get favorites", error); + bugsnag.notify(error); + throw error + } +} /** - * @params current username - * @params target username + * Checks if user is precent in current user's favourites + * @params targetUsername username + * @returns boolean */ -export const getIsFavorite = (targetUsername, currentUsername) => - api - .get(`/isfavorite/${currentUsername}/${targetUsername}`) - .then((resp) => resp.data) - .catch((error) => bugsnag.notify(error)); +export const checkFavorite = async (targetUsername:string) => { + try { + const data = { account: targetUsername }; + const response = await ecencyApi.post(`/private-api/favorites-check`, data); + return response.data || false; + } catch(error) { + console.warn("Failed to check favorite", error); + bugsnag.notify(error); + } +} /** - * @params current username + * Adds taget user to current user's favourites * @params target username + * @returns array of user favourites */ -export const addFavorite = (currentUsername, targetUsername) => - api - .post('/favorite', { - username: currentUsername, - account: targetUsername, - }) - .then((resp) => resp.data) - .catch((error) => bugsnag.notify(error)); +export const addFavorite = async (targetUsername:string) => { + try { + const data = { account: targetUsername }; + const response = await ecencyApi.post(`/private-api/favorites-add`, data); + return response.data; + } catch(error) { + console.warn("Failed to add user favorites", error); + bugsnag.notify(error); + throw error + } +} + /** - * @params current username + * Removes taget user to current user's favourites * @params target username + * @returns array of user favourites */ -export const removeFavorite = (currentUsername, targetUsername) => - api.delete(`/favoriteUser/${currentUsername}/${targetUsername}`); +export const deleteFavorite = async (targetUsername:string) => { + try { + const data = { account: targetUsername }; + const response = await ecencyApi.post(`/private-api/favorites-delete`, data); + return response.data; + } catch(error) { + console.warn("Failed to add user favorites", error); + bugsnag.notify(error); + throw error; + } +} /** diff --git a/src/screens/bookmarks/container/bookmarksContainer.js b/src/screens/bookmarks/container/bookmarksContainer.js index 4c8ae58fc..6600115a8 100644 --- a/src/screens/bookmarks/container/bookmarksContainer.js +++ b/src/screens/bookmarks/container/bookmarksContainer.js @@ -6,9 +6,8 @@ import { injectIntl } from 'react-intl'; // Services and Actions import { getFavorites, - removeFavorite, + deleteFavorite, getBookmarks, - removeBookmark, deleteBookmark, } from '../../../providers/ecency/ecency'; @@ -39,7 +38,7 @@ const BookmarksContainer = ({ currentAccount, intl, navigation }) => { const _getFavorites = () => { setIsLoading(true); - getFavorites(currentAccount.name) + getFavorites() .then((data) => { setFavorites(_sortData(data)); setIsLoading(false); @@ -65,7 +64,7 @@ const BookmarksContainer = ({ currentAccount, intl, navigation }) => { }; const _removeFavorite = (selectedUsername) => { - removeFavorite(currentAccount.name, selectedUsername) + deleteFavorite(selectedUsername) .then(() => { const newFavorites = [...favorites].filter((fav) => fav.account !== selectedUsername);