mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-18 10:52:16 +03:00
migrated favourites api endpoints
This commit is contained in:
parent
e98c83cf0f
commit
48b4bec41f
@ -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,45 +260,49 @@ class ProfileContainer extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
_fetchProfile = async (username = null, isProfileAction = false) => {
|
_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) {
|
if (username) {
|
||||||
const { currentAccount } = this.props;
|
const { currentAccount } = this.props;
|
||||||
let _isFollowing;
|
let _isFollowing;
|
||||||
let _isMuted;
|
let _isMuted;
|
||||||
let isFavorite;
|
let isFavorite;
|
||||||
let follows;
|
let follows;
|
||||||
|
|
||||||
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;
|
||||||
|
isFavorite = await checkFavorite(username)
|
||||||
|
}
|
||||||
|
|
||||||
_isMuted = res && res.ignores;
|
try {
|
||||||
|
follows = await getFollows(username);
|
||||||
|
} catch (err) {
|
||||||
|
follows = null;
|
||||||
|
}
|
||||||
|
|
||||||
getIsFavorite(username, currentAccount.name).then((isFav) => {
|
if (isProfileAction && isFollowing === _isFollowing && isMuted === _isMuted) {
|
||||||
isFavorite = isFav;
|
this._fetchProfile(_username, true);
|
||||||
});
|
} else {
|
||||||
}
|
this.setState({
|
||||||
|
follows,
|
||||||
try {
|
isFollowing: _isFollowing,
|
||||||
follows = await getFollows(username);
|
isMuted: _isMuted,
|
||||||
} catch (err) {
|
isFavorite,
|
||||||
follows = null;
|
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) => {
|
_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()
|
||||||
|
)
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user