Skip to content

Commit 281265f

Browse files
committed
Refactoring, added visibility and generate buttons to password dialogs in the vault
1 parent 0b392be commit 281265f

File tree

6 files changed

+321
-160
lines changed

6 files changed

+321
-160
lines changed

src/components/CreatePasswordDialog/index.jsx

Lines changed: 128 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,43 @@ import TextField from '@mui/material/TextField';
44
import DialogContent from '@mui/material/DialogContent';
55
import DialogTitle from '@mui/material/DialogTitle';
66
import DialogActions from '@mui/material/DialogActions';
7+
import IconButton from '@mui/material/IconButton';
8+
import Grid from '@mui/material/Grid';
79
import Button from '@mui/material/Button';
10+
import RefreshIcon from '@mui/icons-material/Refresh';
11+
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
12+
import VisibilityIcon from '@mui/icons-material/Visibility';
813
import { MainContext } from '../../contexts/MainContextProvider';
914
import PasswordStrength from '../../utils/PasswordStrength';
1015
import LinearProgressWithLabel from '../LinearProgressWithLabel';
16+
import { setError } from '../../reducers/MainReducer/Actions';
17+
import { generatePasswordArray, getFullCharacterSet } from '../../reducers/PasswordReducer/Actions';
18+
import { PasswordContext } from '../../contexts/PasswordContextProvider';
1119

1220
const CreatePasswordDialog = ({ open, onCreate, onClose }) => {
13-
const [state] = useContext(MainContext);
21+
const [state, d1] = useContext(MainContext);
22+
const [state2] = useContext(PasswordContext);
23+
1424
const language = state.languages[state.languageIndex];
25+
const {
26+
min,
27+
max,
28+
smallLetters,
29+
capitalLetters,
30+
spaces,
31+
specialCharacters,
32+
numbers,
33+
brackets,
34+
useAdvanced,
35+
characterSet,
36+
allowDuplicates,
37+
} = state2;
1538

1639
const [title, setTitle] = useState('');
1740
const [description, setDescription] = useState('');
1841
const [url, setUrl] = useState('');
1942
const [password, setPassword] = useState('');
43+
const [showPassword, setShowPassword] = useState(false);
2044

2145
/**
2246
* Close the dialog
@@ -51,6 +75,33 @@ const CreatePasswordDialog = ({ open, onCreate, onClose }) => {
5175
}
5276
};
5377

78+
/**
79+
* Generate passwords
80+
*/
81+
const generatePassword = () => {
82+
const simpleCharacterSet = getFullCharacterSet(
83+
characterSet,
84+
useAdvanced,
85+
smallLetters,
86+
capitalLetters,
87+
spaces,
88+
numbers,
89+
specialCharacters,
90+
brackets,
91+
);
92+
if (!simpleCharacterSet || simpleCharacterSet.length === 0 || min > max || max < min) {
93+
return;
94+
}
95+
96+
generatePasswordArray(min, max, simpleCharacterSet, 1, allowDuplicates, null)
97+
.then((res) => {
98+
setPassword(res[0]);
99+
})
100+
.catch((err) => {
101+
d1(setError(err));
102+
});
103+
};
104+
54105
return (
55106
<Dialog
56107
open={open}
@@ -61,50 +112,82 @@ const CreatePasswordDialog = ({ open, onCreate, onClose }) => {
61112
{language.createPassword}
62113
</DialogTitle>
63114
<DialogContent>
64-
<TextField
65-
sx={{ mt: 2 }}
66-
value={title}
67-
error={title.length === 0}
68-
label={language.title}
69-
onChange={(e) => setTitle(e.target.value)}
70-
onKeyUp={handleKeyUp}
71-
autoComplete="off"
72-
variant="outlined"
73-
fullWidth
74-
/>
75-
<TextField
76-
sx={{ mt: 2 }}
77-
value={description}
78-
label={language.description}
79-
onChange={(e) => setDescription(e.target.value)}
80-
onKeyUp={handleKeyUp}
81-
autoComplete="off"
82-
variant="outlined"
83-
fullWidth
84-
/>
85-
<TextField
86-
sx={{ mt: 2 }}
87-
value={url}
88-
label={language.url}
89-
onChange={(e) => setUrl(e.target.value)}
90-
onKeyUp={handleKeyUp}
91-
autoComplete="off"
92-
variant="outlined"
93-
fullWidth
94-
/>
95-
<TextField
96-
sx={{ mt: 2 }}
97-
value={password}
98-
type="password"
99-
error={password.length === 0}
100-
label={language.password}
101-
onChange={(e) => setPassword(e.target.value)}
102-
onKeyUp={handleKeyUp}
103-
autoComplete="off"
104-
variant="outlined"
105-
fullWidth
106-
/>
107-
<LinearProgressWithLabel value={PasswordStrength(password)} />
115+
<Grid container spacing={2} sx={{ mt: 2 }}>
116+
<Grid item xs={12} md={12} lg={12}>
117+
<TextField
118+
value={title}
119+
error={title.length === 0}
120+
label={language.title}
121+
onChange={(e) => setTitle(e.target.value)}
122+
onKeyUp={handleKeyUp}
123+
autoComplete="off"
124+
variant="outlined"
125+
fullWidth
126+
/>
127+
</Grid>
128+
<Grid item xs={12} md={12} lg={12}>
129+
<TextField
130+
value={description}
131+
label={language.description}
132+
onChange={(e) => setDescription(e.target.value)}
133+
onKeyUp={handleKeyUp}
134+
autoComplete="off"
135+
variant="outlined"
136+
fullWidth
137+
/>
138+
</Grid>
139+
<Grid item xs={12} md={12} lg={12}>
140+
<TextField
141+
value={url}
142+
label={language.url}
143+
onChange={(e) => setUrl(e.target.value)}
144+
onKeyUp={handleKeyUp}
145+
autoComplete="off"
146+
variant="outlined"
147+
fullWidth
148+
/>
149+
</Grid>
150+
<Grid item xs={12} md={8} lg={8}>
151+
<TextField
152+
value={password}
153+
type={showPassword ? 'text' : 'password'}
154+
error={password.length === 0}
155+
label={language.password}
156+
onChange={(e) => setPassword(e.target.value)}
157+
onKeyUp={handleKeyUp}
158+
autoComplete="off"
159+
variant="outlined"
160+
fullWidth
161+
/>
162+
</Grid>
163+
<Grid item xs={6} md={2} lg={2}>
164+
<IconButton
165+
color="primary"
166+
size="large"
167+
sx={{ width: '100%', height: '100%' }}
168+
onClick={() => setShowPassword(!showPassword)}
169+
>
170+
{showPassword ? (
171+
<VisibilityOffIcon fontSize="inherit" />
172+
) : (
173+
<VisibilityIcon fontSize="inherit" />
174+
)}
175+
</IconButton>
176+
</Grid>
177+
<Grid item xs={6} md={2} lg={2}>
178+
<IconButton
179+
color="primary"
180+
size="large"
181+
sx={{ width: '100%', height: '100%' }}
182+
onClick={generatePassword}
183+
>
184+
<RefreshIcon fontSize="inherit" />
185+
</IconButton>
186+
</Grid>
187+
<Grid item xs={12} md={12} lg={12}>
188+
<LinearProgressWithLabel value={PasswordStrength(password)} />
189+
</Grid>
190+
</Grid>
108191
</DialogContent>
109192
<DialogActions>
110193
<Button onClick={handleClose}>

0 commit comments

Comments
 (0)