@@ -19,6 +19,7 @@ import CreateVaultDialog from '../../components/CreateVaultDialog';
1919import { saveVault , setVault } from '../../reducers/VaultReducer/Actions' ;
2020import VaultCard from '../../components/VaultCard' ;
2121import CreatePasswordDialog from '../../components/CreatePasswordDialog' ;
22+ import EditPasswordDialog from '../../components/EditPasswordDialog' ;
2223
2324const 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