File size: 4,891 Bytes
b72f5af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Copyright (C) 2024 Puter Technologies Inc.
 *
 * This file is part of Puter.
 *
 * Puter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import UIWindow from './UIWindow.js'
import UIAlert from './UIAlert.js'

function UIWindowRecoverPassword(options){
    return new Promise(async (resolve) => {
        options = options ?? {};

        let h = '';
        h += `<div style="-webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; color: #3e5362;">`;
            h += `<h3 style="text-align:center; font-weight: 400; font-size: 20px;">Recover Password</h3>`;
            h += `<form class="pass-recovery-form">`;
                h += `<p style="text-align:center; padding: 0 20px;"></p>`;
                h += `<div class="error"></div>`;
                h += `<label>Email or Username</label>`;
                h += `<input class="pass-recovery-username-or-email" type="text"/>`;
                h += `<button type="submit" class="send-recovery-email button button-block button-primary" style="margin-top:10px;">Send Recovery Email</button>`;
            h += `</form>`;
        h += `</div>`;

        const el_window = await UIWindow({
            title: null,
            backdrop: options.backdrop ?? false,
            icon: null,
            uid: null,
            is_dir: false,
            body_content: h,
            has_head: options.has_head ?? true,
            selectable_body: false,
            draggable_body: true,
            allow_context_menu: false,
            is_draggable: options.is_draggable ?? true,
            is_droppable: false,
            is_resizable: false,
            stay_on_top: options.stay_on_top ?? false,
            allow_native_ctxmenu: true,
            allow_user_select: true,
            width: 350,
            dominant: true,
            ...options.window_options,
            onAppend: function(el_window){
                $(el_window).find('.pass-recovery-username-or-email').first().focus();
            },
            window_class: 'window-item-properties',
            window_css:{
                height: 'initial',
            },
            body_css: {
                padding: '10px',
                width: 'initial',
                height: 'initial',
                'background-color': 'rgba(231, 238, 245)',
                'backdrop-filter': 'blur(3px)',
            }
        })
        $(el_window).find('.pass-recovery-form').on('submit', function(e){
            e.preventDefault();
            e.stopPropagation();
            return false;
        })

        // Send recovery email
        $(el_window).find('.send-recovery-email').on('click', function(e){
            let email, username;
            let input = $(el_window).find('.pass-recovery-username-or-email').val();
            if(is_email(input))
                email = input;
            else
                username = input;

            // todo validation before sending
            $.ajax({
                url: api_origin + "/send-pass-recovery-email",
                type: 'POST',
                async: true,
                contentType: "application/json",
                data: JSON.stringify({
                    email: email,
                    username: username,
                }),    
                statusCode: {
                    401: function () {
                        logout();
                    },
                },        
                success: async function (res){
                    $(el_window).close();
                    await UIAlert({
                        message: res.message,
                        body_icon: window.icons['c-check.svg'],
                        stay_on_top: true,
                        backdrop: true,
                        window_options: {
                            backdrop: true,
                            close_on_backdrop_click: false,
                        }
                    })           
                },
                error: function (err){
                    $(el_window).find('.error').html(err.responseText);
                    $(el_window).find('.error').fadeIn();
                },
                complete: function(){
                }
            })
        })
    })
}

export default UIWindowRecoverPassword