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';
// 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,6 +260,7 @@ class ProfileContainer extends Component {
};
_fetchProfile = async (username = null, isProfileAction = false) => {
try {
const { username: _username, isFollowing, isMuted, isOwnProfile } = this.state;
if (username) {
@ -272,12 +273,8 @@ class ProfileContainer extends Component {
if (!isOwnProfile) {
const res = await getRelationship(currentAccount.name, username);
_isFollowing = res && res.follows;
_isMuted = res && res.ignores;
getIsFavorite(username, currentAccount.name).then((isFav) => {
isFavorite = isFav;
});
isFavorite = await checkFavorite(username)
}
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) => {
@ -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()
)
});
};

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) =>
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;
}
}
/**

View File

@ -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);