import React from 'react'; import { withRouter, RouteComponentProps, Redirect, Switch, Route, } from 'react-router-dom'; import { connect } from 'react-redux'; import { compose } from 'recompose'; import { Tabs, Badge } from 'antd'; import { usersActions } from 'modules/users'; import { AppState } from 'store/reducers'; import HeaderDetails from 'components/HeaderDetails'; import ProfileUser from './ProfileUser'; import ProfileEdit from './ProfileEdit'; import ProfilePendingList from './ProfilePendingList'; import ProfileRejectedPermanentlyList from './ProfileRejectedPermanentlyList'; import ProfileProposal from './ProfileProposal'; import ProfileContribution from './ProfileContribution'; import ProfileComment from './ProfileComment'; import ProfileInvite from './ProfileInvite'; import ProfileCCR from './ProfileCCR'; import Placeholder from 'components/Placeholder'; import Loader from 'components/Loader'; import ExceptionPage from 'components/ExceptionPage'; import ContributionModal from 'components/ContributionModal'; import LinkableTabs from 'components/LinkableTabs'; import { UserContribution } from 'types'; import ProfileArbitrated from './ProfileArbitrated'; import './style.less'; interface StateProps { usersMap: AppState['users']['map']; authUser: AppState['auth']['user']; hasCheckedUser: AppState['auth']['hasCheckedUser']; } interface DispatchProps { fetchUser: typeof usersActions['fetchUser']; fetchUserInvites: typeof usersActions['fetchUserInvites']; } type Props = RouteComponentProps & StateProps & DispatchProps; interface State { activeContribution: UserContribution | null; } class Profile extends React.Component { state: State = { activeContribution: null, }; componentDidMount() { this.fetchData(); } componentDidUpdate(prevProps: Props) { const userLookupId = this.props.match.params.id; const prevUserLookupId = prevProps.match.params.id; if (userLookupId !== prevUserLookupId) { window.scrollTo(0, 0); this.fetchData(); } } render() { const { authUser, match, location, hasCheckedUser } = this.props; const { activeContribution } = this.state; const userLookupParam = match.params.id; if (!userLookupParam) { if (authUser && authUser.userid) { return ; } else { return ; } } const user = this.props.usersMap[userLookupParam]; const waiting = !user || !user.hasFetched || !hasCheckedUser; const isAuthedUser = user && authUser && user.userid === authUser.userid; if (waiting) { return ; } if (user.fetchError) { return ; } const { proposals, pendingProposals, pendingRequests, rejectedPermanentlyProposals, rejectedPermanentlyRequests, requests, contributions, comments, invites, arbitrated, } = user; const isLoading = user.isFetching; const noProposalsPending = pendingProposals.length === 0; const noProposalsCreated = proposals.length === 0; const noRequestsPending = pendingRequests.length === 0; const noRequestsCreated = requests.length === 0; const noneFunded = contributions.length === 0; const noneCommented = comments.length === 0; const noneArbitrated = arbitrated.length === 0; const noneInvites = user.hasFetchedInvites && invites.length === 0; const rejectedPermanentlyCount = rejectedPermanentlyProposals.length + rejectedPermanentlyRequests.length; const noneRejectedPermanently = rejectedPermanentlyCount === 0; // do not allow a tab to be linked to if it won't be visible in the ui const skippedTabs = [ ...(noneFunded ? ['funded'] : []), ...(noneRejectedPermanently ? ['rejected'] : []), ]; const privateMsg = 'Note: This tab is private to your account'; const publicMsg = 'Note: This tab is public to ZF Grants'; const renderTabMsg = (msg: string) => isAuthedUser && (
{msg}
); return (
} /> } />
{isAuthedUser && ( {renderTabMsg(privateMsg)}
{noProposalsPending && noRequestsPending && ( )}
)} {renderTabMsg(publicMsg)}
{noProposalsCreated && noRequestsCreated && ( )} {proposals.map(p => ( ))} {requests.map(c => ( ))}
{!noneFunded && ( {renderTabMsg(publicMsg)}
{contributions.map(c => ( ))}
)} {renderTabMsg(publicMsg)}
{noneCommented && ( )} {comments.map(c => ( ))}
{isAuthedUser && ( {renderTabMsg(privateMsg)}
{noneInvites && ( )} {invites.map(invite => ( ))}
)} {isAuthedUser && ( {renderTabMsg(privateMsg)} {noneArbitrated && ( )} {arbitrated.map(arb => ( ))} )} {isAuthedUser && !noneRejectedPermanently && ( {renderTabMsg(privateMsg)}
)}
); } private fetchData() { const { match } = this.props; const userLookupId = match.params.id; if (userLookupId) { this.props.fetchUser(userLookupId); this.props.fetchUserInvites(userLookupId); } } private openContributionModal = (c: UserContribution) => this.setState({ activeContribution: c }); private closeContributionModal = () => this.setState({ activeContribution: null }); } const TabTitle = (disp: string, count: number) => (
{disp} 0 ? 'is-not-zero' : 'is-zero'}`} showZero={true} count={count} />
); const withConnect = connect( state => ({ usersMap: state.users.map, authUser: state.auth.user, hasCheckedUser: state.auth.hasCheckedUser, }), { fetchUser: usersActions.fetchUser, fetchUserInvites: usersActions.fetchUserInvites, }, ); export default compose( withRouter, withConnect, )(Profile);