diff options
author | Frankie <frankie.pangilinan@consensys.net> | 2016-08-26 07:06:54 +0800 |
---|---|---|
committer | Frankie <frankie.pangilinan@consensys.net> | 2016-08-26 07:06:54 +0800 |
commit | 547894ed391f2d258c46ff382d4b211d18d81a7b (patch) | |
tree | 9425bee50dae3f6d254462363b5f60b837cb2e27 /ui/lib/persistent-form.js | |
parent | 250cf8cc23305e33f4fc26a1711996ebc54fd9ad (diff) | |
parent | 78f73038e789f4483f728a7cc9ea1c68b0b3d7fa (diff) | |
download | tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar.gz tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar.bz2 tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar.lz tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar.xz tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.tar.zst tangerine-wallet-browser-547894ed391f2d258c46ff382d4b211d18d81a7b.zip |
Merge branch 'master' into i#563forgotPassword
Diffstat (limited to 'ui/lib/persistent-form.js')
-rw-r--r-- | ui/lib/persistent-form.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/ui/lib/persistent-form.js b/ui/lib/persistent-form.js new file mode 100644 index 000000000..2fd7600a2 --- /dev/null +++ b/ui/lib/persistent-form.js @@ -0,0 +1,57 @@ +const inherits = require('util').inherits +const Component = require('react').Component +const defaultKey = 'persistent-form-default' +const eventName = 'keyup' + +module.exports = PersistentForm + +function PersistentForm () { + Component.call(this) +} + +inherits(PersistentForm, Component) + +PersistentForm.prototype.componentDidMount = function () { + const fields = document.querySelectorAll('[data-persistent-formid]') + const store = this.getPersistentStore() + fields.forEach((field) => { + const key = field.getAttribute('data-persistent-formid') + const cached = store[key] + if (cached !== undefined) { + field.value = cached + } + + field.addEventListener(eventName, this.persistentFieldDidUpdate.bind(this)) + }) +} + +PersistentForm.prototype.getPersistentStore = function () { + let store = window.localStorage[this.persistentFormParentId || defaultKey] + if (store && store !== 'null') { + store = JSON.parse(store) + } else { + store = {} + } + return store +} + +PersistentForm.prototype.setPersistentStore = function (newStore) { + window.localStorage[this.persistentFormParentId || defaultKey] = JSON.stringify(newStore) +} + +PersistentForm.prototype.persistentFieldDidUpdate = function (event) { + const field = event.target + const store = this.getPersistentStore() + const key = field.getAttribute('data-persistent-formid') + const val = field.value + store[key] = val + this.setPersistentStore(store) +} + +PersistentForm.prototype.componentWillUnmount = function () { + const fields = document.querySelectorAll('[data-persistent-formid]') + fields.forEach((field) => { + field.removeEventListener(eventName, this.persistentFieldDidUpdate.bind(this)) + }) + this.setPersistentStore({}) +} |