Guest User

Untitled

a guest
Mar 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import { compose, graphql } from 'react-apollo';
  2. import PropTypes from 'prop-types';
  3.  
  4. import React from 'react';
  5.  
  6. import { GET_USER, SET_USER } from '../../state/queries';
  7. import { getCookie } from '../../../utils';
  8. import LoginButton from './loginButton';
  9. import ProfileButton from './profileButton';
  10.  
  11. class UserMenu extends React.Component {
  12. static propTypes = {
  13. messages: PropTypes.object,
  14. user: PropTypes.object,
  15. setUser: PropTypes.func
  16. };
  17.  
  18. // check the users auth cookie and set the global user state
  19. checkAuthCookie = () => {
  20. var cookie = getCookie('Authorization');
  21. if (cookie) {
  22. // slice off the "Bearer " and then split into the three sections, taking middle
  23. cookie = cookie.split(' ')[1].split('.')[1];
  24. cookie = atob(cookie);
  25. cookie = JSON.parse(cookie);
  26. const { user } = cookie;
  27.  
  28. this.props.setUser({
  29. name: user.GivenName,
  30. avatarURL: user.AvatarURL,
  31. email: user.Email,
  32. ID: user.ID
  33. });
  34. }
  35. };
  36.  
  37. render() {
  38. const { messages, user } = this.props;
  39.  
  40. if (!user.ID) {
  41. this.checkAuthCookie();
  42. }
  43.  
  44. return user.ID ? (
  45. <ProfileButton messages={messages} />
  46. ) : (
  47. <LoginButton messages={messages} />
  48. );
  49. }
  50. }
  51.  
  52. const setUser = graphql(SET_USER, {
  53. props: ({ mutate }) => ({
  54. setUser: ({ name, avatarURL, email, ID }) => {
  55. mutate({ variables: { name, avatarURL, email, ID } });
  56. }
  57. })
  58. });
  59.  
  60. const getUser = graphql(GET_USER, {
  61. props: ({ data }) => ({ user: data.user })
  62. });
  63.  
  64. export default compose(setUser, getUser)(UserMenu);
Add Comment
Please, Sign In to add comment