-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount-theme.js
209 lines (161 loc) · 5.72 KB
/
account-theme.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* @license MIT
* boutique-en-ligne (maxaboom)
* Copyright (c) 2023 Abraham Ukachi, Axel Vair, Morgane Marechal, Catherine Tranchand . The Maxaboom Project Contributors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @name: Account Theme Page
* @codename: accountThemePage
* @type: Script
* @author: Abraham Ukachi <[email protected]>
* @contributors: Axel Vair <[email protected]>, Morgane Marechal <[email protected]>, Catherine Tranchand <[email protected]>
*
* Example usage:
*
* 1-|> //
* -|>
* -|>
*
*/
// Import a couple of important stuff ;)
import { SUCCESS_TOAST, GOOD_TOAST, BAD_TOAST, ERROR_TOAST, NORMAL_TOAST } from '../app.js'; // <- toasts
import { MAIN_PART, ASIDE_PART, FULL_PART } from '../app.js'; // <- parts
import { accountPage } from './account.js'; // <- account page
"use strict";
// ^^^^^^^^^ This keeps us on our toes, as it forces us to use all pre-defined variables, among other things 😅
/**
* Class representing the account theme page
*/
class AccountThemePage {
/**
* The constructor of the class
*/
constructor() {
// install the event listeners
this._installEventListeners();
}
// PUBLIC SETTERS
// PUBLIC GETTERS
/**
* Returns all the theme elements
*
* @returns { NodeList<Element> }
*/
get themeEls() {
return document.querySelectorAll('li.theme');
}
// PUBLIC METHODS
/**
* Method used to update the theme with the given `value`
*
* @param { String } value
*
* @returns { Promise }
*/
updateTheme(value) {
return new Promise(async(resolve, reject) => {
// define the logout URL
const url = `account/theme/${value}`; // <- the way it is for now ;)
// create a POST request object with `url`
const request = new Request(url, {method: 'PATCH'});
// send the request and handle the response as `requestResponse`
const requestResponse = await fetch(request);
// get the JSON response from the `requestResponse` as `response
const response = await requestResponse.json();
// DEBUG [4dbsmaster]: tell me about the response ;)
console.log(`\x1b[40m;\x1b[33m[updateTheme]: response => \x1b[0m`, response);
// If the response or update was successfull...
if (response.success) {
// ...resolve the promise with the response
resolve(response);
} else {
// ...reject the promise with the response
reject(response);
}
});
}
/**
* Notifies the theme page of a recent update
*/
notifyUpdate() {
// Get the current app theme as `theme`
const theme = mbApp.theme;
// Loop through all the theme elements
for (let themeEl of this.themeEls) {
// get the theme's id from `themeEl` as `themeId`
let themeId = themeEl.dataset.id;
// If the id of this `themeEl` is the same as the app's theme...
if (themeId === mbApp.theme) {
// ...select it
themeEl.setAttribute('selected', '');
continue; // <- go to the next theme element
}
themeEl.removeAttribute('selected');
}
}
// PRIVATE SETTERS
// PRIVATE GETTERS
// PRIVATE METHODS
/**
* Method used to install event listeners on the account's theme page
*/
_installEventListeners() {
// loop through all the theme elements
for (let themeEl of this.themeEls) {
// install a click event listener on each theme element
themeEl.addEventListener('click', this._themeClickHandler.bind(this));
}
}
/**
* Handler that is called wheneve a theme is clicked
*
* @param { PointerEvent } event - The event that triggered the handler
*
*/
_themeClickHandler(event) {
// Get the theme element from event
const themeEl = event.currentTarget;
// Check if this theme has been selected
let selected = themeEl.hasAttribute('selected');
// Do nothing if this theme has already been selected
if (selected) { return }
// Get the theme from `themeEl`
let theme = themeEl.dataset.id;
// Update the theme
this.updateTheme(theme).then((response) => {
// Set the app 'theme'
mbApp.updateTheme(response.theme);
// show a toast for 3 seconds, with the message
mbApp.showToast({
message: response.message,
type: SUCCESS_TOAST,
part: ASIDE_PART
}, 3, true);
// notify the theme page of this update
this.notifyUpdate();
});
// DEBUG [4dbsmaster]: tell me about it ;)
console.log(`\x1b[33[_themeClickHandler]: theme => %s\x1b[0m`, theme);
}
}
// Instantiate the class as `account`
let accountThemePage = new AccountThemePage();
// Export the `accountThemePage`
export { accountThemePage };