Advertisement
Guest User

portal.js

a guest
Mar 1st, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // @flow
  2.  
  3. import React, { PureComponent } from 'react';
  4. import { connect } from 'react-redux';
  5. import { withRouter } from 'react-router';
  6. import { compose } from 'recompose';
  7. import {
  8.   NOTIFICATIONS,
  9.   SOFTWARE_UPDATE_TIMEOUT,
  10.   MINER_ERROR_TIMEOUT,
  11. } from 'src/constants/portal';
  12. import { SHOW_NOTIFICATION } from 'src/actions/notification';
  13. import { UPDATE_PREFERENCES } from 'src/actions/middleware';
  14. import { injectMinerConsumables } from 'src/components/middleware';
  15. import { find } from 'lodash';
  16. import uuid from 'shared/utilities/uuid';
  17. import { NOTIFICATION_TYPE } from 'shared/constants';
  18. import Header from 'src/components/Portal/Header/Header';
  19. import Tabs from 'src/components/common/Tabs/Tabs';
  20. import { PORTAL_TABS } from 'src/constants/navigation';
  21. import Sidebar from './Sidebar/Sidebar';
  22. import Modal from './Modal/Modal';
  23. import Store from './Store/Store';
  24. import Notification from './Notification/Notification';
  25. import MyGames from './MyGames/MyGames';
  26. import './Portal.scss';
  27.  
  28. class Portal extends PureComponent {
  29.   constructor(props) {
  30.     super(props);
  31.  
  32.     this.handleTabSelect = this.handleTabSelect.bind(this);
  33.     this.getActiveTabFromUrl = this.getActiveTabFromUrl.bind(this);
  34.  
  35.     const activeTab = this.getActiveTabFromUrl();
  36.  
  37.     this.state = {
  38.       activeTabKey: activeTab.tabKey,
  39.     };
  40.   }
  41.  
  42.   componentWillUpdate() {
  43.     const nextTab = this.getActiveTabFromUrl();
  44.     const { location: { pathname: currentPath } } = this.props;
  45.  
  46.     if (nextTab.path !== currentPath) {
  47.       this.setActiveTab(nextTab);
  48.     }
  49.   }
  50.  
  51.   setActiveTab(activeTab) {
  52.     this.props.showNotification({
  53.       notificationName: NOTIFICATIONS.UPDATE_ANNOUNCEMENT,
  54.       timeout: SOFTWARE_UPDATE_TIMEOUT,
  55.     });
  56.  
  57.     this.setState({
  58.       activeTabKey: activeTab.tabKey,
  59.     });
  60.   }
  61.  
  62.   getActiveTabFromUrl() {
  63.     const { history: { location: { pathname: url } } } = this.props;
  64.     return find(PORTAL_TABS, portalTab => {
  65.       return portalTab.path === url;
  66.     });
  67.   }
  68.  
  69.   handleTabSelect(key) {
  70.     const { history } = this.props;
  71.     const nextTab = find(PORTAL_TABS, portalTab => {
  72.       return portalTab.tabKey === key;
  73.     });
  74.  
  75.     history.push(nextTab.path);
  76.   }
  77.  
  78.   render() {
  79.     const { activeTabKey } = this.state;
  80.  
  81.     return (
  82.       <div className="gc-portal">
  83.         <Header />
  84.         <Sidebar />
  85.         <Modal />
  86.         <div className="gc-portal__content">
  87.           <div className="gc-layout gc-layout--hr">
  88.             <div className="gc-layout--max">
  89.               <Tabs
  90.                 activeKey={activeTabKey}
  91.                 onSelect={this.handleTabSelect}
  92.                 id="portalTabs"
  93.               >
  94.                 <Tabs.List>
  95.                   <Tabs.Item eventKey={1}>Store</Tabs.Item>
  96.                   <Tabs.Item eventKey={2}> My Games</Tabs.Item>
  97.                 </Tabs.List>
  98.                 <Tabs.Content>
  99.                   <Tabs.Tab eventKey={1}>
  100.                     <Store />
  101.                   </Tabs.Tab>
  102.                   <Tabs.Tab eventKey={2}>
  103.                     <MyGames />
  104.                   </Tabs.Tab>
  105.                 </Tabs.Content>
  106.               </Tabs>
  107.             </div>
  108.           </div>
  109.         </div>
  110.         <Notification />
  111.       </div>
  112.     );
  113.   }
  114. }
  115.  
  116. const actions = {
  117.   showNotification: (payload: Object) => ({
  118.     type: SHOW_NOTIFICATION,
  119.     payload: {
  120.       ...payload,
  121.       id: uuid(),
  122.     },
  123.   }),
  124.   remoteUpdatePreferences: (payload: Object) => ({
  125.     type: UPDATE_PREFERENCES,
  126.     payload,
  127.   }),
  128. };
  129.  
  130. export default compose(
  131.   withRouter,
  132.   connect(null, actions),
  133.   injectMinerConsumables({
  134.     releaseNotes: (releaseNotes, { showNotification }) => {
  135.       showNotification({
  136.         notificationName: NOTIFICATIONS.UPDATE_ANNOUNCEMENT,
  137.         timeout: SOFTWARE_UPDATE_TIMEOUT,
  138.       });
  139.     },
  140.     notification: (notification, { showNotification }) => {
  141.       switch (notification.type) {
  142.         case NOTIFICATION_TYPE.ERROR: {
  143.           showNotification({
  144.             notificationName: NOTIFICATIONS.SIMPLE_NOTIFICATION,
  145.             timeout: MINER_ERROR_TIMEOUT,
  146.             data: {
  147.               message: notification.error.message,
  148.               type: notification.type,
  149.               size: 'full',
  150.               links: notification.error.links,
  151.             },
  152.           });
  153.           break;
  154.         }
  155.         default:
  156.           break;
  157.       }
  158.     },
  159.     updatePreferences: (
  160.       { preferences, name },
  161.       { remoteUpdatePreferences, minerMiddleware: middleware },
  162.     ) => remoteUpdatePreferences({ preferences, name, middleware }),
  163.     downloadProgress: (_, { showNotification }) =>
  164.       showNotification({
  165.         notificationName: NOTIFICATIONS.MINER_DOWNLOAD,
  166.         timeout: SOFTWARE_UPDATE_TIMEOUT,
  167.       }),
  168.   }),
  169. )(Portal);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement