// - Import react components import React, { Component } from 'react' import { connect } from 'react-redux' import { withRouter } from 'react-router-dom' import PropTypes from 'prop-types' import Tabs, { Tab } from 'material-ui/Tabs' import { grey, cyan } from 'material-ui/colors' import { push } from 'react-router-redux' import AppBar from 'material-ui/AppBar' import Typography from 'material-ui/Typography' // - Import app components import FindPeople from 'components/findPeople' import Following from 'components/following' import Followers from 'components/followers' import YourCircles from 'components/yourCircles' // - Import API // - Import actions import * as circleActions from 'actions/circleActions' import * as globalActions from 'actions/globalActions' import { IPeopleComponentProps } from './IPeopleComponentProps' import { IPeopleComponentState } from './IPeopleComponentState' const TabContainer = (props: any) => { return ( {props.children} ) } /** * Create component class */ export class PeopleComponent extends Component { static propTypes = { } /** * Component constructor * @param {object} props is an object properties of component */ constructor (props: IPeopleComponentProps) { super(props) const {tab} = this.props.match.params // Defaul state this.state = { tabIndex: this.getTabIndexByNav(tab) } // Binding functions to `this` } /** * Hadle on tab change */ handleChangeTab = (event: any, value: any) => { const {circlesLoaded, goTo, setHeaderTitle} = this.props this.setState({ tabIndex: value }) switch (value) { case 0: goTo!('/people') setHeaderTitle!('People') break case 1: goTo!('/people/circles') setHeaderTitle!('Circles') break case 2: goTo!('/people/followers') setHeaderTitle!('Followers') break default: break } } componentWillMount () { const { setHeaderTitle} = this.props const {tab} = this.props.match.params switch (tab) { case undefined: case '': setHeaderTitle!('People') break case 'circles': setHeaderTitle!('Circles') break case 'followers': setHeaderTitle!('Followers') break default: break } } /** * Reneder component DOM * @return {react element} return the DOM which rendered by component */ render () { /** * Component styles */ const styles = { people: { margin: '0 auto', width: '90%' }, headline: { fontSize: 24, paddingTop: 16, marginBottom: 12, fontWeight: 400 }, slide: { padding: 10 } } const {circlesLoaded, goTo, setHeaderTitle} = this.props const {tabIndex} = this.state return (
{tabIndex === 0 && {circlesLoaded ? : ''}} {tabIndex === 1 && {circlesLoaded ? : ''} {circlesLoaded ? : ''} } {tabIndex === 2 && {circlesLoaded ? : ''}}
) } /** * Get tab index by navigation name */ private getTabIndexByNav: (navName: string) => number = (navName: string) => { let tabIndex = 0 switch (navName) { case 'circles': return 1 case 'followers': return 2 default: return 0 } } } /** * Map dispatch to props * @param {func} dispatch is the function to dispatch action to reducers * @param {object} ownProps is the props belong to component * @return {object} props of component */ const mapDispatchToProps = (dispatch: any, ownProps: IPeopleComponentProps) => { return { goTo: (url: string) => dispatch(push(url)), setHeaderTitle : (title: string) => dispatch(globalActions.setHeaderTitle(title)) } } /** * Map state to props * @param {object} state is the obeject from redux store * @param {object} ownProps is the props belong to component * @return {object} props of component */ const mapStateToProps = (state: any, ownProps: IPeopleComponentProps) => { return { uid: state.authorize.uid, circlesLoaded: state.circle.loaded } } // - Connect component to redux store export default withRouter(connect(mapStateToProps, mapDispatchToProps)(PeopleComponent as any))