{"size": 2831, "ext": "js", "max_stars_repo_path": "src/screens/Settings/General/ConfirmPassword.js", "max_stars_repo_name": "PinkDiamond1/ledger-live-mobile", "max_stars_repo_stars_event_min_datetime": "2022-01-21T23:16:19.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-21T23:16:19.000Z", "max_issues_repo_path": "src/screens/Settings/General/ConfirmPassword.js", "max_issues_repo_name": "PinkDiamond1/ledger-live-mobile", "max_issues_repo_issues_event_min_datetime": "2022-02-15T08:39:51.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T23:40:00.000Z", "max_forks_repo_path": "src/screens/Settings/General/ConfirmPassword.js", "max_forks_repo_name": "PinkDiamond1/ledger-live-mobile", "max_forks_repo_forks_event_min_datetime": "2021-11-21T11:41:36.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-21T11:41:36.000Z"}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": 1, "avg_line_length": 25.2767857143, "max_line_length": 72, "alphanum_fraction": 0.6220416814}
/* @flow */
import React, { PureComponent } from "react";
import { Platform, Vibration } from "react-native";
import * as Keychain from "react-native-keychain";
import { connect } from "react-redux";
import { compose } from "redux";
import { withTranslation } from "react-i18next";
import { PasswordsDontMatchError } from "@ledgerhq/errors";

import { setPrivacy } from "../../../actions/settings";
import type { Privacy } from "../../../reducers/settings";
import type { T } from "../../../types/common";
import PasswordForm from "./PasswordForm";
import { VIBRATION_PATTERN_ERROR } from "../../../constants";

type Props = {
  t: T,
  setPrivacy: (privacy: Privacy) => void,
  navigation: any,
  route: any,
};

type State = {
  password: string,
  confirmPassword: string,
  error: ?Error,
  biometricsType?: string,
};

const mapDispatchToProps = {
  setPrivacy,
};

class ConfirmPassword extends PureComponent<Props, State> {
  componentDidMount() {
    Keychain.getSupportedBiometryType().then(biometricsType => {
      if (biometricsType) this.setState({ biometricsType });
    });
  }

  constructor({ route }) {
    super();
    const password = route.params?.password;
    this.state = {
      password,
      confirmPassword: "",
      error: null,
    };
  }

  onChange = (confirmPassword: string) => {
    this.setState({ confirmPassword, error: null });
  };

  async save() {
    const { password, biometricsType } = this.state;
    const { setPrivacy, navigation } = this.props;
    const options =
      Platform.OS === "ios"
        ? {}
        : {
            accessControl: Keychain.ACCESS_CONTROL.APPLICATION_PASSWORD,
            rules: Keychain.SECURITY_RULES.NONE,
          };
    try {
      await Keychain.setGenericPassword("ledger", password, options);
      setPrivacy({
        biometricsType,
        biometricsEnabled: false,
      });
      const n = navigation.getParent();
      if (n) n.goBack();
    } catch (err) {
      // eslint-disable-next-line no-console
      console.log("could not save credentials");
    }
  }

  onSubmit = () => {
    if (!this.state.password) return;
    if (this.state.password === this.state.confirmPassword) {
      this.save();
    } else {
      Vibration.vibrate(VIBRATION_PATTERN_ERROR);
      this.setState({
        error: new PasswordsDontMatchError(),
        confirmPassword: "",
      });
    }
  };

  render() {
    const { t } = this.props;
    const { error, confirmPassword } = this.state;
    return (
      <PasswordForm
        placeholder={t("auth.confirmPassword.placeholder")}
        onChange={this.onChange}
        onSubmit={this.onSubmit}
        error={error}
        value={confirmPassword}
      />
    );
  }
}

export default compose(
  // $FlowFixMe
  connect(null, mapDispatchToProps),
  withTranslation(),
)(ConfirmPassword);
