This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
OpenVPNCredentialsModal.js
86 lines (80 loc) · 2.94 KB
/
OpenVPNCredentialsModal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { c } from 'ttag';
import {
FormModal,
Row,
Label,
Field,
Input,
PasswordInput,
useLoading,
useNotifications,
useApi
} from 'react-components';
import { updateVPNName, updateVPNPassword } from 'proton-shared/lib/api/vpn';
const OpenVPNCredentialsModal = ({ username = '', password = '', fetchUserVPN, ...rest }) => {
const [loading, withLoading] = useLoading();
const api = useApi();
const { createNotification } = useNotifications();
const [credentials, setCredentials] = useState({ username, password });
const title = c('Title').t`Edit OpenVPN / IKEv2 credentials`;
const handleChangeUsername = ({ target }) => setCredentials({ ...credentials, username: target.value });
const handleChangePassword = ({ target }) => setCredentials({ ...credentials, password: target.value });
const handleSubmit = async () => {
const RESERVED_USERNAMES = ['guest'];
if (RESERVED_USERNAMES.includes(credentials.username.toLowerCase())) {
createNotification({
text: c('Error').t`'${credentials.username}' is a reserved word. Please set another username.`
});
return;
}
await api(updateVPNName(credentials.username));
await api(updateVPNPassword(credentials.password));
await fetchUserVPN();
rest.onClose();
createNotification({ text: c('Notification').t`OpenVPN / IKEv2 credentials updated` });
};
return (
<FormModal
loading={loading}
title={title}
close={c('Action').t`Cancel`}
submit={c('Action').t`Update`}
onSubmit={() => withLoading(handleSubmit())}
small={true}
{...rest}
>
<Row>
<Label htmlFor="openvpn-username">{c('Label').t`OpenVPN / IKEv2 username`}</Label>
<Field>
<Input
id="openvpn-username"
autoComplete="off"
value={credentials.username}
onChange={handleChangeUsername}
required
/>
</Field>
</Row>
<Row>
<Label htmlFor="openvpn-password">{c('Label').t`OpenVPN / IKEv2 password`}</Label>
<Field>
<PasswordInput
id="openvpn-password"
autoComplete="off"
value={credentials.password}
onChange={handleChangePassword}
required
/>
</Field>
</Row>
</FormModal>
);
};
OpenVPNCredentialsModal.propTypes = {
username: PropTypes.string,
password: PropTypes.string,
fetchUserVPN: PropTypes.func.isRequired
};
export default OpenVPNCredentialsModal;