fixed data fetch from back button && created remove and add

This commit is contained in:
u-e 2019-01-11 17:17:53 +03:00
parent 7952922e46
commit 7b2565344e
6 changed files with 85 additions and 33 deletions

View File

@ -34,7 +34,9 @@ class HeaderContainer extends PureComponent {
};
_handleOnPressBackButton = () => {
const { navigation } = this.props;
const { navigation, handleOnBackPress } = this.props;
if (handleOnBackPress) handleOnBackPress();
navigation.goBack();
};

View File

@ -65,6 +65,7 @@ class ProfileSummaryView extends PureComponent {
followerCount,
followingCount,
handleFollowUnfollowUser,
handleOnFavoritePress,
handleOnFollowsPress,
handleUIChange,
hoursRC,
@ -190,6 +191,7 @@ class ProfileSummaryView extends PureComponent {
name={isFavorite ? 'favorite' : 'favorite-border'}
size={20}
style={[styles.insetIconStyle]}
onPress={() => handleOnFavoritePress(isFavorite)}
/>
{isProfileLoading ? (
<ActivityIndicator style={styles.activityIndicator} />

View File

@ -109,7 +109,7 @@ export const getIsFavorite = (targetUsername, currentUsername) => api.get(`/isfa
export const addFavorite = (currentUsername, targetUsername) => api
.post('/favorite', {
username: currentUsername,
targetUsername,
account: targetUsername,
})
.then(resp => resp.data);
@ -117,7 +117,7 @@ export const addFavorite = (currentUsername, targetUsername) => api
* @params current username
* @params target username
*/
export const removeFavoriteUser = (currentUsername, targetUsername) => api.delete(`/favoriteUser/${currentUsername}/${targetUsername}`);
export const removeFavorite = (currentUsername, targetUsername) => api.delete(`/favoriteUser/${currentUsername}/${targetUsername}`);
export const getLeaderboard = () => api.get('/leaderboard').then(resp => resp.data);

View File

@ -4,7 +4,7 @@ import { Alert } from 'react-native';
import { injectIntl } from 'react-intl';
// Services and Actions
import { getFavorites, removeFavoriteUser } from '../../../providers/esteem/esteem';
import { getFavorites, removeFavorite } from '../../../providers/esteem/esteem';
// Constants
import ROUTES from '../../../constants/routeNames';
@ -32,10 +32,15 @@ class DraftsContainer extends Component {
// Component Life Cycle Functions
componentDidMount() {
this._getFavorites();
this._fetchData();
}
// Component Functions
_fetchData = () => {
this._getFavorites();
};
_getFavorites = () => {
const { currentAccount, intl } = this.props;
this.setState({ isLoading: true });
@ -53,7 +58,7 @@ class DraftsContainer extends Component {
_removeFavorite = (selectedUsername) => {
const { currentAccount, intl } = this.props;
removeFavoriteUser(currentAccount.name, selectedUsername)
removeFavorite(currentAccount.name, selectedUsername)
.then(() => {
const { favorites } = this.state;
const newFavorites = [...favorites].filter(fav => fav.account !== selectedUsername);
@ -72,6 +77,7 @@ class DraftsContainer extends Component {
routeName: ROUTES.SCREENS.PROFILE,
params: {
username,
fetchData: this._fetchData,
},
});
};

View File

@ -15,7 +15,7 @@ import {
} from '../../../providers/steem/dsteem';
// Esteem providers
import { getIsFavorite } from '../../../providers/esteem/esteem';
import { getIsFavorite, addFavorite, removeFavorite } from '../../../providers/esteem/esteem';
// Constants
import { default as ROUTES } from '../../../constants/routeNames';
@ -69,9 +69,7 @@ class ProfileContainer extends Component {
const {
navigation, currentAccount, activeBottomTab, isLoggedIn,
} = this.props;
const currentUsername = currentAccount.name
!== nextProps.currentAccount.name
&& nextProps.currentAccount.name;
const currentUsername = currentAccount.name !== nextProps.currentAccount.name && nextProps.currentAccount.name;
const isParamsChange = nextProps.navigation.state
&& navigation.state
&& nextProps.navigation.state.params
@ -87,7 +85,10 @@ class ProfileContainer extends Component {
this._loadProfile(currentUsername);
}
if (activeBottomTab !== nextProps.activeBottomTab && nextProps.activeBottomTab === 'ProfileTabbar') {
if (
activeBottomTab !== nextProps.activeBottomTab
&& nextProps.activeBottomTab === 'ProfileTabbar'
) {
this._loadProfile(currentAccount.name);
}
@ -99,14 +100,11 @@ class ProfileContainer extends Component {
}
_getReplies = async (user) => {
await getRepliesByLastUpdate({ start_author: user, limit: 10 })
.then((result) => {
this.setState({
isReady: true,
comments: result,
});
})
.catch(() => {});
await getRepliesByLastUpdate({ start_author: user, limit: 10 }).then((result) => {
this.setState({
comments: result,
});
});
};
_handleFollowUnfollowUser = async (isFollowAction) => {
@ -196,28 +194,31 @@ class ProfileContainer extends Component {
_fetchProfile = async (username = null) => {
if (username) {
const { isLoggedIn, currentAccount } = this.props;
let _isFollowing;
let _isMuted;
let _isFavorite;
let _follows;
let isFollowing;
let isMuted;
let isFavorite;
let follows;
if (isLoggedIn && currentAccount.name !== username) {
_isFollowing = await getIsFollowing(username, currentAccount.name);
isFollowing = await getIsFollowing(username, currentAccount.name);
_isMuted = _isFollowing ? false : await getIsMuted(username, currentAccount.name);
isMuted = isFollowing ? false : await getIsMuted(username, currentAccount.name);
_isFavorite = getIsFavorite(username, currentAccount.name);
getIsFavorite(username, currentAccount.name).then((isFav) => {
isFavorite = isFav;
});
}
await getFollows(username).then((res) => {
_follows = res;
follows = res;
});
this.setState({
follows: _follows,
isFollowing: _isFollowing,
isMuted: _isMuted,
isFavorite: _isFavorite,
follows,
isFollowing,
isMuted,
isFavorite,
isReady: true,
});
}
};
@ -268,6 +269,41 @@ class ProfileContainer extends Component {
});
};
_addFavorite = () => {
const { currentAccount } = this.props;
const { username } = this.state;
addFavorite(currentAccount.name, username).then(() => {
this.setState({ isFavorite: true });
});
};
_removeFavorite = () => {
const { currentAccount } = this.props;
const { username } = this.state;
removeFavorite(currentAccount.name, username).then(() => {
this.setState({ isFavorite: false });
});
};
_handleOnFavoritePress = (isFavorite) => {
if (isFavorite) {
this._removeFavorite();
} else {
this._addFavorite();
}
};
_handleOnBackPress = () => {
const { navigation } = this.props;
const navigationParams = navigation.state && navigation.state.params;
if (navigationParams && navigationParams.fetchData) {
navigationParams.fetchData();
}
};
render() {
const {
avatar,
@ -296,6 +332,8 @@ class ProfileContainer extends Component {
follows={follows}
handleFollowUnfollowUser={this._handleFollowUnfollowUser}
handleMuteUnmuteUser={this._handleMuteUnmuteUser}
handleOnBackPress={this._handleOnBackPress}
handleOnFavoritePress={this._handleOnFavoritePress}
handleOnFollowsPress={this._handleFollowsPress}
isDarkTheme={isDarkTheme}
isFavorite={isFavorite}

View File

@ -58,6 +58,8 @@ class ProfileScreen extends PureComponent {
follows,
handleFollowUnfollowUser,
handleMuteUnmuteUser,
handleOnBackPress,
handleOnFavoritePress,
handleOnFollowsPress,
intl,
isDarkTheme,
@ -103,6 +105,7 @@ class ProfileScreen extends PureComponent {
key={selectedQuickProfile && selectedQuickProfile.name}
selectedUser={selectedQuickProfile}
isReverse={isReverseHeader}
handleOnBackPress={handleOnBackPress}
/>
<View style={styles.container}>
{!isReady ? (
@ -128,13 +131,15 @@ class ProfileScreen extends PureComponent {
followingCount={follows.following_count}
handleFollowUnfollowUser={handleFollowUnfollowUser}
handleMuteUnmuteUser={handleMuteUnmuteUser}
handleOnFavoritePress={handleOnFavoritePress}
handleOnFollowsPress={handleOnFollowsPress}
handleUIChange={this._handleUIChange}
hoursRC={fullInHourRC || null}
hoursVP={fullInHourVP || null}
intl={intl}
isDarkTheme={isDarkTheme}
isFollowing={isFollowing}
isFavorite={isFavorite}
isFollowing={isFollowing}
isLoggedIn={isLoggedIn}
isMuted={isMuted}
isOwnProfile={!isReverseHeader}
@ -143,7 +148,6 @@ class ProfileScreen extends PureComponent {
location={location}
percentRC={resourceCredits}
percentVP={votingPower}
handleUIChange={this._handleUIChange}
/>
</CollapsibleCard>
)}