utils#useAlert TypeScript Examples

The following examples show how to use utils#useAlert. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Overview.tsx    From react-native-crypto-wallet-app with MIT License 6 votes vote down vote up
Overview = () => {
  const alert = useAlert();
  const checkIfNewUser = useCallback(async () => {
    try {
      const isNewUser = Boolean(await AsyncStorage.getItem('isNewUser'));
      if (isNewUser) {
        alert('You are a new user!');
      } else {
        alert('You are an existing user!');
      }
    } catch (error) {}
  }, [alert]);

  useEffect(() => {
    checkIfNewUser();
  }, [checkIfNewUser]);

  const handleSignOut = async () => {
    await auth().signOut();
  };

  return (
    <Background>
      <StyledButton variant="positive" label="Sign out" onPress={handleSignOut} />
    </Background>
  );
}
Example #2
Source File: Login.tsx    From react-native-crypto-wallet-app with MIT License 5 votes vote down vote up
SignUp = ({ navigation }: StackNavigationProps<SignUpScreens, 'SignUp'>) => {
  const [loading, setLoading] = useState<boolean>(false);
  const [email, setEmail] = useState<string>('');
  const [password, setPassword] = useState<string>('');
  const [showPassword, setShowPassword] = useState<boolean>(false);
  const alert = useAlert();

  const { height } = Dimensions.get('window');

  const isEmailValid = email.length > 0 ? validateEmailAddress(email) : undefined;
  const isPasswordValid = password.length > 0 ? validatePassword(password) : undefined;
  const isButtonDisabled = [isEmailValid, isPasswordValid].some(c => !c) || loading;

  const handleLogin = async () => {
    setLoading(true);
    try {
      const res = await auth().signInWithEmailAndPassword(email, password);
      if (res) {
        const { additionalUserInfo } = res;
        if (additionalUserInfo) {
          const { isNewUser } = additionalUserInfo;
          await AsyncStorage.setItem('isNewUser', `${isNewUser}`);
        }
      }
    } catch (error) {
      if (error.code === 'auth/user-not-found') {
        setLoading(false);
        alert(
          'Error',
          'The email and password you entered did not match our records. Please double check and try again.',
        );
      }
    }
  };

  return (
    <Background>
      <Header {...{ navigation }} title="Welcome Back!" colorMode="dark" />
      <Box alignItems="center" justifyContent="flex-end" height={height * 0.38}>
        <Illustration name="login" width="308" height={`${(height * 28) / 100}`} />
      </Box>
      <AuthForm
        {...{
          email,
          password,
          isEmailValid,
          isPasswordValid,
          loading,
          isButtonDisabled,
          showPassword,
        }}
        onEmailChange={v => setEmail(v)}
        onPasswordChange={v => setPassword(v)}
        onShowPasswordPress={() => setShowPassword(s => !s)}
        onNavigationToLoginOrSignUp={() => navigation.navigate('SignUp')}
        onSignUpPress={handleLogin}
        onForgotPasswordPress={() => true}
        submitButtonLabel="Login"
        bottomSectionLightTextLabel="Don't have an account?"
        bottomSectionAccentTextLabel="Sign Up"
      />
    </Background>
  );
}
Example #3
Source File: SignUp.tsx    From react-native-crypto-wallet-app with MIT License 5 votes vote down vote up
SignUp = ({ navigation }: StackNavigationProps<SignUpScreens, 'SignUp'>) => {
  const [loading, setLoading] = useState<boolean>(false);
  const [email, setEmail] = useState<string>('');
  const [password, setPassword] = useState<string>('');
  const [showPassword, setShowPassword] = useState<boolean>(false);
  const alert = useAlert();

  const { height } = Dimensions.get('window');

  const isEmailValid = email.length > 0 ? validateEmailAddress(email) : undefined;
  const isPasswordValid = password.length > 0 ? validatePassword(password) : undefined;
  const isButtonDisabled = [isEmailValid, isPasswordValid].some(c => !c) || loading;

  const handleNavigationToLogin = () => {
    navigation.navigate('Login');
  };

  const handleSignUp = async () => {
    setLoading(true);
    try {
      const res = await auth().createUserWithEmailAndPassword(email, password);
      if (res) {
        const { additionalUserInfo } = res;
        if (additionalUserInfo) {
          const { isNewUser } = additionalUserInfo;
          await AsyncStorage.setItem('isNewUser', `${isNewUser}`);
        }
      }
    } catch (error) {
      if (error.code === 'auth/email-already-in-use') {
        setLoading(false);
        alert('Error', 'The email address is already in use by another account.', [
          { text: 'Login instead', onPress: handleNavigationToLogin },
        ]);
      }
    }
  };

  return (
    <Background>
      <Header {...{ navigation }} title="Create account" />
      <Box alignItems="center" justifyContent="flex-end" height={height * 0.37}>
        <Illustration name="office" width="308" height="171" />
      </Box>
      <AuthForm
        {...{
          email,
          password,
          isEmailValid,
          isPasswordValid,
          loading,
          isButtonDisabled,
          showPassword,
        }}
        isSignUp
        onEmailChange={v => setEmail(v)}
        onPasswordChange={v => setPassword(v)}
        onShowPasswordPress={() => setShowPassword(s => !s)}
        onNavigationToLoginOrSignUp={handleNavigationToLogin}
        onSignUpPress={handleSignUp}
        submitButtonLabel="Let's get started"
        bottomSectionLightTextLabel="Already have an account?"
        bottomSectionAccentTextLabel="Login"
      />
    </Background>
  );
}
Example #4
Source File: CreateConfirmVerifyPin.tsx    From react-native-crypto-wallet-app with MIT License 4 votes vote down vote up
CreateConfirmVerifyPin = ({
  navigation,
}: StackNavigationProps<CCVPScreens, 'CreateConfirmVerifyPin'>) => {
  const [pin, setPin] = useState<string | null>(null);
  const [pinEntry, setPinEntry] = useState<string>('');
  const [createdPin, setCreatedPin] = useState<string>('');
  const [loading, setLoading] = useState<boolean>(false);

  const alert = useAlert();
  const isLogin = pin !== null;
  const isConfirm = createdPin.length === 4;

  const checkIfPinExists = useCallback(async () => {
    setLoading(true);
    try {
      const savedPin = await AsyncStorage.getItem('pin');
      setPin(savedPin);
    } catch (error) {
      alert('Error', error);
    } finally {
      setLoading(false);
    }
  }, [alert]);

  const handleNavigateHome = useCallback(() => {
    navigation.replace('Home');
  }, [navigation]);

  const savePin = useCallback(async () => {
    await AsyncStorage.setItem('pin', pinEntry, () => {
      handleNavigateHome();
    });
    setLoading(false);
  }, [handleNavigateHome, pinEntry]);

  const handleInvalidPin = useCallback(() => {
    setLoading(false);
    setPinEntry('');
    alert('Invalid PIN', 'Please try again.');
  }, [alert]);

  const handlePinEntry = (v: string) => {
    if (!isLogin && !isConfirm) {
      setCreatedPin(p => p.concat(v));
    } else {
      setPinEntry(p => p.concat(v));
    }
  };

  const handlePinEntryFinish = useCallback(() => {
    if (pinEntry.length === 4) {
      setLoading(true);
      if (isLogin) {
        if (pinEntry === pin) {
          handleNavigateHome();
        } else {
          handleInvalidPin();
        }
      } else if (isConfirm) {
        if (pinEntry === createdPin) {
          savePin();
        } else {
          handleInvalidPin();
        }
      } else {
        setLoading(false);
      }
    }
  }, [
    createdPin,
    handleInvalidPin,
    handleNavigateHome,
    isConfirm,
    isLogin,
    pin,
    pinEntry,
    savePin,
  ]);

  return (
    <PinLayout
      {...{ pinEntry, loading, isLogin, isConfirm }}
      onPinChange={handlePinEntry}
      onCheckIfPinExists={checkIfPinExists}
      onPinEntryFinished={handlePinEntryFinish}
      onPinDelete={() => setPinEntry(p => p.slice(1, p.length))}
    />
  );
}