1 import { useState } from 'react';
3 import { c } from 'ttag';
5 import { rotateOrganizationKeys } from '@proton/account';
6 import { Button } from '@proton/atoms';
7 import Alert from '@proton/components/components/alert/Alert';
8 import Form from '@proton/components/components/form/Form';
9 import type { ModalProps } from '@proton/components/components/modalTwo/Modal';
10 import Modal from '@proton/components/components/modalTwo/Modal';
11 import ModalContent from '@proton/components/components/modalTwo/ModalContent';
12 import ModalFooter from '@proton/components/components/modalTwo/ModalFooter';
13 import ModalHeader from '@proton/components/components/modalTwo/ModalHeader';
14 import useModalState from '@proton/components/components/modalTwo/useModalState';
15 import InputFieldTwo from '@proton/components/components/v2/field/InputField';
16 import PasswordInputTwo from '@proton/components/components/v2/input/PasswordInput';
17 import useFormErrors from '@proton/components/components/v2/useFormErrors';
18 import AuthModal from '@proton/components/containers/password/AuthModal';
19 import useErrorHandler from '@proton/components/hooks/useErrorHandler';
20 import useEventManager from '@proton/components/hooks/useEventManager';
21 import useNotifications from '@proton/components/hooks/useNotifications';
22 import { useLoading } from '@proton/hooks';
23 import { useDispatch } from '@proton/redux-shared-store';
24 import { confirmPasswordValidator, passwordLengthValidator } from '@proton/shared/lib/helpers/formValidators';
25 import type { CachedOrganizationKey } from '@proton/shared/lib/interfaces';
26 import noop from '@proton/utils/noop';
28 interface Props extends ModalProps {
29 hasOtherAdmins: boolean;
30 organizationKey: CachedOrganizationKey;
34 const ChangeOrganizationKeysModal = ({ onClose, mode, hasOtherAdmins, organizationKey, ...rest }: Props) => {
35 const [authModalProps, setAuthModal, renderAuthModal] = useModalState();
36 const dispatch = useDispatch();
37 const { call } = useEventManager();
38 const { createNotification } = useNotifications();
39 const { validator, onFormSubmit } = useFormErrors();
40 const [config, setConfig] = useState<any>();
41 const errorHandler = useErrorHandler();
43 const [loading, withLoading] = useLoading();
44 const [newPassword, setNewPassword] = useState('');
45 const [confirmPassword, setConfirmPassword] = useState('');
48 mode === 'reset' ? c('passwordless').t`Reset organization key` : c('passwordless').t`Change organization key`;
50 const handleClose = loading ? noop : onClose;
52 const handleSubmit = async (password: string) => {
54 const config = await dispatch(rotateOrganizationKeys({ password }));
61 {renderAuthModal && config && (
69 onExit={() => setConfig(undefined)}
70 onSuccess={async () => {
72 createNotification({ text: c('Success').t`Keys updated` });
80 if (!onFormSubmit()) {
84 void withLoading(handleSubmit(newPassword)).catch(errorHandler);
89 <ModalHeader title={title} />
93 <Alert className="mb-4">{c('Info')
94 .t`Other administrators exist in your organization, you are responsible for communicating the new password to them.`}</Alert>
96 <Alert className="mb-4" type="warning">
98 .t`Do NOT forget this password. If you forget it, you will not be able to manage your organization.`}
101 .t`Save your password somewhere safe. Click on icon to confirm that you have typed your password correctly.`}
105 id="organizationPassword"
106 as={PasswordInputTwo}
107 label={c('Label').t`New organization password`}
108 placeholder={c('Placeholder').t`Password`}
110 onValue={setNewPassword}
111 error={validator([passwordLengthValidator(newPassword)])}
112 autoComplete="new-password"
118 as={PasswordInputTwo}
119 label={c('Label').t`Confirm organization password`}
120 placeholder={c('Placeholder').t`Confirm`}
121 value={confirmPassword}
122 onValue={setConfirmPassword}
124 passwordLengthValidator(newPassword),
125 confirmPasswordValidator(newPassword, confirmPassword),
127 autoComplete="new-password"
132 <Button onClick={handleClose} disabled={loading}>
133 {c('Action').t`Close`}
135 <Button loading={loading} type="submit" color="norm">
136 {c('Action').t`Change keys`}
143 export default ChangeOrganizationKeysModal;