File size: 2,456 Bytes
152b33a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    const apiConfigForm = document.getElementById('apiConfigForm');
    
    // Cargar configuraci贸n guardada
    loadSavedConfig();

    apiConfigForm.addEventListener('submit', async function(e) {
        e.preventDefault();
        
        const config = {
            openai_key: document.getElementById('openaiKey').value,
            huggingface_key: document.getElementById('huggingfaceKey').value,
            elevenlabs_key: document.getElementById('elevenLabsKey').value
        };

        try {
            const response = await fetch('/save_config', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(config)
            });

            if (response.ok) {
                showAlert('success', 'Configuraci贸n guardada exitosamente');
            } else {
                showAlert('danger', 'Error al guardar la configuraci贸n');
            }
        } catch (error) {
            console.error('Error:', error);
            showAlert('danger', 'Error al guardar la configuraci贸n');
        }
    });

    async function loadSavedConfig() {
        try {
            const response = await fetch('/get_config');
            if (response.ok) {
                const config = await response.json();
                document.getElementById('openaiKey').value = config.openai_key || '';
                document.getElementById('huggingfaceKey').value = config.huggingface_key || '';
                document.getElementById('elevenLabsKey').value = config.elevenlabs_key || '';
            }
        } catch (error) {
            console.error('Error al cargar la configuraci贸n:', error);
        }
    }

    function showAlert(type, message) {
        const alertDiv = document.createElement('div');
        alertDiv.className = `alert alert-${type} alert-dismissible fade show`;
        alertDiv.role = 'alert';
        alertDiv.innerHTML = `
            ${message}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        `;
        
        const form = document.getElementById('apiConfigForm');
        form.insertAdjacentElement('beforebegin', alertDiv);

        // Auto-cerrar la alerta despu茅s de 3 segundos
        setTimeout(() => {
            alertDiv.remove();
        }, 3000);
    }
});