migrated favourites api endpoints

This commit is contained in:
Nouman Tahir 2021-07-05 15:05:53 +05:00
parent e98c83cf0f
commit 48b4bec41f
3 changed files with 105 additions and 66 deletions

View File

@ -20,7 +20,7 @@ import {
} from '../providers/hive/dhive'; } from '../providers/hive/dhive';
// Ecency providers // Ecency providers
import { getIsFavorite, addFavorite, removeFavorite } from '../providers/ecency/ecency'; import { checkFavorite, addFavorite, deleteFavorite } from '../providers/ecency/ecency';
// Utilitites // Utilitites
import { getRcPower, getVotingPower } from '../utils/manaBar'; import { getRcPower, getVotingPower } from '../utils/manaBar';
@ -260,6 +260,7 @@ class ProfileContainer extends Component {
}; };
_fetchProfile = async (username = null, isProfileAction = false) => { _fetchProfile = async (username = null, isProfileAction = false) => {
try {
const { username: _username, isFollowing, isMuted, isOwnProfile } = this.state; const { username: _username, isFollowing, isMuted, isOwnProfile } = this.state;
if (username) { if (username) {
@ -272,12 +273,8 @@ class ProfileContainer extends Component {
if (!isOwnProfile) { if (!isOwnProfile) {
const res = await getRelationship(currentAccount.name, username); const res = await getRelationship(currentAccount.name, username);
_isFollowing = res && res.follows; _isFollowing = res && res.follows;
_isMuted = res && res.ignores; _isMuted = res && res.ignores;
isFavorite = await checkFavorite(username)
getIsFavorite(username, currentAccount.name).then((isFav) => {
isFavorite = isFav;
});
} }
try { try {
@ -299,6 +296,13 @@ class ProfileContainer extends Component {
}); });
} }
} }
}
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) => { _loadProfile = async (username = null) => {
@ -341,7 +345,7 @@ class ProfileContainer extends Component {
}; };
_handleOnFavoritePress = (isFavorite = false) => { _handleOnFavoritePress = (isFavorite = false) => {
const { currentAccount, dispatch, intl } = this.props; const { dispatch, intl } = this.props;
const { username } = this.state; const { username } = this.state;
let favoriteAction; let favoriteAction;
@ -350,12 +354,12 @@ class ProfileContainer extends Component {
}); });
if (isFavorite) { if (isFavorite) {
favoriteAction = removeFavorite; favoriteAction = deleteFavorite;
} else { } else {
favoriteAction = addFavorite; favoriteAction = addFavorite;
} }
favoriteAction(currentAccount.name, username).then(() => { favoriteAction(username).then(() => {
dispatch( dispatch(
toastNotification( toastNotification(
intl.formatMessage({ intl.formatMessage({
@ -364,6 +368,15 @@ class ProfileContainer extends Component {
), ),
); );
this.setState({ isFavorite: !isFavorite, isProfileLoading: false }); 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()
)
}); });
}; };

View File

@ -176,43 +176,70 @@ export const addReport = (url) =>
*/ */
/** /**
* @params current username * Fetches user favourites
* @returns array of favourite accounts
*/ */
export const getFavorites = (username) => export const getFavorites = async () => {
api try{
.get(`/favorites/${username}`) const response = await ecencyApi.post(`/private-api/favorites`)
.then((resp) => resp.data) return response.data;
.catch((error) => bugsnag.notify(error)); } catch(error) {
console.warn("Failed to get favorites", error);
bugsnag.notify(error);
throw error
}
}
/** /**
* @params current username * Checks if user is precent in current user's favourites
* @params target username * @params targetUsername username
* @returns boolean
*/ */
export const getIsFavorite = (targetUsername, currentUsername) => export const checkFavorite = async (targetUsername:string) => {
api try {
.get(`/isfavorite/${currentUsername}/${targetUsername}`) const data = { account: targetUsername };
.then((resp) => resp.data) const response = await ecencyApi.post(`/private-api/favorites-check`, data);
.catch((error) => bugsnag.notify(error)); 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 * @params target username
* @returns array of user favourites
*/ */
export const addFavorite = (currentUsername, targetUsername) => export const addFavorite = async (targetUsername:string) => {
api try {
.post('/favorite', { const data = { account: targetUsername };
username: currentUsername, const response = await ecencyApi.post(`/private-api/favorites-add`, data);
account: targetUsername, return response.data;
}) } catch(error) {
.then((resp) => resp.data) console.warn("Failed to add user favorites", error);
.catch((error) => bugsnag.notify(error)); bugsnag.notify(error);
throw error
}
}
/** /**
* @params current username * Removes taget user to current user's favourites
* @params target username * @params target username
* @returns array of user favourites
*/ */
export const removeFavorite = (currentUsername, targetUsername) => export const deleteFavorite = async (targetUsername:string) => {
api.delete(`/favoriteUser/${currentUsername}/${targetUsername}`); 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;
}
}
/** /**

View File

@ -6,9 +6,8 @@ import { injectIntl } from 'react-intl';
// Services and Actions // Services and Actions
import { import {
getFavorites, getFavorites,
removeFavorite, deleteFavorite,
getBookmarks, getBookmarks,
removeBookmark,
deleteBookmark, deleteBookmark,
} from '../../../providers/ecency/ecency'; } from '../../../providers/ecency/ecency';
@ -39,7 +38,7 @@ const BookmarksContainer = ({ currentAccount, intl, navigation }) => {
const _getFavorites = () => { const _getFavorites = () => {
setIsLoading(true); setIsLoading(true);
getFavorites(currentAccount.name) getFavorites()
.then((data) => { .then((data) => {
setFavorites(_sortData(data)); setFavorites(_sortData(data));
setIsLoading(false); setIsLoading(false);
@ -65,7 +64,7 @@ const BookmarksContainer = ({ currentAccount, intl, navigation }) => {
}; };
const _removeFavorite = (selectedUsername) => { const _removeFavorite = (selectedUsername) => {
removeFavorite(currentAccount.name, selectedUsername) deleteFavorite(selectedUsername)
.then(() => { .then(() => {
const newFavorites = [...favorites].filter((fav) => fav.account !== selectedUsername); const newFavorites = [...favorites].filter((fav) => fav.account !== selectedUsername);