Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import React from 'react';
  2. import { render } from 'react-dom';
  3. import { connect } from 'react-redux'
  4. import { Switch, BrowserRouter, Route, Link } from 'react-router-dom';
  5.  
  6. // antd
  7. import { Layout, Breadcrumb, Menu, Icon } from 'antd';
  8. const { Header, Content, Footer, Sider } = Layout;
  9.  
  10. // Helpers
  11. import { Alert } from '../helpers/notifications';
  12.  
  13. // Components
  14. import Home from '../components/Home';
  15. // import Header from '../components/Header';
  16. import NotFound from '../components/NotFound';
  17. import PostsEditor from '../components/Posts/PostsEditor';
  18.  
  19. // Actions
  20. import { setRouteActiveFlag } from '../actions/ui.action'
  21.  
  22. class Sidebar extends React.Component {
  23.  
  24. componentDidMount () {
  25. const routes = {
  26. '/' : 1,
  27. '/posts' : 2,
  28. '/logout' : 3
  29. }
  30.  
  31. this.props.detectActiveRoute(setRouteActiveFlag({
  32. routes:routes,
  33. path:window.location.pathname
  34. }))
  35. }
  36.  
  37.  
  38. render() {
  39.  
  40. const { selectedRoute } = this.props;
  41. console.log(selectedRoute);
  42.  
  43. return (
  44. <div>
  45. <Layout>
  46. <Sider
  47. style={{
  48. overflow: 'auto',
  49. height: '100vh',
  50. position: 'fixed',
  51. left: 0,
  52. }}
  53. breakpoint="lg"
  54. collapsedWidth="0"
  55. onBreakpoint={broken => {
  56. console.log(broken);
  57. }}
  58. onCollapse={(collapsed, type) => {
  59. console.log(collapsed, type);
  60. }}
  61. >
  62. <div className="logo" >
  63. Logo <br/><br/><br/>
  64. </div>
  65. <Menu theme="dark" mode="inline" style={{ lineHeight: '64px' }} defaultSelectedKeys={[selectedRoute.toString() || '1']}>
  66. <Menu.Item key="1">
  67. <Link to="/" style={{ color:'#fff' }}>
  68. <Icon type="user" />
  69. <span className="nav-text">Home</span>
  70. </Link>
  71. </Menu.Item>
  72. <Menu.Item key="2">
  73. <Link to="/posts" style={{ color:'#fff' }}>
  74. <Icon type="user" />
  75. <span className="nav-text">Posts</span>
  76. </Link>
  77. </Menu.Item>
  78. <Menu.Item key="3">
  79. <a href="/logout" style={{ color:'#fff' }}>
  80. <Icon type="user" />
  81. <span className="nav-text">Logout</span>
  82. </a>
  83. </Menu.Item>
  84. </Menu>
  85. </Sider>
  86. <Layout style={{ marginLeft: 200 }}>
  87. <Content style={{ margin: '24px 16px 0', overflow: 'initial'}}>
  88.  
  89. <Breadcrumb style={{ margin: '0 0 20px 0' }}>
  90. <Breadcrumb.Item>Home</Breadcrumb.Item>
  91. <Breadcrumb.Item>List</Breadcrumb.Item>
  92. <Breadcrumb.Item>App</Breadcrumb.Item>
  93. </Breadcrumb>
  94.  
  95. <div style={{ padding: 24, background: '#fff', minHeight: 360 }}>
  96. <Switch>
  97. <Route path="/" exact component={Home} />
  98. <Route path="/posts/:id?" component={PostsEditor} />
  99. <Route component={NotFound}/>
  100. </Switch>
  101. <Alert stack={ { limit: 3 } } />
  102. </div>
  103.  
  104. </Content>
  105.  
  106. <Footer style={{ textAlign: 'center' }}>Ant Design ©2018 Created by Ant UED</Footer>
  107. </Layout>
  108. </Layout>
  109. </div>
  110. );
  111. }
  112. }
  113.  
  114. const mapStateToProps = (state, ownProps) => {
  115. return {
  116. state: state,
  117. props: ownProps,
  118. selectedRoute:state.ui.selectedRoute || 1
  119. }
  120. }
  121.  
  122. const mapDispatchToProps = (dispatch, ownProps) => {
  123. return {
  124. detectActiveRoute: (obj) => dispatch(obj)
  125. }
  126. }
  127.  
  128.  
  129. export default connect(
  130. mapStateToProps,
  131. mapDispatchToProps
  132. )(Sidebar)
  133.  
  134. export const setRouteActiveFlag = (payload = 'global') => ({
  135. type: actions.SET_ROUTE_ACTIVE_FLAG,
  136. payload
  137. });
  138.  
  139. import { handleActions } from 'redux-actions';
  140. import Immutable from 'seamless-immutable';
  141. import * as actions from '../consts/action-types';
  142.  
  143.  
  144. const initialState = Immutable({
  145. requests: {},
  146. selectedRoute:{}
  147. });
  148.  
  149.  
  150. export default handleActions({
  151. [actions.SET_ROUTE_ACTIVE_FLAG]: (state, action) => {
  152. if (action.payload.routes && action.payload.path && action.payload.routes[ action.payload.path ]) {
  153. return state.set('selectedRoute', action.payload.routes[ action.payload.path ])
  154. }else{
  155. return state.set('selectedRoute', 1)
  156. }
  157. }
  158. }, initialState);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement