Extract preferences into a separate class and source file
Krzysztof Sikorski

Krzysztof Sikorski commited on 2021-11-08 00:44:43
Showing 3 changed files, with 82 additions and 24 deletions.

... ...
@@ -26,6 +26,7 @@
26 26
     <button type="submit">Save</button>
27 27
   </p>
28 28
 </form>
29
+<script src="../src/preferences.js"></script>
29 30
 <script src="../src/options.js"></script>
30 31
 </body>
31 32
 </html>
... ...
@@ -1,41 +1,29 @@
1
+/* global Preferences */
1 2
 'use strict'
2 3
 
4
+const preferences = new Preferences()
5
+
3 6
 const getOptionsForm = () => document.getElementById('optionsForm')
4
-const getStorage = () => browser.storage.sync
5 7
 
6 8
 const saveForm = event => {
7 9
   event.preventDefault()
8 10
 
9 11
   const optionsForm = getOptionsForm()
10
-  const userAccessToken = optionsForm.elements['userAccessToken'].value
11
-  const trackerSubmitUrl = optionsForm.elements['trackerSubmitUrl'].value
12
-
13
-  getStorage().set({userAccessToken, trackerSubmitUrl}).catch(
14
-    error => {
15
-      const message = `Error saving preferences: ${error}`
16
-      window.console.error(message)
17
-      window.alert(message)
18
-    }
19
-  )
12
+  preferences.userAccessToken = optionsForm.elements['userAccessToken'].value
13
+  preferences.trackerSubmitUrl = optionsForm.elements['trackerSubmitUrl'].value
14
+  preferences.save()
20 15
 }
21 16
 
22 17
 const initForm = () => {
23 18
   const optionsForm = getOptionsForm()
24 19
   optionsForm.addEventListener('submit', saveForm)
25 20
 
26
-  getStorage().get(null).then(
27
-    results => {
28
-      if (Object.prototype.hasOwnProperty.call(results, 'userAccessToken')) {
29
-        optionsForm.elements['userAccessToken'].value = results.userAccessToken
30
-      }
31
-      if (Object.prototype.hasOwnProperty.call(results, 'trackerSubmitUrl')) {
32
-        optionsForm.elements['trackerSubmitUrl'].value = results.trackerSubmitUrl
33
-      }
34
-    },
35
-    error => {
36
-      const message = `Error loading preferences: ${error}`
37
-      window.console.error(message)
38
-      window.alert(message)
21
+  preferences.load().then(
22
+    () => {
23
+      window.console.debug('[options] CALL preferences.load().then') // TODO tmp
24
+      window.console.debug('[options] preferences:', preferences) // TODO tmp
25
+      optionsForm.elements['userAccessToken'].value = preferences.userAccessToken
26
+      optionsForm.elements['trackerSubmitUrl'].value = preferences.trackerSubmitUrl
39 27
     }
40 28
   )
41 29
 }
... ...
@@ -0,0 +1,69 @@
1
+/* exported Preferences */
2
+'use strict'
3
+
4
+class Preferences {
5
+  constructor() {
6
+    this._userAccessToken = null
7
+    this._trackerSubmitUrl = null
8
+  }
9
+
10
+  get userAccessToken() {
11
+    return this._userAccessToken
12
+  }
13
+
14
+  set userAccessToken(value) {
15
+    this._userAccessToken = value
16
+  }
17
+
18
+
19
+  get trackerSubmitUrl() {
20
+    return this._trackerSubmitUrl
21
+  }
22
+
23
+  set trackerSubmitUrl(value) {
24
+    this._trackerSubmitUrl = value
25
+  }
26
+
27
+  get _storage() {
28
+    return browser.storage.sync
29
+  }
30
+
31
+  load() {
32
+    const storageGetter = this._storage.get(null)
33
+
34
+    storageGetter.then(
35
+      results => {
36
+        if (Object.prototype.hasOwnProperty.call(results, 'userAccessToken')) {
37
+          this.userAccessToken = results.userAccessToken
38
+        }
39
+        if (Object.prototype.hasOwnProperty.call(results, 'trackerSubmitUrl')) {
40
+          this.trackerSubmitUrl = results.trackerSubmitUrl
41
+        }
42
+      },
43
+      error => {
44
+        const message = `Error loading preferences: ${error}`
45
+        window.console.error(message)
46
+        window.alert(message)
47
+      }
48
+    )
49
+
50
+    return storageGetter
51
+  }
52
+
53
+  save() {
54
+    const storageSetter = this._storage.set({
55
+      userAccessToken: this.userAccessToken,
56
+      trackerSubmitUrl: this.trackerSubmitUrl
57
+    })
58
+
59
+    storageSetter.catch(
60
+      error => {
61
+        const message = `Error saving preferences: ${error}`
62
+        window.console.error(message)
63
+        window.alert(message)
64
+      }
65
+    )
66
+
67
+    return storageSetter
68
+  }
69
+}
0 70