types#CCVPScreens TypeScript Examples

The following examples show how to use types#CCVPScreens. 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: CCVPNavigator.tsx    From react-native-crypto-wallet-app with MIT License 5 votes vote down vote up
CCVPStack = createStackNavigator<CCVPScreens>()
Example #2
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))}
    />
  );
}