verify.js 2.2 KB

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