AllenYuan

pagination - onSetPage for Profile

Apr 19th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. src/components/Profile
  2.  
  3. // ...
  4.  
  5. const mapDispatchToProps = dispatch => ({
  6.   onFollow: username => dispatch({
  7.     type: 'FOLLOW_USER',
  8.     payload: agent.Profile.follow(username)
  9.   }),
  10.   onLoad: payload => dispatch({ type: 'PROFILE_PAGE_LOADED', payload }),
  11.   // SET_PAGE action with page# and data
  12.   onSetPage: (page, payload) => dispatch({ type: 'SET_PAGE', page, payload }),
  13.   onUnfollow: username => dispatch({
  14.     type: 'UNFOLLOW_USER',
  15.     payload: agent.Profile.unfollow(username)
  16.   }),
  17.   onUnload: () => dispatch({ type: 'PROFILE_PAGE_UNLOADED' })
  18. });
  19.  
  20. class Profile extends React.Component {
  21.   // ...
  22.   // get articles from this author
  23.   onSetPage(page) {
  24.     const promise = agent.Articles.byAuthor(this.props.profile.username, page);
  25.     this.props.onSetPage(page, promise);
  26.   }
  27.  
  28.   render() {
  29.     const profile = this.props.profile;
  30.     if (!profile) {e
  31.       return null;
  32.     }
  33.  
  34.     const isUser = this.props.currentUser &&
  35.       this.props.profile.username === this.props.currentUser.username;
  36.  
  37.     const onSetPage = page => this.onSetPage(page)
  38.  
  39.     return (
  40.       <div className="profile-page">
  41.  
  42.         <div className="user-info">
  43.           <div className="container">
  44.             <div className="row">
  45.               <div className="col-xs-12 col-md-10 offset-md-1">
  46.  
  47.                 <img src={profile.image} className="user-img" />
  48.                 <h4>{profile.username}</h4>
  49.                 <p>{profile.bio}</p>
  50.  
  51.                 <EditProfileSettings isUser={isUser} />
  52.                 <FollowUserButton
  53.                   isUser={isUser}
  54.                   user={profile}
  55.                   follow={this.props.onFollow}
  56.                   unfollow={this.props.onUnfollow}
  57.                   />
  58.  
  59.               </div>
  60.             </div>
  61.           </div>
  62.         </div>
  63.  
  64.         <div className="container">
  65.           <div className="row">
  66.  
  67.             <div className="col-xs-12 col-md-10 offset-md-1">
  68.  
  69.               <div className="articles-toggle">
  70.                 {this.renderTabs()}
  71.               </div>
  72.               // pass onSetPage to ArticleList
  73.               <ArticleList
  74.                 articles={this.props.articles}
  75.                 articlesCount={this.props.articlesCount}
  76.                 currentPage={this.props.currentPage}
  77.                 onSetPage={onSetPage} />
  78.             </div>
  79.  
  80.           </div>
  81.         </div>
  82.  
  83.       </div>
  84.     );
  85.   }
  86. }
  87.  
  88. export default connect(mapStateToProps, mapDispatchToProps)(Profile);
  89. export { Profile as Profile, mapStateToProps as mapStateToProps };
Advertisement
Add Comment
Please, Sign In to add comment