From 9f6c04055419f54a730bcbd3f1da4c5f992db94d Mon Sep 17 00:00:00 2001 From: Kevin Serrano Date: Thu, 9 Mar 2017 13:58:42 -0800 Subject: Create persistence address book. --- app/scripts/controllers/address-book.js | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 app/scripts/controllers/address-book.js (limited to 'app/scripts/controllers') diff --git a/app/scripts/controllers/address-book.js b/app/scripts/controllers/address-book.js new file mode 100644 index 000000000..824a256d1 --- /dev/null +++ b/app/scripts/controllers/address-book.js @@ -0,0 +1,46 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +class AddressBookController { + + constructor (opts = {}) { + const initState = extend({ + addressBook: [], + }, opts.initState) + this.store = new ObservableStore(initState) + } + + // + // PUBLIC METHODS + // + + setAddressList (address, name) { + return this.addToAddressList(address, name) + .then((addressBook) => { + this.store.updateState({ + addressBook, + }) + return Promise.resolve() + }) + } + + addToAddressList (address, name) { + let addressBook = this.getAddressList() + let index = addressBook.findIndex((element) => { return element.address === address }) + if (index !== -1) { + addressBook.splice(index, 1) + } + addressBook.push({ + address, + name, + }) + return Promise.resolve(addressBook) + } + + getAddressList () { + return this.store.getState().addressBook + } + +} + +module.exports = AddressBookController -- cgit v1.2.3