UserDropdown.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import s from './UserDropdown.scss';
  17. import Dropdown from 'react-dropdown';
  18. import Location from '../../core/Location';
  19. import UserStore from '../../stores/UserStore';
  20. import UserActions from '../../actions/UserActions';
  21. class UserDropdown extends Dropdown {
  22. static defaultProps = {
  23. options: [
  24. { label: "Profile", value: "profile" },
  25. { label: "Sign Out", value: "signout" },
  26. ],
  27. baseClassName: "UserDropdown"
  28. }
  29. constructor(props) {
  30. super(props)
  31. this.store = UserStore
  32. }
  33. setValue(value) {
  34. switch (value) {
  35. case "profile":
  36. Location.push("/user/profile");
  37. break;
  38. case "signout":
  39. UserActions.logout()
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. buildMenu() {
  46. let buildMenuResult = super.buildMenu.call(this)
  47. return (
  48. <div>
  49. <div className={s.userData}>
  50. <div className={s.userName}>{this.props.userData.name}</div>
  51. <div className={s.userEmail}>{this.props.userData.email}</div>
  52. </div>
  53. <div className={s.userMenu}>
  54. {buildMenuResult}
  55. </div>
  56. </div>
  57. )
  58. }
  59. handleMouseDown(event) {
  60. super.handleMouseDown.call(this, event)
  61. this.setState({ firstHover: false })
  62. }
  63. render() {
  64. let result = super.render.call(this)
  65. let children = Object.assign({}, result.props.children)
  66. children = [(<div
  67. className={s.userIcon + (this.props.dark ? " dark" : "")}
  68. onMouseDown={(e) => this.handleMouseDown(e)}
  69. />), children[1]]
  70. let newResult = React.cloneElement(result, { children: children })
  71. return newResult
  72. }
  73. }
  74. export default withStyles(UserDropdown, s);