From 7b2565344e4357753afee9c625ae9a0f28b3f961 Mon Sep 17 00:00:00 2001 From: u-e Date: Fri, 11 Jan 2019 17:17:53 +0300 Subject: [PATCH] fixed data fetch from back button && created remove and add --- .../header/container/headerContainer.js | 4 +- .../profileSummary/view/profileSummaryView.js | 2 + src/providers/esteem/esteem.js | 4 +- .../bookmarks/container/bookmarksContainer.js | 12 ++- .../profile/container/profileContainer.js | 88 +++++++++++++------ src/screens/profile/screen/profileScreen.js | 8 +- 6 files changed, 85 insertions(+), 33 deletions(-) diff --git a/src/components/header/container/headerContainer.js b/src/components/header/container/headerContainer.js index 7d189514e..2ed0cd4bf 100644 --- a/src/components/header/container/headerContainer.js +++ b/src/components/header/container/headerContainer.js @@ -34,7 +34,9 @@ class HeaderContainer extends PureComponent { }; _handleOnPressBackButton = () => { - const { navigation } = this.props; + const { navigation, handleOnBackPress } = this.props; + + if (handleOnBackPress) handleOnBackPress(); navigation.goBack(); }; diff --git a/src/components/profileSummary/view/profileSummaryView.js b/src/components/profileSummary/view/profileSummaryView.js index e9a0358e4..98182c63e 100644 --- a/src/components/profileSummary/view/profileSummaryView.js +++ b/src/components/profileSummary/view/profileSummaryView.js @@ -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 ? ( diff --git a/src/providers/esteem/esteem.js b/src/providers/esteem/esteem.js index f7f07cee3..d3b8f5a72 100644 --- a/src/providers/esteem/esteem.js +++ b/src/providers/esteem/esteem.js @@ -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); diff --git a/src/screens/bookmarks/container/bookmarksContainer.js b/src/screens/bookmarks/container/bookmarksContainer.js index ef38c18f1..c46736346 100644 --- a/src/screens/bookmarks/container/bookmarksContainer.js +++ b/src/screens/bookmarks/container/bookmarksContainer.js @@ -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, }, }); }; diff --git a/src/screens/profile/container/profileContainer.js b/src/screens/profile/container/profileContainer.js index a6b291ff0..a3afe6421 100644 --- a/src/screens/profile/container/profileContainer.js +++ b/src/screens/profile/container/profileContainer.js @@ -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} diff --git a/src/screens/profile/screen/profileScreen.js b/src/screens/profile/screen/profileScreen.js index 272edb031..bd0e7602b 100644 --- a/src/screens/profile/screen/profileScreen.js +++ b/src/screens/profile/screen/profileScreen.js @@ -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} /> {!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} /> )}