Advertisement
GeoDato

react sample

Feb 6th, 2021
1,281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { FC, useEffect, useState } from 'react';
  2. import styled from 'styled-components';
  3. import { AuthWrapper } from '../../../../components/wrapper';
  4. import { LinkText } from '../../../../components/ui';
  5. import * as Yup from 'yup';
  6. import Validation from '../../../../services/validation';
  7. import { Formik, Form, Field } from 'formik';
  8. import FormikField from '../../../../components/ui/FormikField/FormikField';
  9. import { Http } from '../../../../services/Http';
  10. import Storage from '../../../../services/Storage';
  11. import ROUTES from '../../../../routes/routes';
  12. import { UserInterface } from '../../../../constants/interfaces/User';
  13. import { MAX_SCENARIO_LOGO_SIZE } from '../../../../constants/enums/scenarios';
  14. import { Button } from '../../../../components/ui/Button';
  15. import { getRawNumber } from '../../../../helpers/functions/phone-number';
  16. import { LocationState, useHistory } from '../../../../tools/router';
  17. import { useLocation } from 'react-router';
  18. import {
  19.     useResetProfileService,
  20.     useTestInvitationService,
  21. } from '../../store/services';
  22. import { useUploadPhotoService } from '../../../UserProfile/store/services';
  23. import { useShowMessage } from '../../../../components/ui/ErrorMessages/ErrorMessages';
  24. import { useSelector } from 'react-redux';
  25. import { getProfileState } from '../../../UserProfile/store/reducers';
  26. import WebPhotoUpload from '../components/WebUpload';
  27. import { ROLE_NAMES } from '../../../../constants/enums';
  28. import { Variables } from '../../../../theme/variables';
  29. const qs = require('query-string');
  30.  
  31. const StyledRegisterContent = styled.div``;
  32. const StyledUploadPhotoSection = styled.div`
  33.     margin-bottom: 14px;
  34. `;
  35.  
  36. export const StyledInputDivs = styled.div<{ marginBottom?: string }>`
  37.     display: flex;
  38.     justify-content: space-between;
  39.     flex-wrap: wrap;
  40.     > div,
  41.     p {
  42.         flex: 1;
  43.         margin-left: 12px;
  44.         margin-right: 12px;
  45.         margin-bottom: ${props =>
  46.             !!props.marginBottom ? props.marginBottom : '16px'};
  47.         min-width: 288px;
  48.     }
  49. `;
  50.  
  51. const StyledField = styled(Field)`
  52.     flex: 1;
  53.     min-width: 100px;
  54. `;
  55.  
  56. const StyledSpan = styled.span`
  57.     margin-right: 11px;
  58. `;
  59.  
  60. export const StyledActionsDivs = styled.div<{ break?: boolean }>`
  61.     display: flex;
  62.     justify-content: space-between;
  63.     align-items: center;
  64.     margin-top: ${props => (props.break ? '48px' : '65px')};
  65.     padding: 0px 12px;
  66.     flex-wrap: wrap-reverse;
  67.     flex-direction: ${props => (props.break ? 'column-reverse' : 'row')};
  68. `;
  69.  
  70. export const StyledActionsLinks = styled.div<{ break?: boolean }>`
  71.     display: flex;
  72.     flex-direction: column;
  73.     justify-content: ${props => (props.break ? 'center' : 'flex-start')};
  74.     margin-bottom: 24px;
  75. `;
  76.  
  77. export const StyledCreateButton = styled.div<{ break?: boolean }>`
  78.     display: flex;
  79.     justify-content: ${props => (props.break ? 'center' : 'flex-end')};
  80.     margin-bottom: 24px;
  81. `;
  82.  
  83. export const StyledActionsText = styled.div`
  84.     font-family: ${props => props.theme.Fonts.manrope};
  85.     font-size: 15px;
  86.     font-weight: bold;
  87.     font-stretch: normal;
  88.     font-style: normal;
  89.     line-height: normal;
  90.     letter-spacing: normal;
  91.     margin-left: 11px;
  92.     color: ${props => props.theme.Colors.white};
  93. `;
  94.  
  95. export const StyledNotice = styled.p`
  96.     font-family: ${props => props.theme.Fonts.manrope};
  97.     font-size: 13px;
  98.     font-weight: normal;
  99.     font-stretch: normal;
  100.     font-style: normal;
  101.     line-height: 1.45;
  102.     letter-spacing: normal;
  103.     margin: 0 22px !important;
  104.     color: ${props => props.theme.Colors.paleGrey};
  105. `;
  106.  
  107. const RegisterSchema = Yup.object().shape({
  108.     firstName: Validation.firstName,
  109.     lastName: Validation.lastName,
  110.     email: Validation.email,
  111.     password: Validation.password,
  112.     phoneNumber: Validation.phoneNumber.required('Mobile Number is required'),
  113.     invitationCode: Validation.invitationCode.required(
  114.         'Invitation Code is required'
  115.     ),
  116. });
  117.  
  118. const WebView: FC = () => {
  119.     const profile: UserInterface | undefined = useSelector(getProfileState);
  120.  
  121.     const history = useHistory();
  122.     const handleFileUpload = useUploadPhotoService();
  123.     const handleTestInvitation = useTestInvitationService();
  124.     const handleResetProfile = useResetProfileService();
  125.  
  126.     const location = useLocation<LocationState>();
  127.     const showMessage = useShowMessage();
  128.     const formRef: any = React.createRef();
  129.  
  130.     const [currentImage, setCurrentImage] = useState({
  131.         url: '',
  132.         files: '',
  133.     });
  134.  
  135.     const [invite] = useState(
  136.         qs.parse(location.search).code ? qs.parse(location.search).code : null
  137.     );
  138.  
  139.     useEffect(() => {
  140.         if (invite) {
  141.             handleTestInvitation(invite);
  142.         }
  143.         return () => {
  144.             handleResetProfile();
  145.         };
  146.     }, [invite, handleTestInvitation, handleResetProfile]);
  147.  
  148.     const loginAction = (): void => {
  149.         history.push('/login');
  150.     };
  151.  
  152.     const handleUpload = (e: any) => {
  153.         e.persist();
  154.         let files = e.target.files[0];
  155.         if (!files) return;
  156.         if (files.size >= MAX_SCENARIO_LOGO_SIZE) {
  157.             showMessage('File size must be less than 10mb', 'error');
  158.             return;
  159.         } else if (!files.type.includes('image')) {
  160.             showMessage('File must be an image', 'error');
  161.             return;
  162.         }
  163.         setCurrentImage({
  164.             url: files ? URL.createObjectURL(files) : '',
  165.             files: files ? files : '',
  166.         });
  167.     };
  168.  
  169.     const checkIfEmailIsUnique = (email: string) => {
  170.         /*const state =
  171.             formRef && formRef.current && formRef.current.state
  172.                 ? formRef.current.state
  173.                 : null;
  174.  
  175.         console.log(state, 'state');
  176.         state.errors.phoneNumber = 'bleee';*/
  177.  
  178.         return false;
  179.     };
  180.  
  181.     return (
  182.         <AuthWrapper width="760px" header="Create Your Dashboard Account">
  183.             <StyledRegisterContent>
  184.                 <StyledUploadPhotoSection>
  185.                     <WebPhotoUpload
  186.                         handleChange={handleUpload}
  187.                         currentImage={currentImage.url}
  188.                     />
  189.                 </StyledUploadPhotoSection>
  190.                 <Formik
  191.                     ref={formRef}
  192.                     initialValues={{
  193.                         firstName:
  194.                             profile && profile.firstName
  195.                                 ? profile.firstName
  196.                                 : '',
  197.                         lastName:
  198.                             profile && profile.lastName ? profile.lastName : '',
  199.                         password: '',
  200.                         phoneNumber: undefined,
  201.                         email: profile && profile.email ? profile.email : '',
  202.                         invitationCode:
  203.                             profile && profile.invitationCode
  204.                                 ? profile && profile.invitationCode
  205.                                 : '',
  206.                     }}
  207.                     enableReinitialize={true}
  208.                     onSubmit={(values, { setFieldError }) => {
  209.                         const registerValues = {
  210.                             firstName: values.firstName,
  211.                             lastName: values.lastName,
  212.                             email:
  213.                                 profile && profile.email
  214.                                     ? profile.email
  215.                                     : values.email,
  216.                             password: values.password,
  217.                             phoneNumber:
  218.                                 values.phoneNumber &&
  219.                                 getRawNumber(values.phoneNumber),
  220.                             invitationCode:
  221.                                 profile && profile.invitationCode
  222.                                     ? profile.invitationCode
  223.                                     : values.invitationCode,
  224.                         };
  225.                         const result = checkIfEmailIsUnique(
  226.                             registerValues.email
  227.                         );
  228.                         setFieldError('phoneNumber', 'TODO: implement this');
  229.                         if (!result) return;
  230.  
  231.                         Http.register(registerValues).then((response: any) => {
  232.                             if (response.status === 'error') {
  233.                                 showMessage(response.message, 'error');
  234.                             } else {
  235.                                 if (
  236.                                     response.user.role.name ===
  237.                                     ROLE_NAMES.TRAINEE
  238.                                 ) {
  239.                                     history.push(
  240.                                         `/trainee-splash/${response.user.firstName}`
  241.                                     );
  242.                                 } else {
  243.                                     Storage.set('token', response.token);
  244.                                     if (currentImage.files !== '') {
  245.                                         const formData = new FormData();
  246.                                         formData.set(
  247.                                             'picture',
  248.                                             currentImage.files
  249.                                         );
  250.                                         handleFileUpload(formData);
  251.                                     }
  252.                                     history.push(ROUTES.PROGRESS);
  253.                                 }
  254.                             }
  255.                         });
  256.                     }}
  257.                     validationSchema={RegisterSchema}
  258.                 >
  259.                     {({ values, handleChange }) => (
  260.                         <Form>
  261.                             <StyledInputDivs>
  262.                                 <StyledField
  263.                                     type="text"
  264.                                     disabled={
  265.                                         !!profile && profile.invitationCode
  266.                                     }
  267.                                     component={FormikField}
  268.                                     name="invitationCode"
  269.                                     label="Invite Code"
  270.                                     colorTheme="darkThree"
  271.                                     fontSize="15px"
  272.                                     labelFontSize="15px"
  273.                                     height="64px"
  274.                                     labelFontWeight={600}
  275.                                     errorPadding="8px 5px 0 10px"
  276.                                 />
  277.  
  278.                                 <StyledField
  279.                                     type="email"
  280.                                     disabled={!!profile && profile.email}
  281.                                     component={FormikField}
  282.                                     name="email"
  283.                                     label="Email Address"
  284.                                     colorTheme="darkThree"
  285.                                     fontSize="15px"
  286.                                     labelFontSize="15px"
  287.                                     height="64px"
  288.                                     labelFontWeight={600}
  289.                                     errorPadding="8px 5px 0 10px"
  290.                                 />
  291.                             </StyledInputDivs>
  292.                             <StyledInputDivs>
  293.                                 <StyledField
  294.                                     type="text"
  295.                                     component={FormikField}
  296.                                     name="firstName"
  297.                                     label="First Name"
  298.                                     colorTheme="darkThree"
  299.                                     fontSize="15px"
  300.                                     labelFontSize="15px"
  301.                                     height="64px"
  302.                                     labelFontWeight={600}
  303.                                     errorPadding="8px 5px 0 10px"
  304.                                 />
  305.  
  306.                                 <StyledField
  307.                                     type="text"
  308.                                     component={FormikField}
  309.                                     name="lastName"
  310.                                     label="Last Name"
  311.                                     colorTheme="darkThree"
  312.                                     fontSize="15px"
  313.                                     labelFontSize="15px"
  314.                                     height="64px"
  315.                                     labelFontWeight={600}
  316.                                     errorPadding="8px 5px 0 10px"
  317.                                 />
  318.                             </StyledInputDivs>
  319.                             <StyledInputDivs marginBottom="9px">
  320.                                 <StyledField
  321.                                     type="phone"
  322.                                     component={FormikField}
  323.                                     name="phoneNumber"
  324.                                     autoComplete="new-phone"
  325.                                     label="Mobile Number"
  326.                                     colorTheme="darkThree"
  327.                                     value={values.phoneNumber}
  328.                                     fontSize="15px"
  329.                                     labelFontSize="15px"
  330.                                     height="64px"
  331.                                     labelFontWeight={600}
  332.                                     errorPadding="8px 5px 0 10px"
  333.                                 />
  334.  
  335.                                 <StyledField
  336.                                     type="password"
  337.                                     autoComplete="new-password"
  338.                                     component={FormikField}
  339.                                     name="password"
  340.                                     label="Password"
  341.                                     colorTheme="darkThree"
  342.                                     fontSize="15px"
  343.                                     labelFontSize="15px"
  344.                                     height="64px"
  345.                                     labelFontWeight={600}
  346.                                     errorPadding="8px 5px 0 10px"
  347.                                 />
  348.                             </StyledInputDivs>
  349.                             <StyledInputDivs>
  350.                                 <StyledNotice>
  351.                                     We will send an SMS to verify your mobile
  352.                                     number. Carrier fees may apply.
  353.                                 </StyledNotice>
  354.  
  355.                                 <StyledNotice>
  356.                                     Password must be 8 characters long and
  357.                                     include at least one special character and
  358.                                     number.
  359.                                 </StyledNotice>
  360.                             </StyledInputDivs>
  361.                             <StyledActionsDivs>
  362.                                 <StyledActionsLinks>
  363.                                     <StyledActionsText>
  364.                                         <StyledSpan>
  365.                                             Already have an account?
  366.                                         </StyledSpan>
  367.                                         <LinkText
  368.                                             fontSize={15}
  369.                                             color={Variables.Colors.softBlue}
  370.                                             onClick={loginAction}
  371.                                         >
  372.                                             Log In
  373.                                         </LinkText>
  374.                                     </StyledActionsText>
  375.                                 </StyledActionsLinks>
  376.                                 <StyledCreateButton>
  377.                                     <Button
  378.                                         type="submit"
  379.                                         width="232px"
  380.                                         fontSize={15}
  381.                                     >
  382.                                         Create Account
  383.                                     </Button>
  384.                                 </StyledCreateButton>
  385.                             </StyledActionsDivs>
  386.                         </Form>
  387.                     )}
  388.                 </Formik>
  389.             </StyledRegisterContent>
  390.         </AuthWrapper>
  391.     );
  392. };
  393.  
  394. export default WebView;
  395.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement