feat: impl wrong password prompt

This commit is contained in:
thecodrr
2020-03-07 15:20:01 +05:00
parent dd024d2f03
commit dd40d52870

View File

@@ -1,11 +1,21 @@
import React from "react"; import React, { useState, useRef, useCallback } from "react";
import { Box } from "rebass"; import { Box, Text, Flex } from "rebass";
import { Input } from "@rebass/forms"; import { Input } from "@rebass/forms";
import Dialog, { showDialog } from "./dialog"; import Dialog, { showDialog } from "./dialog";
import * as Icon from "react-feather"; import * as Icon from "react-feather";
function PasswordDialog(props) { function PasswordDialog(props) {
let password = ""; let password = "";
const [isWrong, setIsWrong] = useState(false);
const passwordRef = useRef();
const submit = useCallback(async () => {
if (await props.validate(password)) {
props.onDone();
} else {
setIsWrong(true);
passwordRef.current.focus();
}
}, [setIsWrong, password, props]);
return ( return (
<Dialog <Dialog
isOpen={true} isOpen={true}
@@ -14,16 +24,33 @@ function PasswordDialog(props) {
content={ content={
<Box my={1}> <Box my={1}>
<Input <Input
variant="default" ref={passwordRef}
autoFocus
variant={isWrong ? "error" : "default"}
type="password" type="password"
onChange={e => (password = e.target.value)} onChange={e => (password = e.target.value)}
placeholder="Enter vault password" placeholder="Enter vault password"
onKeyUp={async e => {
if (e.key === "Enter") {
await submit();
} else {
setIsWrong(false);
}
}}
/> />
{isWrong && (
<Flex alignItems="center" color="red" mt={2}>
<Icon.AlertTriangle size={16} />
<Text ml={1} fontSize={"subBody"}>
Wrong password
</Text>
</Flex>
)}
</Box> </Box>
} }
positiveButton={{ positiveButton={{
text: props.positiveButtonText, text: props.positiveButtonText,
onClick: () => props.onDone(password) onClick: submit
}} }}
negativeButton={{ text: "Cancel", onClick: props.onCancel }} negativeButton={{ text: "Cancel", onClick: props.onCancel }}
/> />
@@ -55,15 +82,16 @@ function getDialogData(type) {
} }
} }
export const showPasswordDialog = type => { export const showPasswordDialog = (type, validate) => {
const { title, icon, positiveButtonText } = getDialogData(type); const { title, icon, positiveButtonText } = getDialogData(type);
return showDialog(perform => ( return showDialog(perform => (
<PasswordDialog <PasswordDialog
title={title} title={title}
icon={icon} icon={icon}
positiveButtonText={positiveButtonText} positiveButtonText={positiveButtonText}
onCancel={() => perform()} onCancel={() => perform(false)}
onDone={password => perform(password)} validate={validate}
onDone={() => perform(true)}
/> />
)); ));
}; };