refactor(@wallet): simplify saved address

This commit is contained in:
Anthony Laibe 2022-03-24 10:00:13 +01:00 committed by Anthony Laibe
parent acb38a4c4c
commit 1e6a640c33
1 changed files with 7 additions and 17 deletions

View File

@ -25,7 +25,7 @@ proc newService*(events: EventEmitter): Service =
result = Service() result = Service()
result.events = events result.events = events
proc init*(self: Service) = proc fetchAddresses(self: Service) =
try: try:
let response = backend.getSavedAddresses() let response = backend.getSavedAddresses()
@ -35,7 +35,10 @@ proc init*(self: Service) =
) )
except Exception as e: except Exception as e:
error "error: ", procName="init", errName = e.name, errDesription = e.msg error "error: ", procName="fetchAddress", errName = e.name, errDesription = e.msg
proc init*(self: Service) =
self.fetchAddresses()
proc getSavedAddresses*(self: Service): seq[SavedAddressDto] = proc getSavedAddresses*(self: Service): seq[SavedAddressDto] =
return self.savedAddresses return self.savedAddresses
@ -47,16 +50,7 @@ proc createOrUpdateSavedAddress*(self: Service, name, address: string): string =
if not response.error.isNil: if not response.error.isNil:
raise newException(Exception, response.error.message) raise newException(Exception, response.error.message)
var found = false self.fetchAddresses()
for savedAddress in self.savedAddresses:
if savedAddress.address == address:
savedAddress.name = name
found = true
break
if not found:
self.savedAddresses.add(newSavedAddressDto(name, address))
self.events.emit(SIGNAL_SAVED_ADDRESS_CHANGED, Args()) self.events.emit(SIGNAL_SAVED_ADDRESS_CHANGED, Args())
return "" return ""
except Exception as e: except Exception as e:
@ -71,11 +65,7 @@ proc deleteSavedAddress*(self: Service, address: string): string =
if not response.error.isNil: if not response.error.isNil:
raise newException(Exception, response.error.message) raise newException(Exception, response.error.message)
for i in 0..<self.savedAddresses.len: self.fetchAddresses()
if self.savedAddresses[i].address == address:
self.savedAddresses.delete(i)
break
self.events.emit(SIGNAL_SAVED_ADDRESS_CHANGED, Args()) self.events.emit(SIGNAL_SAVED_ADDRESS_CHANGED, Args())
return "" return ""
except Exception as e: except Exception as e: