PinUnlockWithPassword.tsx 3.33 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

import React, { useState } from 'react';

18
import { KeyboardAwareContainer } from 'modules/unlock/components/Container';
19
20
21
22
23
24
import PinInput from 'modules/unlock/components/PinInput';
import { usePinState } from 'modules/unlock/hooks';
import t from 'modules/unlock/strings';
import { getSubtitle, onPinInputChange } from 'modules/unlock/utils';
import testIDs from 'e2e/testIDs';
import ScreenHeading from 'components/ScreenHeading';
25
import { NavigationTargetIdentityProps } from 'types/props';
26
27
import { withAccountStore, withTargetIdentity } from 'utils/HOC';
import { useSeedRef } from 'utils/seedRefHooks';
28
import Button from 'components/Button';
29
30

function PinUnlockWithPassword({
31
	targetIdentity,
32
	route
33
}: NavigationTargetIdentityProps<'PinUnlockWithPassword'>): React.ReactElement {
34
35
	const [state, updateState, resetState] = usePinState();
	const [focusPassword, setFocusPassword] = useState<boolean>(false);
36
	const { createSeedRef } = useSeedRef(targetIdentity.encryptedSeed);
37
38
39

	async function submit(): Promise<void> {
		const { pin, password } = state;
40
		const resolvePassword = route.params.resolve;
41
42
43
44
		if (!route.params.isSeedRefValid) {
			if (pin.length >= 6 && targetIdentity) {
				try {
					await createSeedRef(pin);
45
					resolvePassword(password);
46
47
48
49
50
51
52
					resetState();
				} catch (e) {
					updateState({ password: '', pin: '', pinMismatch: true });
					//TODO record error times;
				}
			} else {
				updateState({ pinTooShort: true });
53
54
			}
		} else {
Hanwen Cheng's avatar
Hanwen Cheng committed
55
			resolvePassword(password);
56
			resetState();
57
58
59
60
61
62
63
64
65
66
67
		}
	}

	function onPasswordInputChange(password: string): void {
		updateState({
			password,
			pinMismatch: false
		});
	}

	return (
68
69
70
71
72
		<KeyboardAwareContainer
			contentContainerStyle={{
				flexGrow: 1
			}}
		>
73
74
75
76
77
			<ScreenHeading
				title={t.title.pinUnlock}
				error={state.pinMismatch || state.pinTooShort}
				subtitle={getSubtitle(state, true)}
			/>
78
79
80
81
82
83
84
85
86
87
88
			{!route.params.isSeedRefValid && (
				<PinInput
					label={t.pinLabel}
					autoFocus
					testID={testIDs.IdentityPin.unlockPinInput}
					returnKeyType="done"
					onChangeText={onPinInputChange('pin', updateState)}
					onSubmitEditing={(): void => setFocusPassword(true)}
					value={state.pin}
				/>
			)}
89
90
91
92
			<PinInput
				label={t.passwordLabel}
				testID={testIDs.IdentityPin.passwordInput}
				returnKeyType="done"
93
				keyboardType="default"
94
95
96
97
98
				focus={focusPassword}
				onChangeText={onPasswordInputChange}
				onSubmitEditing={submit}
				value={state.password}
			/>
99
			<Button
100
101
102
103
				title={t.doneButton.pinUnlock}
				onPress={submit}
				testID={testIDs.IdentityPin.unlockPinButton}
			/>
104
		</KeyboardAwareContainer>
105
106
107
	);
}

108
export default withAccountStore(withTargetIdentity(PinUnlockWithPassword));