diff --git a/web/cashtab/src/components/Configure/Configure.js b/web/cashtab/src/components/Configure/Configure.js --- a/web/cashtab/src/components/Configure/Configure.js +++ b/web/cashtab/src/components/Configure/Configure.js @@ -483,6 +483,14 @@ const [contactListArray, setContactListArray] = useState([{}]); + const [showRenameContactModal, setShowRenameContactModal] = useState(false); + const [contactToBeRenamed, setContactToBeRenamed] = useState(null); //object + const [newContactNameIsValid, setNewContactNameIsValid] = useState(null); + const [ + confirmationOfContactToBeRenamed, + setConfirmationOfContactToBeRenamed, + ] = useState(''); + useEffect(() => { // Update savedWallets every time the active wallet changes updateSavedWallets(wallet); @@ -744,6 +752,84 @@ } }; + const handleContactNameInput = e => { + const { value } = e.target; + + if (value && value.length && value.length < 24) { + setNewContactNameIsValid(true); + } else { + setNewContactNameIsValid(false); + } + setConfirmationOfContactToBeRenamed(value); + }; + + const handleRenameContact = contactObj => { + if (!contactObj) { + console.log( + 'handleRenameContact() error: Invalid contact object for update', + ); + return; + } + setContactToBeRenamed(contactObj); + setShowRenameContactModal(true); + }; + + const handleRenameContactCancel = () => { + setShowRenameContactModal(false); + }; + + const handleRenameContactModalOk = () => { + if ( + !newContactNameIsValid || + newContactNameIsValid === null || + !contactToBeRenamed + ) { + return; + } + renameContactByName(contactToBeRenamed); + setShowRenameContactModal(false); + }; + + const renameContactByName = async contactObj => { + // obtain reference to the contact object in the array + let contactObjToUpdate = contactListArray.find( + element => element.address === contactObj.address, + ); + + // if a match was found + if (contactObjToUpdate) { + // update the contact name + contactObjToUpdate.name = confirmationOfContactToBeRenamed; + + // update local object array and local storage + setContactListArray(contactListArray); + + let updateContactListStatus; + try { + updateContactListStatus = await updateContactListInLocalForage( + contactListArray, + ); + } catch (err) { + console.log('Error in updateContactListInLocalForage()'); + console.log(err); + } + + if (!updateContactListStatus) { + errorNotification( + null, + 'Unable to update localforage with updated contact list', + 'Updating localforage with contact list', + ); + } + } else { + errorNotification( + null, + 'Unable to find contact in array', + 'Updating localforage with contact list', + ); + } + }; + const handleSendModalToggle = checkedState => { changeCashtabSettings('sendModal', checkedState); }; @@ -751,6 +837,43 @@ return ( + {showRenameContactModal && ( + handleRenameContactModalOk()} + onCancel={() => handleRenameContactCancel()} + > + +
+ + } + placeholder="Enter new contact name" + name="newContactName" + value={confirmationOfContactToBeRenamed} + onChange={e => + handleContactNameInput(e) + } + /> + +
+
+
+ )} {walletToBeRenamed !== null && (