-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddVaultModal.tsx
More file actions
128 lines (120 loc) · 3.75 KB
/
AddVaultModal.tsx
File metadata and controls
128 lines (120 loc) · 3.75 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { JSX, RefObject } from 'react';
import { useEffect } from 'react';
import { useRef } from 'react';
import React, { useState } from 'react';
import {
Box,
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TextField,
} from '@mui/material';
import { VaultsGateway } from '@ui/gateways/vaults.gateway';
import { container } from 'tsyringe';
import ErrorMessage from '@ui/components/common/ErrorMessage';
import { UiLogger } from '@ui/logs/ui.logger';
import { useApiCall } from '@ui/hooks/api/useApiCall';
import type { CreateVaultDataDto } from '@shared/dto/output/data/create-vault.data.dto';
import type { CreateVaultPayloadDto } from '@shared/dto/input/payloads/create-vault.payload.dto';
import Form from 'next/form';
import { BusinessError } from '@shared/errors/business-error';
type AddVaultModalProps = {
open: boolean;
onClose: () => void;
refreshVaults: () => Promise<void>;
};
export default function AddVaultModal(props: AddVaultModalProps): JSX.Element {
const delayToFocusFirstInput: number = 100;
const [newLabel, setNewLabel] = useState<string>('');
const [newSecret, setNewSecret] = useState<string>('');
const [labelError, setLabelError] = useState<Error | null>(null);
const [globalError, setGlobalError] = useState<Error | null>(null);
const vaultsGateway: VaultsGateway = container.resolve(VaultsGateway);
const labelInputRef: RefObject<HTMLInputElement | null> =
useRef<HTMLInputElement>(null);
const handleClose = (): void => {
setLabelError(null);
setGlobalError(null);
setNewLabel('');
setNewSecret('');
props.onClose();
};
const { execute: createVault, loading } = useApiCall<
CreateVaultDataDto,
CreateVaultPayloadDto
>({
request: payload => vaultsGateway.createVault(payload!),
onSuccess: async () => {
handleClose();
await props.refreshVaults();
},
onError: err => {
if (err instanceof BusinessError && err.message.includes('label')) {
setLabelError(err);
} else {
setGlobalError(err);
}
UiLogger.error('Create vault failed', err);
},
});
const handleSubmit = async (): Promise<void> => {
setGlobalError(null);
await createVault({ label: newLabel, secret: newSecret });
};
useEffect(() => {
if (props.open) {
setTimeout(() => {
labelInputRef.current?.focus();
}, delayToFocusFirstInput);
}
}, [props.open]);
return (
<Dialog open={props.open} onClose={handleClose} fullWidth maxWidth="xs">
<DialogTitle>Add a vault</DialogTitle>
<Form action={handleSubmit}>
<DialogContent>
<TextField
inputRef={labelInputRef}
margin="dense"
label="Label"
fullWidth
required
value={newLabel}
onChange={e => setNewLabel(e.target.value)}
error={!!globalError || !!labelError}
helperText={labelError ? labelError.message : ''}
/>
<TextField
error={!!globalError}
margin="dense"
label="Secret"
fullWidth
required
value={newSecret}
onChange={e => setNewSecret(e.target.value)}
sx={{ mt: 2 }}
/>
</DialogContent>
<Box
sx={{
paddingLeft: 3,
height: 15,
width: '100%',
overflowWrap: 'break-word',
wordBreak: 'break-word',
}}
>
<ErrorMessage error={globalError} />
</Box>
<DialogActions>
<Button onClick={handleClose}>Cancel</Button>
<Button type={'submit'} variant="contained" loading={loading}>
Create
</Button>
</DialogActions>
</Form>
</Dialog>
);
}