verify.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import Router from 'next/router';
  4. import withRedux from 'next-redux-wrapper';
  5. import { bindActionCreators } from 'redux';
  6. import styled from 'styled-components';
  7. import cookie from 'js-cookie';
  8. import initialState from '../store';
  9. import BodyWrapper from '../components/BodyWrapper';
  10. import { authRenew, authUser, showPageLoading } from '../actions';
  11. import Button from '../components/Button';
  12. const Wrapper = styled.div`
  13. display: flex;
  14. flex-direction: column;
  15. align-items: center;
  16. `;
  17. const MessageWrapper = styled.div`
  18. display: flex;
  19. justify-content: center;
  20. align-items: center;
  21. margin: 32px 0;
  22. `;
  23. const Message = styled.p`
  24. font-size: 24px;
  25. font-weight: 300;
  26. @media only screen and (max-width: 768px) {
  27. font-size: 18px;
  28. }
  29. `;
  30. const Icon = styled.img`
  31. width: 32px;
  32. height: 32px;
  33. margin-right: 16px;
  34. @media only screen and (max-width: 768px) {
  35. width: 26px;
  36. height: 26px;
  37. margin-right: 8px;
  38. }
  39. `;
  40. const Verify = ({ showLoading, query }) => {
  41. if (query) {
  42. cookie.set('token', query.token, { expires: 7 });
  43. }
  44. const goToHomepage = e => {
  45. e.preventDefault();
  46. showLoading();
  47. Router.push('/');
  48. };
  49. const message = query ? (
  50. <Wrapper>
  51. <MessageWrapper>
  52. <Icon src="/images/check.svg" />
  53. <Message>Your account has been verified successfully!</Message>
  54. </MessageWrapper>
  55. <Button icon="arrow-left" onClick={goToHomepage}>
  56. Back to homepage
  57. </Button>
  58. </Wrapper>
  59. ) : (
  60. <MessageWrapper>
  61. <Icon src="/images/x.svg" />
  62. <Message>Invalid verification.</Message>
  63. </MessageWrapper>
  64. );
  65. return <BodyWrapper norenew>{message}</BodyWrapper>;
  66. };
  67. Verify.getInitialProps = ({ store, query }) => {
  68. if (query && query.token) {
  69. store.dispatch(authUser(query.token));
  70. store.dispatch(authRenew());
  71. return { query };
  72. }
  73. return null;
  74. };
  75. Verify.propTypes = {
  76. query: PropTypes.shape({
  77. token: PropTypes.string,
  78. }),
  79. showLoading: PropTypes.func.isRequired,
  80. };
  81. Verify.defaultProps = {
  82. query: null,
  83. };
  84. const mapDispatchToProps = dispatch => ({
  85. showLoading: bindActionCreators(showPageLoading, dispatch),
  86. });
  87. export default withRedux(initialState, null, mapDispatchToProps)(Verify);