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 = () => { _handleOnPressBackButton = () => {
const { navigation } = this.props; const { navigation, handleOnBackPress } = this.props;
if (handleOnBackPress) handleOnBackPress();
navigation.goBack(); navigation.goBack();
}; };

View File

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

View File

@ -109,7 +109,7 @@ export const getIsFavorite = (targetUsername, currentUsername) => api.get(`/isfa
export const addFavorite = (currentUsername, targetUsername) => api export const addFavorite = (currentUsername, targetUsername) => api
.post('/favorite', { .post('/favorite', {
username: currentUsername, username: currentUsername,
targetUsername, account: targetUsername,
}) })
.then(resp => resp.data); .then(resp => resp.data);
@ -117,7 +117,7 @@ export const addFavorite = (currentUsername, targetUsername) => api
* @params current username * @params current username
* @params target 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); 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'; import { injectIntl } from 'react-intl';
// Services and Actions // Services and Actions
import { getFavorites, removeFavoriteUser } from '../../../providers/esteem/esteem'; import { getFavorites, removeFavorite } from '../../../providers/esteem/esteem';
// Constants // Constants
import ROUTES from '../../../constants/routeNames'; import ROUTES from '../../../constants/routeNames';
@ -32,10 +32,15 @@ class DraftsContainer extends Component {
// Component Life Cycle Functions // Component Life Cycle Functions
componentDidMount() { componentDidMount() {
this._getFavorites(); this._fetchData();
} }
// Component Functions // Component Functions
_fetchData = () => {
this._getFavorites();
};
_getFavorites = () => { _getFavorites = () => {
const { currentAccount, intl } = this.props; const { currentAccount, intl } = this.props;
this.setState({ isLoading: true }); this.setState({ isLoading: true });
@ -53,7 +58,7 @@ class DraftsContainer extends Component {
_removeFavorite = (selectedUsername) => { _removeFavorite = (selectedUsername) => {
const { currentAccount, intl } = this.props; const { currentAccount, intl } = this.props;
removeFavoriteUser(currentAccount.name, selectedUsername) removeFavorite(currentAccount.name, selectedUsername)
.then(() => { .then(() => {
const { favorites } = this.state; const { favorites } = this.state;
const newFavorites = [...favorites].filter(fav => fav.account !== selectedUsername); const newFavorites = [...favorites].filter(fav => fav.account !== selectedUsername);
@ -72,6 +77,7 @@ class DraftsContainer extends Component {
routeName: ROUTES.SCREENS.PROFILE, routeName: ROUTES.SCREENS.PROFILE,
params: { params: {
username, username,
fetchData: this._fetchData,
}, },
}); });
}; };

View File

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

View File

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