ente/src/components/TwoFactorModal.tsx

107 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-06-24 08:31:05 +00:00
import { useRouter } from 'next/router';
import { DeadCenter, SetLoading } from 'pages/gallery';
import React, { useEffect, useState } from 'react';
import { Button } from 'react-bootstrap';
import { disableTwoFactor } from 'services/userService';
import styled from 'styled-components';
import { getData, LS_KEYS, setData } from 'utils/storage/localStorage';
import constants from 'utils/strings/constants';
import MessageDialog, { SetDialogMessage } from './MessageDialog';
interface Props {
show: boolean;
onHide: () => void;
setDialogMessage: SetDialogMessage;
setLoading: SetLoading
}
const Row = styled.div`
display:flex;
align-items:center;
margin-bottom:20px;
flex:1
`;
const Label = styled.div`
width:70%;
`;
function TwoFactorModal(props: Props) {
const router = useRouter();
const [isTwoFactorEnabled, setTwoFactorStatus] = useState(false);
useEffect(() => {
if (!props.show) {
return;
}
const isTwoFactorEnabled = getData(LS_KEYS.USER).isTwoFactorEnabled ?? false;
setTwoFactorStatus(isTwoFactorEnabled);
}, [props.show]);
const warnTwoFactorDisable = async () => {
props.setDialogMessage({
2021-06-26 04:56:53 +00:00
title: constants.DISABLE_TWO_FACTOR,
2021-06-24 08:31:05 +00:00
staticBackdrop: true,
2021-06-26 04:56:53 +00:00
content: constants.DISABLE_TWO_FACTOR_MESSAGE,
2021-06-24 08:31:05 +00:00
close: { text: constants.CANCEL },
proceed: {
variant: 'danger',
text: constants.DISABLE,
action: twoFactorDisable,
},
});
};
const twoFactorDisable = async () => {
await disableTwoFactor();
setData(LS_KEYS.USER, { ...getData(LS_KEYS.USER), isTwoFactorEnabled: false });
props.onHide();
};
const warnTwoFactorReconfigure = async () => {
props.setDialogMessage({
2021-06-26 04:56:53 +00:00
title: constants.UPDATE_TWO_FACTOR,
2021-06-24 08:31:05 +00:00
staticBackdrop: true,
2021-06-25 20:34:56 +00:00
content: constants.UPDATE_TWO_FACTOR_MESSAGE,
2021-06-24 08:31:05 +00:00
close: { text: constants.CANCEL },
proceed: {
variant: 'success',
text: constants.UPDATE,
action: reconfigureTwoFactor,
},
});
};
const reconfigureTwoFactor = async () => {
router.push('/two-factor/setup');
2021-06-24 08:31:05 +00:00
};
return (
<MessageDialog
show={props.show}
onHide={props.onHide}
{...(!isTwoFactorEnabled && { size: 'lg' })}
attributes={{
2021-06-25 19:59:38 +00:00
title: constants.TWO_FACTOR_AUTHENTICATION,
2021-06-24 08:31:05 +00:00
staticBackdrop: true,
}}
>
<div {...(!isTwoFactorEnabled ? { style: { padding: '10px 40px 30px 40px' } } : { style: { padding: '10px' } })}>
{
isTwoFactorEnabled ?
<>
<Row>
2021-06-26 04:56:53 +00:00
<Label>{constants.DISABLE_TWO_FACTOR_HINT} </Label><Button variant={'outline-danger'} style={{ width: '30%' }} onClick={warnTwoFactorDisable}>{constants.DISABLE}</Button>
2021-06-24 08:31:05 +00:00
</Row>
<Row>
2021-06-26 04:56:53 +00:00
<Label>{constants.UPDATE_TWO_FACTOR_HINT}</Label> <Button variant={'outline-success'} style={{ width: '30%' }} onClick={warnTwoFactorReconfigure}>{constants.RECONFIGURE}</Button>
2021-06-24 08:31:05 +00:00
</Row>
</> : (
<DeadCenter>
<p>{constants.TWO_FACTOR_INFO}</p>
<div style={{ height: '10px' }} />
<Button variant="outline-success" onClick={() => router.push('/two-factor/setup')}>{constants.ENABLE_TWO_FACTOR}</Button>
2021-06-24 08:31:05 +00:00
</DeadCenter>
)
}
</div>
</MessageDialog >
);
}
export default TwoFactorModal;