File size: 7,190 Bytes
c72f0c4
 
 
 
 
95dafc9
 
 
c72f0c4
 
 
 
 
95dafc9
e19913f
 
 
c72f0c4
 
e19913f
95dafc9
 
c72f0c4
e19913f
c72f0c4
 
 
954a8a4
e19913f
 
c72f0c4
 
 
 
 
95dafc9
c72f0c4
 
95dafc9
c72f0c4
 
 
 
 
 
 
 
78a2bad
c72f0c4
 
 
78a2bad
 
954a8a4
c72f0c4
 
95dafc9
 
e19913f
 
954a8a4
c72f0c4
 
95dafc9
 
e19913f
7abe33d
95dafc9
 
 
 
 
 
 
 
 
 
 
 
7abe33d
 
c72f0c4
 
 
 
 
 
 
 
95dafc9
 
c72f0c4
 
 
 
95dafc9
7abe33d
 
c72f0c4
 
 
 
 
95dafc9
 
 
 
c72f0c4
95dafc9
 
c72f0c4
 
 
95dafc9
7abe33d
 
c72f0c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95dafc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7abe33d
4987d1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
document.addEventListener('DOMContentLoaded', function() {
    loadProjectDetails();
    setupEventListeners();
});

let CURRENT_PROJECT = null;
let CURRENT_VERSION = null;

function setupEventListeners() {
    document.getElementById('saveChangesBtn').addEventListener('click', saveProject);
    document.getElementById('publishVersionBtn').addEventListener('click', publishProject);
    document.getElementById('addIntentBtn').addEventListener('click', showAddIntentModal);
    document.getElementById('newVersionBtn').addEventListener('click', showNewVersionModal);
    document.getElementById('newProjectBtn').addEventListener('click', showNewProjectModal);
}

function loadProjectDetails() {
    fetch('/project/details')
        .then(response => response.json())
        .then(data => {
            CURRENT_PROJECT = data.name;
            CURRENT_VERSION = data.version;
            renderProjectDetails(data);
        })
        .catch(error => {
            console.error('Error loading project details:', error);
            alert('Failed to load project details.');
        });
}

function renderProjectDetails(data) {
    const container = document.getElementById('projectDetails');
    container.innerHTML = '';

    const published = data.published ? 'βœ… Yes' : '❌ No';
    container.innerHTML += `<p><strong>Project:</strong> ${data.name}</p>`;
    container.innerHTML += `<p><strong>Version:</strong> ${data.version}</p>`;
    container.innerHTML += `<p><strong>Published:</strong> ${published}</p>`;

    const list = document.createElement('ul');
    data.intents.forEach(intent => {
        const li = document.createElement('li');
        li.style.marginBottom = '8px';
        li.innerHTML = `${intent.name} 
            <button class="btn btn-sm btn-primary" onclick="editIntent('${intent.id}')">Edit</button>
            <button class="btn btn-sm btn-danger" onclick="removeIntent('${intent.id}')">-</button>`;
        list.appendChild(li);
    });
    container.appendChild(list);

    document.getElementById('publishVersionBtn').disabled = data.published;
}

function saveProject() {
    fetch('/project/update', { method: 'POST' })
        .then(response => response.json())
        .then(() => alert('Project saved!'))
        .catch(() => alert('Failed to save project.'));
}

function publishProject() {
    fetch('/project/publish', { method: 'POST' })
        .then(response => response.json())
        .then(() => alert('Project published!'))
        .catch(() => alert('Failed to publish project.'));
}

function showNewProjectModal() {
    const name = prompt('Enter new project name:');
    if (name) {
        fetch('/project/new', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ name })
        })
        .then(r => r.json())
        .then(() => alert('Project created!'))
        .catch(() => alert('Failed to create project.'));
    }
}

function showNewVersionModal() {
    fetch('/project/versions')
        .then(response => response.json())
        .then(versions => {
            const select = document.getElementById('baseVersionSelect');
            select.innerHTML = '';
            versions.forEach(v => {
                const option = document.createElement('option');
                option.value = v.no;
                option.textContent = `Version ${v.no}`;
                select.appendChild(option);
            });
            $('#newVersionModal').modal('show');
        })
        .catch(() => alert('Failed to load versions.'));
}

function createNewVersion() {
    const baseVersionId = document.getElementById('baseVersionSelect').value;
    fetch('/project/new-version', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            project_name: CURRENT_PROJECT,
            base_version_no: parseInt(baseVersionId)
        })
    })
    .then(r => r.json())
    .then(() => {
        alert('New version created!');
        loadProjectDetails();
    })
    .catch(() => alert('Failed to create new version.'));
}

function loadSparkProjects() {
    fetch('/spark/projects')
        .then(response => response.json())
        .then(data => {
            if (data.error) {
                alert('⚠ Spark service unreachable.');
                return;
            }
            const list = document.getElementById('sparkProjectList');
            list.innerHTML = '';
            data.projects.forEach(p => {
                const li = document.createElement('li');
                li.textContent = p.name;
                list.appendChild(li);
            });
        })
        .catch(() => alert('⚠ Spark service unreachable.'));
}

function showAddIntentModal() {
    $('#addIntentModal').modal('show');
}

function addHeaderRow() {
    const container = document.getElementById('headerContainer');
    const row = document.createElement('div');
    row.className = 'header-row mb-2';
    row.innerHTML = `
        <input type="text" class="form-control d-inline-block header-key" placeholder="Key" style="width: 40%">
        <input type="text" class="form-control d-inline-block header-value" placeholder="Value" style="width: 40%">
        <button class="btn btn-danger btn-sm" onclick="this.parentElement.remove()">-</button>`;
    container.appendChild(row);
}

function saveAPI() {
    const name = document.getElementById('apiNameInput').value;
    const url = document.getElementById('apiUrlInput').value;
    const method = document.getElementById('apiMethodSelect').value;
    const auth = document.getElementById('apiAuthInput').value;
    const headers = [];

    document.querySelectorAll('.header-row').forEach(row => {
        const key = row.querySelector('.header-key').value;
        const value = row.querySelector('.header-value').value;
        if (key && value) {
            headers.push({ key, value });
        }
    });

    const apiData = { name, url, method, auth, headers };

    fetch('/project/update-api', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
            project_name: CURRENT_PROJECT,
            version_no: CURRENT_VERSION,
            api_data: apiData
        })
    })
    .then(r => r.json())
    .then(() => alert('API updated!'))
    .catch(() => alert('Failed to update API.'));
}

function login() {
    const username = document.getElementById('usernameInput').value;
    const password = document.getElementById('passwordInput').value;

    fetch('/auth/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username, password })
    })
    .then(response => {
        if (response.ok) {
            document.getElementById('loginPanel').style.display = 'none';
            document.getElementById('mainPanel').style.display = 'block';
            loadProjectDetails();
        } else {
            document.getElementById('loginError').style.display = 'block';
        }
    })
    .catch(() => {
        document.getElementById('loginError').style.display = 'block';
    });
}