Header.jsx 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React from 'react'
  2. import { connect } from 'react-redux'
  3. import { withRouter } from 'react-router'
  4. import i18n from '../i18n.js'
  5. import appFactory from '../appFactory.js'
  6. import { translate } from 'react-i18next'
  7. import Cookies from 'js-cookie'
  8. import Logo from '../component/Header/Logo.jsx'
  9. import NavbarToggler from '../component/Header/NavbarToggler.jsx'
  10. import MenuLinkList from '../component/Header/MenuLinkList.jsx'
  11. import MenuActionListItemSearch from '../component/Header/MenuActionListItem/Search.jsx'
  12. import MenuActionListItemDropdownLang from '../component/Header/MenuActionListItem/DropdownLang.jsx'
  13. import MenuActionListItemHelp from '../component/Header/MenuActionListItem/Help.jsx'
  14. import MenuActionListItemMenuProfil from '../component/Header/MenuActionListItem/MenuProfil.jsx'
  15. import MenuActionListItemNotification from '../component/Header/MenuActionListItem/Notification.jsx'
  16. import MenuActionListAdminLink from '../component/Header/MenuActionListItem/AdminLink.jsx'
  17. import logoHeader from '../img/logo-tracim.png'
  18. import {
  19. newFlashMessage,
  20. setUserLang,
  21. setUserDisconnected
  22. } from '../action-creator.sync.js'
  23. import {
  24. postUserLogout,
  25. putUserLang
  26. } from '../action-creator.async.js'
  27. import { COOKIE, PAGE, PROFILE } from '../helper.js'
  28. class Header extends React.Component {
  29. handleClickLogo = () => {}
  30. handleClickFeature = () => {}
  31. handleClickExplore = () => {}
  32. handleClickAbout = () => {}
  33. handleChangeInput = e => this.setState({inputSearchValue: e.target.value})
  34. handleClickSubmit = () => {}
  35. handleChangeLang = async idLang => {
  36. const { props } = this
  37. const fetchPutUserLang = await props.dispatch(putUserLang(props.user, idLang))
  38. switch (fetchPutUserLang.status) {
  39. case 200:
  40. props.dispatch(setUserLang(idLang))
  41. i18n.changeLanguage(idLang)
  42. props.dispatchCustomEvent('allApp_changeLang', idLang)
  43. break
  44. default: props.dispatch(newFlashMessage(props.t('Error while saving new lang'))); break
  45. }
  46. }
  47. handleClickHelp = () => {}
  48. handleClickLogout = async () => {
  49. const { history, dispatch, t } = this.props
  50. const fetchPostUserLogout = await dispatch(postUserLogout())
  51. if (fetchPostUserLogout.status === 204) {
  52. Cookies.remove(COOKIE.USER_LOGIN)
  53. Cookies.remove(COOKIE.USER_AUTH)
  54. dispatch(setUserDisconnected())
  55. history.push(PAGE.LOGIN)
  56. } else {
  57. dispatch(newFlashMessage(t('Disconnection error', 'danger')))
  58. }
  59. }
  60. render () {
  61. const { lang, user } = this.props
  62. return (
  63. <header className='header'>
  64. <nav className='navbar navbar-expand-md navbar-light bg-light'>
  65. <Logo logoSrc={logoHeader} onClickImg={this.handleClickLogo} />
  66. { /*
  67. <div className='header__breadcrumb d-none d-lg-block ml-4'>
  68. Dev Tracim - liste des contenus
  69. </div>
  70. */ }
  71. <NavbarToggler />
  72. <div className='header__menu collapse navbar-collapse justify-content-end' id='navbarSupportedContent'>
  73. <MenuLinkList
  74. onClickFeature={this.handleClickFeature}
  75. onClickExplore={this.handleClickExplore}
  76. onClickAbout={this.handleClickAbout}
  77. />
  78. <ul className='header__menu__rightside'>
  79. <MenuActionListItemSearch
  80. onChangeInput={this.handleChangeInput}
  81. onClickSubmit={this.handleClickSubmit}
  82. />
  83. {user.profile === PROFILE.ADMINISTRATOR.slug &&
  84. <MenuActionListAdminLink t={this.props.t} />
  85. }
  86. <MenuActionListItemDropdownLang
  87. langList={lang}
  88. idLangActive={user.lang ? user.lang : 'en'}
  89. onChangeLang={this.handleChangeLang}
  90. />
  91. <MenuActionListItemHelp
  92. onClickHelp={this.handleClickHelp}
  93. />
  94. <MenuActionListItemNotification />
  95. <MenuActionListItemMenuProfil
  96. user={user}
  97. onClickLogout={this.handleClickLogout}
  98. />
  99. </ul>
  100. </div>
  101. </nav>
  102. </header>
  103. )
  104. }
  105. }
  106. const mapStateToProps = ({ lang, user }) => ({ lang, user })
  107. export default withRouter(connect(mapStateToProps)(translate()(appFactory(Header))))