neon_arch commited on
Commit
3446820
โ€ข
1 Parent(s): 1d97187

๐Ÿšธ chore: add code to display saved user settings from the cookies on the settings page (#461)

Browse files
Files changed (1) hide show
  1. public/static/cookies.js +67 -3
public/static/cookies.js CHANGED
@@ -1,3 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  /**
2
  * This function is executed when any page on the website finishes loading and
3
  * this function retrieves the cookies if it is present on the user's machine.
@@ -16,9 +75,14 @@ document.addEventListener(
16
  let cookie = decodeURIComponent(document.cookie)
17
  // Set the value of the input field to the decoded cookie value if it is not empty
18
  // Otherwise, display a message indicating that no cookies have been saved on the user's system
19
- document.querySelector('.cookies input').value = cookie.length
20
- ? cookie
21
- : 'No cookies have been saved on your system'
 
 
 
 
 
22
  } catch (error) {
23
  // If there is an error decoding the cookie, log the error to the console
24
  // and display an error message in the input field
 
1
+ /**
2
+ * This functions gets the saved cookies if it is present on the user's machine If it
3
+ * is available then it is parsed and converted to an object which is then used to
4
+ * retrieve the preferences that the user had selected previously and is then loaded
5
+ * and used for displaying the user provided settings by setting them as the selected
6
+ * options in the settings page.
7
+ *
8
+ * @function
9
+ * @param {string} cookie - It takes the client settings cookie as a string.
10
+ * @returns {void}
11
+ */
12
+ function setClientSettingsOnPage(cookie) {
13
+ let cookie_value = cookie
14
+ .split(';')
15
+ .map((item) => item.split('='))
16
+ .reduce((acc, [_, v]) => (acc = JSON.parse(v)) && acc, {})
17
+
18
+ // Loop through all select tags and add their values to the cookie dictionary
19
+ document.querySelectorAll('select').forEach((select_tag) => {
20
+ switch (select_tag.name) {
21
+ case 'themes':
22
+ select_tag.value = cookie_value['theme']
23
+ break
24
+ case 'colorschemes':
25
+ select_tag.value = cookie_value['colorscheme']
26
+ break
27
+ case 'animations':
28
+ select_tag.value = cookie_value['animation']
29
+ break
30
+ case 'safe_search_levels':
31
+ select_tag.value = cookie_value['safe_search_level']
32
+ break
33
+ }
34
+ })
35
+ let engines = document.querySelectorAll('.engine')
36
+ let engines_cookie = cookie_value['engines']
37
+
38
+ if (engines_cookie.length === engines.length) {
39
+ document.querySelector('.select_all').checked = true
40
+ engines.forEach((engine_checkbox) => {
41
+ engine_checkbox.checked = true
42
+ })
43
+ } else {
44
+ engines.forEach((engines_checkbox) => {
45
+ engines_checkbox.checked = false
46
+ })
47
+ engines_cookie.forEach((engine_name) => {
48
+ engines.forEach((engine_checkbox) => {
49
+ if (
50
+ engine_checkbox.parentNode.parentNode.innerText.trim() ===
51
+ engine_name.trim()
52
+ ) {
53
+ engine_checkbox.checked = true
54
+ }
55
+ })
56
+ })
57
+ }
58
+ }
59
+
60
  /**
61
  * This function is executed when any page on the website finishes loading and
62
  * this function retrieves the cookies if it is present on the user's machine.
 
75
  let cookie = decodeURIComponent(document.cookie)
76
  // Set the value of the input field to the decoded cookie value if it is not empty
77
  // Otherwise, display a message indicating that no cookies have been saved on the user's system
78
+ if (cookie.length) {
79
+ document.querySelector('.cookies input').value = cookie
80
+ // This function displays the user provided settings on the settings page.
81
+ setClientSettingsOnPage(cookie)
82
+ } else {
83
+ document.querySelector('.cookies input').value =
84
+ 'No cookies have been saved on your system'
85
+ }
86
  } catch (error) {
87
  // If there is an error decoding the cookie, log the error to the console
88
  // and display an error message in the input field