Skip to content

Commit bedc127

Browse files
committed
Added ability to edit passwords in the vault
1 parent 1c2e6c3 commit bedc127

File tree

9 files changed

+170
-8
lines changed

9 files changed

+170
-8
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React, { useContext, useState } from 'react';
2+
import Dialog from '@mui/material/Dialog';
3+
import TextField from '@mui/material/TextField';
4+
import DialogContent from '@mui/material/DialogContent';
5+
import DialogTitle from '@mui/material/DialogTitle';
6+
import DialogActions from '@mui/material/DialogActions';
7+
import Button from '@mui/material/Button';
8+
import { MainContext } from '../../contexts/MainContextProvider';
9+
10+
const EditPasswordDialog = ({
11+
data, open, onSave, onClose,
12+
}) => {
13+
const [state] = useContext(MainContext);
14+
const language = state.languages[state.languageIndex];
15+
16+
const [title, setTitle] = useState(data && data.title ? data.title : '');
17+
const [description, setDescription] = useState(data && data.description ? data.description : '');
18+
const [url, setUrl] = useState(data && data.url ? data.url : '');
19+
const [password, setPassword] = useState(data && data.password ? data.password : '');
20+
21+
/**
22+
* Close the dialog
23+
*/
24+
const handleClose = () => {
25+
if (onClose) {
26+
onClose();
27+
}
28+
};
29+
30+
/**
31+
* Create a new vault
32+
*/
33+
const save = () => {
34+
if (onSave) {
35+
onSave(data.id, title, description, url, password);
36+
}
37+
handleClose();
38+
};
39+
40+
return (
41+
<Dialog
42+
open={open}
43+
onClose={handleClose}
44+
aria-labelledby="edit-vault-dialog-title"
45+
>
46+
<DialogTitle id="edit-vault-dialog-title">
47+
{language.editPassword}
48+
</DialogTitle>
49+
<DialogContent>
50+
<TextField
51+
sx={{ mt: 2 }}
52+
value={title}
53+
error={title.length === 0}
54+
label={language.title}
55+
onChange={(e) => setTitle(e.target.value)}
56+
autoComplete="off"
57+
variant="outlined"
58+
fullWidth
59+
/>
60+
<TextField
61+
sx={{ mt: 2 }}
62+
value={description}
63+
label={language.description}
64+
onChange={(e) => setDescription(e.target.value)}
65+
autoComplete="off"
66+
variant="outlined"
67+
fullWidth
68+
/>
69+
<TextField
70+
sx={{ mt: 2 }}
71+
value={url}
72+
label={language.url}
73+
onChange={(e) => setUrl(e.target.value)}
74+
autoComplete="off"
75+
variant="outlined"
76+
fullWidth
77+
/>
78+
<TextField
79+
sx={{ mt: 2 }}
80+
value={password}
81+
type="password"
82+
error={password.length === 0}
83+
label={language.password}
84+
onChange={(e) => setPassword(e.target.value)}
85+
autoComplete="off"
86+
variant="outlined"
87+
fullWidth
88+
/>
89+
</DialogContent>
90+
<DialogActions>
91+
<Button onClick={handleClose}>
92+
{language.cancel}
93+
</Button>
94+
<Button
95+
onClick={save}
96+
autoFocus
97+
disabled={password.length === 0 || title.length === 0}
98+
>
99+
{language.save}
100+
</Button>
101+
</DialogActions>
102+
</Dialog>
103+
);
104+
};
105+
106+
export default EditPasswordDialog;

src/languages/de_de.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "Hinzufügen",
9898
"search": "Suche",
99-
"noResults": "Keine Ergebnisse"
99+
"noResults": "Keine Ergebnisse",
100+
"editPassword": "Passwort bearbeiten"
100101
}

src/languages/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "Add",
9898
"search": "Search",
99-
"noResults": "No results"
99+
"noResults": "No results",
100+
"editPassword": "Edit password"
100101
}

src/languages/fr_fr.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "Ajouter",
9898
"search": "Rechercher",
99-
"noResults": "Aucun résultat"
99+
"noResults": "Aucun résultat",
100+
"editPassword": "Modifier le mot de passe"
100101
}

src/languages/jp_jp.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "追加",
9898
"search": "サーチ",
99-
"noResults": "結果がありません"
99+
"noResults": "結果がありません",
100+
"editPassword": "パスワードを編集する"
100101
}

src/languages/nl_nl.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "Toevoegen",
9898
"search": "Zoeken",
99-
"noResults": "Geen resultaten"
99+
"noResults": "Geen resultaten",
100+
"editPassword": "Wachtwoord bewerken"
100101
}

src/languages/ru_ru.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "URL",
9797
"add": "Добавить",
9898
"search": "Поиск",
99-
"noResults": "Нет результатов"
99+
"noResults": "Нет результатов",
100+
"editPassword": "Изменить пароль"
100101
}

src/languages/zh_cn.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@
9696
"url": "网址",
9797
"add": "添加",
9898
"search": "搜索",
99-
"noResults": "无结果"
99+
"noResults": "无结果",
100+
"editPassword": "编辑密码"
100101
}

src/routes/Vault/index.jsx

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import CreateVaultDialog from '../../components/CreateVaultDialog';
1919
import { saveVault, setVault } from '../../reducers/VaultReducer/Actions';
2020
import VaultCard from '../../components/VaultCard';
2121
import CreatePasswordDialog from '../../components/CreatePasswordDialog';
22+
import EditPasswordDialog from '../../components/EditPasswordDialog';
2223

2324
const Vault = () => {
2425
const [state, d1] = useContext(MainContext);
@@ -31,6 +32,8 @@ const Vault = () => {
3132
const [search, setSearch] = useState('');
3233
const [createDialogOpen, setCreateDialogOpen] = useState(false);
3334
const [createPasswordDialogOpen, setCreatePasswordDialogOpen] = useState(false);
35+
const [editPasswordDialogOpen, setEditPasswordDialogOpen] = useState(false);
36+
const [editPasswordId, setEditPasswordId] = useState(null);
3437

3538
/**
3639
* Create a new vault
@@ -84,6 +87,42 @@ const Vault = () => {
8487
d3(setVault(newVault));
8588
};
8689

90+
/**
91+
* Open the edit password dialog
92+
* @param id The ID of the password that needs to be edited
93+
*/
94+
const openEditPasswordDialog = (id) => {
95+
setEditPasswordId(id);
96+
setEditPasswordDialogOpen(true);
97+
};
98+
99+
/**
100+
* Close the dialog to edit a password
101+
*/
102+
const closeEditPasswordDialog = () => {
103+
setEditPasswordId(null);
104+
setEditPasswordDialogOpen(false);
105+
};
106+
107+
/**
108+
* Edit a password
109+
* @param id The ID of the password
110+
* @param title The new title
111+
* @param description The new description
112+
* @param url The new URL
113+
* @param password The new password
114+
*/
115+
const editPassword = (id, title, description, url, password) => {
116+
const newVault = JSON.parse(JSON.stringify(vault));
117+
newVault.filter((e) => e.id === id).forEach((e) => {
118+
e.title = title;
119+
e.description = description;
120+
e.url = url;
121+
e.password = password;
122+
});
123+
d3(setVault(newVault));
124+
};
125+
87126
useEffect(() => {
88127
d1(setPageIndex(4));
89128
}, []);
@@ -116,14 +155,16 @@ const Vault = () => {
116155
editLabel={language.edit}
117156
deleteLabel={language.delete}
118157
onClick={openPassword}
119-
onEdit={() => {}}
158+
onEdit={openEditPasswordDialog}
120159
onDelete={deletePassword}
121160
/>
122161
</Grid>
123162
));
124163
}
125164
}
126165

166+
const toEdit = vault && vault.length > 0 ? vault.filter((p) => p.id === editPasswordId)[0] : null;
167+
127168
return (
128169
<Container>
129170
<Grid container spacing={2}>
@@ -212,6 +253,14 @@ const Vault = () => {
212253
onClose={() => setCreatePasswordDialogOpen(false)}
213254
onCreate={addPassword}
214255
/>
256+
{editPasswordDialogOpen && toEdit ? (
257+
<EditPasswordDialog
258+
open={editPasswordDialogOpen}
259+
onClose={closeEditPasswordDialog}
260+
onSave={editPassword}
261+
data={toEdit}
262+
/>
263+
) : null}
215264
</Container>
216265
);
217266
};

0 commit comments

Comments
 (0)