Guest User

Untitled

a guest
Mar 28th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { IContextCore, withContextCore } from "comtouch-context";
  2. import { i18n } from "comtouch-i18n";
  3. import {
  4.   dataValidateAscii,
  5.   dataValidateEmail,
  6.   dataValidateLength,
  7. } from "comtouch-utils";
  8. import {
  9.   Body,
  10.   CheckBox,
  11.   Container,
  12.   Content,
  13.   Form as NbForm,
  14.   ListItem,
  15.   Text,
  16. } from "native-base";
  17. import React, { Component } from "react";
  18. import { Field, Form } from "react-final-form";
  19. import {
  20.   INavRoutes,
  21.   IUiImportance,
  22.   navService,
  23.   themeBrandbook,
  24.   themeForm,
  25.   uiToast,
  26. } from "src/utils";
  27. import { InputButton, InputText } from "../shared";
  28. import { UiSeparator } from "../ui";
  29.  
  30. //
  31. // types
  32. //
  33.  
  34. interface IFormData {
  35.   userEmail: string;
  36.   userPassword: string;
  37.   userName?: string;
  38.   userSurname?: string;
  39. }
  40.  
  41. type IProps = IPropsContext & IPropsOwn;
  42.  
  43. type IPropsContext = Pick<IContextCore, "usersPost">;
  44.  
  45. interface IPropsOwn {}
  46.  
  47. interface IState {
  48.   isAgreementAccepted: boolean;
  49. }
  50.  
  51. //
  52. // view
  53. //
  54.  
  55. class View extends Component<IProps, IState> {
  56.   private static validateStringLength = dataValidateLength(2, 20);
  57.  
  58.   public ["constructor"]: typeof View;
  59.  
  60.   public state: IState = {
  61.     isAgreementAccepted: true,
  62.   };
  63.  
  64.   public render() {
  65.     const {
  66.       constructor: { validateStringLength },
  67.       handleSubmit,
  68.       handleToggleAgreement,
  69.       state: { isAgreementAccepted },
  70.     } = this;
  71.     const {
  72.       checkBox: checkBoxStyle,
  73.       form: formStyle,
  74.       personalDataText: personalDataTextStyle,
  75.     } = style;
  76.  
  77.     return (
  78.       <Container>
  79.         <Content>
  80.           <Form onSubmit={handleSubmit as (values: object) => void}>
  81.             {({ handleSubmit, submitting, valid }) => (
  82.               <NbForm style={formStyle}>
  83.                 <UiSeparator transparent />
  84.                 <Field
  85.                   component={InputText}
  86.                   label={`${i18n._("Email")} *`}
  87.                   name="userEmail"
  88.                   validate={dataValidateEmail}
  89.                 />
  90.                 <Field
  91.                   component={InputText}
  92.                   label={`${i18n._("Password")} *`}
  93.                   name="userPassword"
  94.                   secure
  95.                   validate={dataValidateAscii}
  96.                 />
  97.                 <Field
  98.                   component={InputText}
  99.                   label={i18n._("Name")}
  100.                   name="userName"
  101.                   validate={validateStringLength}
  102.                 />
  103.                 <Field
  104.                   component={InputText}
  105.                   label={i18n._("Surname")}
  106.                   name="userSurname"
  107.                   validate={validateStringLength}
  108.                 />
  109.                 <ListItem style={checkBoxStyle}>
  110.                   <CheckBox
  111.                     checked={isAgreementAccepted}
  112.                     onPress={handleToggleAgreement}
  113.                   />
  114.                   <Body>
  115.                     <Text style={personalDataTextStyle}>
  116.                       {i18n._("Allow to process provided personal data")}
  117.                     </Text>
  118.                   </Body>
  119.                 </ListItem>
  120.                 <InputButton
  121.                   block
  122.                   disabled={!valid || !isAgreementAccepted}
  123.                   loading={submitting}
  124.                   onPress={handleSubmit}
  125.                   text={i18n._("Registration")}
  126.                 />
  127.               </NbForm>
  128.             )}
  129.           </Form>
  130.         </Content>
  131.       </Container>
  132.     );
  133.   }
  134.  
  135.   private handleToggleAgreement = () => {
  136.     this.setState(({ isAgreementAccepted }) => ({
  137.       isAgreementAccepted: !isAgreementAccepted,
  138.     }));
  139.   };
  140.  
  141.   private handleSubmit = async (formData: IFormData) => {
  142.     const {
  143.       props: { usersPost },
  144.     } = this;
  145.     try {
  146.       const user = await usersPost({
  147.         email: formData.userEmail,
  148.         name: formData.userName,
  149.         password: formData.userPassword,
  150.         surname: formData.userSurname,
  151.       });
  152.       uiToast(
  153.         IUiImportance.SUCCESS,
  154.         i18n._("Welcome, {username}!", { username: user.name }),
  155.       );
  156.       navService.navigate({ route: INavRoutes.Main });
  157.     } catch (error) {
  158.       uiToast(IUiImportance.ERROR, error.message);
  159.     }
  160.   };
  161. }
  162.  
  163. //
  164. // style
  165. //
  166.  
  167. const style = {
  168.   checkBox: {
  169.     borderBottomColor: "transparent",
  170.     marginLeft: 0,
  171.   },
  172.   form: {
  173.     marginLeft: themeForm["NativeBase.Item"].marginLeft,
  174.     marginRight: themeForm["NativeBase.Item"].marginLeft,
  175.     paddingBottom: themeBrandbook.Space.Vertical.XXL,
  176.   },
  177.   personalDataText: {
  178.     fontSize: themeBrandbook.Font.Size.S,
  179.   },
  180. };
  181.  
  182. //
  183. // export
  184. //
  185.  
  186. export const Registration = withContextCore<IPropsContext, IPropsOwn>(
  187.   View,
  188.   ({ usersPost }) => ({ usersPost }),
  189. );
Add Comment
Please, Sign In to add comment