2020-03-07 15:20:01 +05:00
|
|
|
import React, { useState, useRef, useCallback } from "react";
|
|
|
|
|
import { Box, Text, Flex } from "rebass";
|
2020-03-07 12:32:17 +05:00
|
|
|
import { Input } from "@rebass/forms";
|
|
|
|
|
import Dialog, { showDialog } from "./dialog";
|
2020-03-17 09:09:14 +05:00
|
|
|
import * as Icon from "../icons";
|
2020-03-07 12:32:17 +05:00
|
|
|
|
|
|
|
|
function PasswordDialog(props) {
|
2020-03-07 15:20:01 +05:00
|
|
|
const [isWrong, setIsWrong] = useState(false);
|
|
|
|
|
const passwordRef = useRef();
|
|
|
|
|
const submit = useCallback(async () => {
|
2020-03-07 19:41:22 +05:00
|
|
|
const password = passwordRef.current.value;
|
2020-03-07 15:20:01 +05:00
|
|
|
if (await props.validate(password)) {
|
|
|
|
|
props.onDone();
|
|
|
|
|
} else {
|
|
|
|
|
setIsWrong(true);
|
|
|
|
|
passwordRef.current.focus();
|
|
|
|
|
}
|
2020-03-07 19:41:22 +05:00
|
|
|
}, [setIsWrong, props]);
|
2020-03-07 12:32:17 +05:00
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
isOpen={true}
|
|
|
|
|
title={props.title}
|
|
|
|
|
icon={props.icon}
|
|
|
|
|
content={
|
|
|
|
|
<Box my={1}>
|
|
|
|
|
<Input
|
2020-03-07 15:20:01 +05:00
|
|
|
ref={passwordRef}
|
|
|
|
|
autoFocus
|
|
|
|
|
variant={isWrong ? "error" : "default"}
|
2020-03-07 12:32:17 +05:00
|
|
|
type="password"
|
|
|
|
|
placeholder="Enter vault password"
|
2020-03-07 15:20:01 +05:00
|
|
|
onKeyUp={async e => {
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
await submit();
|
|
|
|
|
} else {
|
|
|
|
|
setIsWrong(false);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2020-03-07 12:32:17 +05:00
|
|
|
/>
|
2020-03-07 15:20:01 +05:00
|
|
|
{isWrong && (
|
2020-03-17 09:09:14 +05:00
|
|
|
<Flex alignItems="center" color="error" mt={2}>
|
|
|
|
|
<Icon.Alert size={16} color="error" />
|
2020-03-07 15:20:01 +05:00
|
|
|
<Text ml={1} fontSize={"subBody"}>
|
|
|
|
|
Wrong password
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
|
|
|
|
)}
|
2020-03-07 12:32:17 +05:00
|
|
|
</Box>
|
|
|
|
|
}
|
|
|
|
|
positiveButton={{
|
|
|
|
|
text: props.positiveButtonText,
|
2020-03-07 15:20:01 +05:00
|
|
|
onClick: submit
|
2020-03-07 12:32:17 +05:00
|
|
|
}}
|
|
|
|
|
negativeButton={{ text: "Cancel", onClick: props.onCancel }}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDialogData(type) {
|
|
|
|
|
switch (type) {
|
|
|
|
|
case "create_vault":
|
|
|
|
|
return {
|
|
|
|
|
title: "Set Up Your Vault",
|
2020-03-17 09:09:14 +05:00
|
|
|
icon: Icon.Vault,
|
2020-03-07 12:32:17 +05:00
|
|
|
positiveButtonText: "Done"
|
|
|
|
|
};
|
|
|
|
|
case "unlock_vault":
|
|
|
|
|
return {
|
|
|
|
|
title: "Unlock Vault",
|
|
|
|
|
icon: Icon.Unlock,
|
|
|
|
|
positiveButtonText: "Unlock"
|
|
|
|
|
};
|
|
|
|
|
case "unlock_note":
|
|
|
|
|
return {
|
|
|
|
|
title: "Unlock Note",
|
|
|
|
|
icon: Icon.Unlock,
|
|
|
|
|
positiveButtonText: "Unlock"
|
|
|
|
|
};
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 15:20:01 +05:00
|
|
|
export const showPasswordDialog = (type, validate) => {
|
2020-03-07 12:32:17 +05:00
|
|
|
const { title, icon, positiveButtonText } = getDialogData(type);
|
|
|
|
|
return showDialog(perform => (
|
|
|
|
|
<PasswordDialog
|
|
|
|
|
title={title}
|
|
|
|
|
icon={icon}
|
|
|
|
|
positiveButtonText={positiveButtonText}
|
2020-03-07 15:20:01 +05:00
|
|
|
onCancel={() => perform(false)}
|
|
|
|
|
validate={validate}
|
|
|
|
|
onDone={() => perform(true)}
|
2020-03-07 12:32:17 +05:00
|
|
|
/>
|
|
|
|
|
));
|
|
|
|
|
};
|