import { hot } from 'react-hot-loader/root'; import * as React from 'react'; import { Tabs } from 'antd'; import Profile from './Profile'; import { connect } from 'react-redux'; import { State, Actions } from '../redux/redux'; import { getProfileIds } from '../redux'; import * as actions from '../redux/actions'; import Tab from './Tab'; interface ApplicationProps { profileIds: string[]; currentProfileId: string; addProfile: () => void; removeProfile: (id: string) => void; setCurrentProfileId: (id: string) => void; } class Application extends React.Component { onChangeTab = (activeKey: string) => { this.props.setCurrentProfileId(activeKey); }; onEditTab = (targetKey: string | React.MouseEvent, action: 'add' | 'remove') => { if (action === 'add') { this.props.addProfile(); } else { this.props.removeProfile(targetKey as string); } }; render() { const { profileIds, currentProfileId } = this.props; const activeKey = profileIds.includes(currentProfileId) ? currentProfileId : profileIds[0]; return (
{profileIds.map(id => ( } key={id} closable={profileIds.length > 1 && id === activeKey}> ))}
); } } const EnhancedApplication = connect( (state: State) => { return { profileIds: getProfileIds(state), currentProfileId: state.currentProfileId, }; }, (dispatch: React.Dispatch) => ({ addProfile: () => { dispatch(actions.addProfile()); }, removeProfile: (id: string) => { dispatch(actions.removeProfile(id)); }, setCurrentProfileId: (id: string) => { dispatch(actions.setCurrentProfileId(id)); }, }), )(Application); export default hot(EnhancedApplication);