aboutsummaryrefslogtreecommitdiffstats
path: root/ui/lib
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2016-08-26 06:40:49 +0800
committerGitHub <noreply@github.com>2016-08-26 06:40:49 +0800
commitb6b57d928a8854baf674763211c407bb4c976dba (patch)
tree0967f2b850593f7bd81d24ae62b58ea030fbda76 /ui/lib
parentbbb684309e04030dbf288f3e933222b3d27d9b54 (diff)
parenta1fd2b6688093119042e773762081114c0f29ce9 (diff)
downloadtangerine-wallet-browser-b6b57d928a8854baf674763211c407bb4c976dba.tar.gz
tangerine-wallet-browser-b6b57d928a8854baf674763211c407bb4c976dba.tar.zst
tangerine-wallet-browser-b6b57d928a8854baf674763211c407bb4c976dba.zip
Merge pull request #590 from MetaMask/i432PersistFormState
Persist form state when closing popup
Diffstat (limited to 'ui/lib')
-rw-r--r--ui/lib/persistent-form.js57
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({})
+}