File : dialogs/apiKeysDialog/ReloadFromServerButtonClickEL.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import PasswordDialog from '../passwordDialog/PasswordDialog.js';
26
import DataEncryptor from '../../core/lib/DataEncryptor.js';
27
import DataEncryptorHandlers from './DataEncryptorHandlers.js';
28
29
import { ZERO, ONE, HTTP_STATUS_OK } from '../../main/Constants.js';
30
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
/**
33
click event listener for the reload keys from server file button
34
*/
35
/* ------------------------------------------------------------------------------------------------------------------------- */
36
37
class ReloadFromServerButtonClickEL {
38
39
    /**
40
    A reference to the ApiKeys dialog
41
    @type {ApiKeysDialog}
42
    */
43
44
    #apiKeysDialog;
45
46
    /**
47
    A DataEncryptorHandlers object used to encode / decode the ApiKeys
48
    @type {DataEncryptorHandlers}
49
    */
50
51
    #dataEncryptorHandlers;
52
53
    /**
54
    The constructor
55
    @param {ApiKeysDialog} apiKeysDialog A reference to the ApiKeys dialog
56
    */
57
58
    constructor ( apiKeysDialog ) {
59
        Object.freeze ( this );
60
        this.#apiKeysDialog = apiKeysDialog;
61
        this.#dataEncryptorHandlers = new DataEncryptorHandlers ( this.#apiKeysDialog );
62
    }
63
64
    /**
65
    The destructor
66
    */
67
68
    destructor ( ) {
69
        this.#dataEncryptorHandlers.destructor ( );
70
        this.#apiKeysDialog = null;
71
    }
72
73
    /**
74
    Event listener method
75
    @param {Event} clickEvent The event to handle
76
    */
77
78
    handleEvent ( clickEvent ) {
79
        clickEvent.stopPropagation ( );
80
        this.#apiKeysDialog.hideError ( );
81
        this.#apiKeysDialog.showWait ( );
82
        this.#apiKeysDialog.keyboardELEnabled = false;
83
84
        fetch ( window.location.href.substring ( ZERO, window.location.href.lastIndexOf ( '/' ) + ONE ) + 'ApiKeys' )
85
            .then (
86
                response => {
87
                    if ( HTTP_STATUS_OK === response.status && response.ok ) {
88
                        response.arrayBuffer ( ).then (
89
                            data => {
90
                                new DataEncryptor ( ).decryptData (
91
                                    data,
92
                                    tmpData => { this.#dataEncryptorHandlers.onOkDecrypt ( tmpData ); },
93
                                    err => { this.#dataEncryptorHandlers.onErrorDecrypt ( err ); },
94
                                    new PasswordDialog ( false ).show ( )
95
                                );
96
                            }
97
                        );
98
                    }
99
                    else {
100
                        this.#dataEncryptorHandlers.onErrorDecrypt (
101
                            new Error ( 'Invalid http status' )
102
                        );
103
                    }
104
                }
105
            )
106
            .catch (
107
                err => {
108
                    this.#dataEncryptorHandlers.onErrorDecrypt ( err );
109
                    if ( err instanceof Error ) {
110
                        console.error ( err );
111
                    }
112
                }
113
            );
114
    }
115
}
116
117
export default ReloadFromServerButtonClickEL;
118
119
/* --- End of file --------------------------------------------------------------------------------------------------------- */
120