-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccount-wallet.js
167 lines (130 loc) · 4.95 KB
/
account-wallet.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
/*
* @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 Info Page
* @codename: accountInfoPage
* @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 info page
*/
class AccountInfoPage {
/**
* The constructor of the class
*/
constructor() {
// install the event listeners
this._installEventListeners();
}
// PUBLIC SETTERS
// PUBLIC GETTERS
// PUBLIC METHODS
// PRIVATE SETTERS
// PRIVATE GETTERS
// PRIVATE METHODS
/**
* Method used to install event listeners on the account page
*/
_installEventListeners() {
/*
// If there are logout and delete account buttons on the page...
if (this.logoutButton && this.deleteAccountButton) {
// ...install click event listener on the logout and delete account button,
// using / binding `this` to preserve our PointerEvent ;)
this.logoutButton.addEventListener('click', this._logoutButtonClickHandler.bind(this));
this.deleteAccountButton.addEventListener('click', this._deleteAccountButtonClickHandler.bind(this));
}
*/
}
/**
* Handler that is called whenever the logout button is clicked
*
* @param { PointerEvent } event - The event that triggered the handler
*
* @returns { void }
* @private
*/
_logoutButtonClickHandler(event) {
// open a logout dialog in the main part
mbApp.openDialog({
title: mbApp.i18n.getString('log_out'),
message: mbApp.i18n.getString('logoutConfirmMessage'),
confirmBtnText: mbApp.i18n.getString('yes'),
cancelBtnText: mbApp.i18n.getString('no'),
onConfirm: this._logout.bind(this),
onCancel: () => { mbApp.closeDialog(); },
isCancelable: true
}, 0.5);
}
/**
* Method used to logout the user
*
* @param { PointerEvent } event - The event that triggered the handler
* @returns { void }
*/
async _logout(event) {
// define the logout URL
// const logoutUrl = `${mbApp.config.apiBaseUrl}/logout`; // <- the way it should be
const logoutUrl = `account/logout`; // <- the way it is for now ;)
// create a POST request object with `logoutUrl`
const request = new Request(logoutUrl, {method: 'POST'});
// 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[_logout]: response => \x1b[0m`, response);
// close the dialog
mbApp.closeDialog();
// show a toast for 3 seconds, with the response message
mbApp.showToast({
message: response.message,
type: response.success ? SUCCESS_TOAST : ERROR_TOAST
}, 3, FULL_PART)
.then(() => {
// if the response was successful, redirect the user to the home page
if (response.success) {
location.replace('home');
}
});
}
}
// Instantiate the class as `account`
let accountInfoPage = new AccountInfoPage();
// Export the class as `accountPage`
export { accountInfoPage };