Advertisement
nikpolale

Stormotion Login

Nov 19th, 2019
905
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import auth from '@react-native-firebase/auth';
  2. import {Linking, TouchableWithoutFeedback, View} from 'react-native';
  3. import {SkippedContext} from '../../App';
  4. import HNButton from '../components/HNButton';
  5. import HNTextField from '../components/HNTextField';
  6. import UserWithTabletIcon from '../components/icons/UserWithTabletIcon';
  7. import HeaderText from '../components/texts/HeaderText';
  8. import Roboto from '../components/texts/Roboto';
  9. import SubheaderText from '../components/texts/SubheaderText';
  10. import TwoPartsScreen from '../components/TwoPartsScreen';
  11. import config from '../config';
  12. import {useSetEmailDataMutation} from '../generated/graphql';
  13. import useField from '../hooks/useField';
  14. import colors from '../theme/colors';
  15. import strings from '../theme/strings';
  16. import getSubmitDisabled from '../utils/getSubmitDisabled';
  17. import React, {
  18.   Fragment,
  19.   useCallback,
  20.   useContext,
  21.   useState,
  22.   useMemo,
  23. } from 'react';
  24.  
  25. interface Props {}
  26.  
  27. const Login: React.FunctionComponent<Props> = () => {
  28.   const [email, onChangeText, error] = useField('', 'email');
  29.   const [loginPressed, setLoginPressed] = useState(false);
  30.   const [setEmail] = useSetEmailDataMutation();
  31.  
  32.   const onPressLogin = useCallback(() => {
  33.     setLoginPressed(true);
  34.     setEmail({variables: {emailData: email}});
  35.     auth().sendSignInLinkToEmail(email, {
  36.       handleCodeInApp: true,
  37.       iOS: {bundleId: config.ios.bundleId},
  38.       android: {packageName: config.android.packageName},
  39.       url: config.auth.deepLinkUrl,
  40.     });
  41.   }, [auth, email, setEmail]);
  42.  
  43.   const loginDisabled = useMemo(
  44.     () => getSubmitDisabled(error) || loginPressed,
  45.     [error, loginPressed],
  46.   );
  47.  
  48.   const {setSkippedAuth} = useContext(SkippedContext);
  49.   const onPressLater = useCallback(() => {
  50.     setSkippedAuth(true);
  51.   }, [setSkippedAuth]);
  52.  
  53.   return (
  54.     <View style={{flex: 1}}>
  55.       <TwoPartsScreen
  56.        topComponent={
  57.          <View
  58.            style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
  59.             <UserWithTabletIcon />
  60.           </View>
  61.         }
  62.         centerComponent={
  63.           <Fragment>
  64.             {!loginPressed && (
  65.               <HNButton
  66.                 useShadow
  67.                 font="bold"
  68.                 title={strings.auth.signUpLater}
  69.                 onPress={onPressLater}
  70.                 disabledStyle={{backgroundColor: `${colors.primary}50`}}
  71.                 disabledTitleStyle={{color: 'white'}}
  72.               />
  73.             )}
  74.           </Fragment>
  75.         }>
  76.         <View
  77.          style={{
  78.            flex: 1,
  79.            justifyContent: 'space-between',
  80.            paddingVertical: 20,
  81.            paddingHorizontal: 40,
  82.          }}>
  83.           <FieldWithDescription
  84.             loginPressed={loginPressed}
  85.             value={email}
  86.             onChangeText={onChangeText}
  87.             error={error}
  88.           />
  89.           <Buttons onPressLogin={onPressLogin} loginDisabled={loginDisabled} />
  90.         </View>
  91.       </TwoPartsScreen>
  92.     </View>
  93.   );
  94. };
  95.  
  96. interface FieldWithDescriptionProps {
  97.   loginPressed: boolean;
  98.   value: string;
  99.   onChangeText: (text: string) => void;
  100.   error?: boolean;
  101. }
  102. const FieldWithDescription: React.FunctionComponent<FieldWithDescriptionProps> = React.memo(
  103.   ({loginPressed, value, onChangeText, error}) => {
  104.     const onPressLink = useCallback(() => {
  105.       Linking.openURL(strings.auth.privacyLink);
  106.     }, []);
  107.  
  108.     return (
  109.       <View style={{flex: 1, justifyContent: 'center'}}>
  110.         {loginPressed ? (
  111.           <HeaderText>{strings.auth.weSentEmail}</HeaderText>
  112.         ) : (
  113.           <Fragment>
  114.             <HeaderText style={{marginBottom: 24}}>
  115.               {strings.auth.enterEmail}
  116.             </HeaderText>
  117.             <HNTextField
  118.               value={value}
  119.               keyboardType="email-address"
  120.               onChangeText={onChangeText}
  121.               placeholder={strings.auth.email}
  122.               errorText={strings.auth.emailError}
  123.               error={error}
  124.             />
  125.             <SubheaderText color={colors.primaryLight}>
  126.               {strings.auth.passwordWilCome}
  127.             </SubheaderText>
  128.             <SubheaderText style={{marginTop: 8}} color={colors.primaryLight}>
  129.               {strings.auth.privacyInfo}
  130.             </SubheaderText>
  131.             <TouchableWithoutFeedback onPress={onPressLink}>
  132.               <Roboto textAlign="center" fontSize={14} color={colors.primary}>
  133.                 {strings.auth.privacy}
  134.               </Roboto>
  135.             </TouchableWithoutFeedback>
  136.           </Fragment>
  137.         )}
  138.       </View>
  139.     );
  140.   },
  141. );
  142.  
  143. interface ButtonsProps {
  144.   onPressLogin: () => void;
  145.   loginDisabled: boolean;
  146. }
  147. const Buttons: React.FunctionComponent<ButtonsProps> = React.memo(
  148.   ({onPressLogin, loginDisabled}) => {
  149.     return (
  150.       <HNButton
  151.         useShadow
  152.         font="bold"
  153.         onPress={onPressLogin}
  154.         title={strings.auth.login}
  155.         disabled={loginDisabled}
  156.         disabledStyle={{backgroundColor: `${colors.primary}50`}}
  157.         disabledTitleStyle={{color: 'white'}}
  158.       />
  159.     );
  160.   },
  161. );
  162.  
  163. export default React.memo(Login);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement