// @flow import * as React from 'react'; import compose from 'recompose/compose'; import querystring from 'query-string'; import { withRouter, type History, type Location, type Match, } from 'react-router'; import { connect } from 'react-redux'; import generateMetaInfo from 'shared/generate-meta-info'; import Head from 'src/components/head'; import ThreadFeed from 'src/components/threadFeed'; import { UserProfileCard } from 'src/components/entities'; import { setTitlebarProps } from 'src/actions/titlebar'; import CommunityList from './components/communityList'; import { withCurrentUser } from 'src/components/withCurrentUser'; import { UserAvatar } from 'src/components/avatar'; import { getUserByMatch, type GetUserType, } from 'shared/graphql/queries/user/getUser'; import getUserThreads from 'shared/graphql/queries/user/getUserThreadConnection'; import { ErrorView, LoadingView } from 'src/views/viewHelpers'; import viewNetworkHandler from 'src/components/viewNetworkHandler'; import type { Dispatch } from 'redux'; import { SegmentedControl, Segment } from 'src/components/segmentedControl'; import { ViewGrid, SecondaryPrimaryColumnGrid, PrimaryColumn, SecondaryColumn, } from 'src/components/layout'; import { SidebarSection, SidebarSectionHeader, SidebarSectionHeading, } from 'src/views/community/style'; import { NullColumn, NullColumnHeading, NullColumnSubheading, } from 'src/components/threadFeed/style'; import { MobileUserAction } from 'src/components/titlebar/actions'; import { FeedsContainer } from './style'; import { InfoContainer } from 'src/views/community/style'; const ThreadFeedWithData = compose( connect(), getUserThreads )(ThreadFeed); const ThreadParticipantFeedWithData = compose( connect(), getUserThreads )(ThreadFeed); type Props = { match: Match, currentUser: Object, data: { user: GetUserType, }, isLoading: boolean, queryVarIsChanging: boolean, dispatch: Dispatch, history: History, location: Location, }; type State = { hasNoThreads: boolean, hasThreads: boolean, }; class UserView extends React.Component { state = { hasNoThreads: false, hasThreads: true, }; componentDidMount() { const { dispatch } = this.props; if (this.props.data && this.props.data.user) { this.setDefaultTab(); return dispatch( setTitlebarProps({ title: this.props.data.user.name, titleIcon: ( ), rightAction: , }) ); } } setDefaultTab = () => { const { location, history } = this.props; const { search } = location; const { tab } = querystring.parse(search); if (!tab) history.replace({ ...location, search: querystring.stringify({ tab: 'posts' }), }); }; componentDidUpdate(prevProps: Props) { const { dispatch } = this.props; if (!prevProps.data || !this.props.data) return; if (!prevProps.data.user && this.props.data.user) { this.setDefaultTab(); return dispatch( setTitlebarProps({ title: this.props.data.user.name, titleIcon: ( ), rightAction: , }) ); } // track when a new profile is viewed without the component having been remounted if ( prevProps.data.user && this.props.data.user && prevProps.data.user.id !== this.props.data.user.id ) { this.setDefaultTab(); return dispatch( setTitlebarProps({ title: this.props.data.user.name, titleIcon: ( ), rightAction: , }) ); } } hasNoThreads = () => this.setState({ hasThreads: false }); hasThreads = () => this.setState({ hasThreads: true }); handleSegmentClick = (tab: string) => { const { history, location } = this.props; return history.replace({ ...location, search: querystring.stringify({ tab }), }); }; render() { const { data: { user }, isLoading, queryVarIsChanging, match: { params: { username }, }, location, currentUser, } = this.props; const { hasThreads } = this.state; const { search } = location; const { tab } = querystring.parse(search); const selectedView = tab; if (queryVarIsChanging) { return ; } if (user && user.id) { const { title, description } = generateMetaInfo({ type: 'user', data: { name: user.name, username: user.username, description: user.description, }, }); const Feed = selectedView === 'posts' ? ThreadFeedWithData : ThreadParticipantFeedWithData; return ( Communities this.handleSegmentClick('posts')} isActive={selectedView === 'posts'} data-cy="user-posts-tab" > Posts this.handleSegmentClick('activity')} isActive={selectedView === 'activity'} data-cy="user-activity-tab" > Activity this.handleSegmentClick('info')} hideOnDesktop isActive={selectedView === 'info'} data-cy="user-info-tab" > Info {hasThreads && (selectedView === 'posts' || selectedView === 'activity') && ( )} {selectedView === 'info' && ( Communities )} {!hasThreads && (selectedView === 'posts' || selectedView === 'activity') && ( No posts yet Posts will show up here as they are published and when conversations are joined. )} ); } if (isLoading) { return ; } if (!user) { return ( ); } return ; } } export default compose( getUserByMatch, withCurrentUser, viewNetworkHandler, withRouter, connect() )(UserView);