Spaces:
Building
Building
Update static/js/project.js
Browse files- static/js/project.js +81 -20
static/js/project.js
CHANGED
@@ -1,20 +1,25 @@
|
|
1 |
-
// project.js
|
2 |
document.addEventListener('DOMContentLoaded', function() {
|
3 |
loadProjectDetails();
|
4 |
setupEventListeners();
|
5 |
});
|
6 |
|
|
|
|
|
|
|
7 |
function setupEventListeners() {
|
8 |
document.getElementById('saveChangesBtn').addEventListener('click', saveProject);
|
9 |
document.getElementById('publishVersionBtn').addEventListener('click', publishProject);
|
10 |
document.getElementById('addIntentBtn').addEventListener('click', showAddIntentModal);
|
11 |
document.getElementById('newVersionBtn').addEventListener('click', showNewVersionModal);
|
|
|
12 |
}
|
13 |
|
14 |
function loadProjectDetails() {
|
15 |
fetch('/project/details')
|
16 |
.then(response => response.json())
|
17 |
.then(data => {
|
|
|
|
|
18 |
renderProjectDetails(data);
|
19 |
})
|
20 |
.catch(error => {
|
@@ -28,8 +33,10 @@ function renderProjectDetails(data) {
|
|
28 |
container.innerHTML = '';
|
29 |
|
30 |
const published = data.published ? 'β
Yes' : 'β No';
|
|
|
31 |
container.innerHTML += `<p><strong>Version:</strong> ${data.version}</p>`;
|
32 |
container.innerHTML += `<p><strong>Published:</strong> ${published}</p>`;
|
|
|
33 |
const list = document.createElement('ul');
|
34 |
data.intents.forEach(intent => {
|
35 |
const li = document.createElement('li');
|
@@ -47,20 +54,29 @@ function renderProjectDetails(data) {
|
|
47 |
function saveProject() {
|
48 |
fetch('/project/update', { method: 'POST' })
|
49 |
.then(response => response.json())
|
50 |
-
.then(
|
51 |
-
.catch(
|
52 |
}
|
53 |
|
54 |
function publishProject() {
|
55 |
fetch('/project/publish', { method: 'POST' })
|
56 |
.then(response => response.json())
|
57 |
-
.then(
|
58 |
-
.catch(
|
59 |
}
|
60 |
|
61 |
-
function
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
}
|
65 |
|
66 |
function showNewVersionModal() {
|
@@ -71,15 +87,13 @@ function showNewVersionModal() {
|
|
71 |
select.innerHTML = '';
|
72 |
versions.forEach(v => {
|
73 |
const option = document.createElement('option');
|
74 |
-
option.value = v.
|
75 |
-
option.textContent = `Version ${v.
|
76 |
select.appendChild(option);
|
77 |
});
|
78 |
$('#newVersionModal').modal('show');
|
79 |
})
|
80 |
-
.catch(
|
81 |
-
alert('Failed to load versions.');
|
82 |
-
});
|
83 |
}
|
84 |
|
85 |
function createNewVersion() {
|
@@ -87,14 +101,17 @@ function createNewVersion() {
|
|
87 |
fetch('/project/new-version', {
|
88 |
method: 'POST',
|
89 |
headers: { 'Content-Type': 'application/json' },
|
90 |
-
body: JSON.stringify({
|
|
|
|
|
|
|
91 |
})
|
92 |
-
.then(
|
93 |
-
.then(
|
94 |
alert('New version created!');
|
95 |
loadProjectDetails();
|
96 |
})
|
97 |
-
.catch(
|
98 |
}
|
99 |
|
100 |
function loadSparkProjects() {
|
@@ -113,7 +130,51 @@ function loadSparkProjects() {
|
|
113 |
list.appendChild(li);
|
114 |
});
|
115 |
})
|
116 |
-
.catch(
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
}
|
|
|
|
|
1 |
document.addEventListener('DOMContentLoaded', function() {
|
2 |
loadProjectDetails();
|
3 |
setupEventListeners();
|
4 |
});
|
5 |
|
6 |
+
let CURRENT_PROJECT = null;
|
7 |
+
let CURRENT_VERSION = null;
|
8 |
+
|
9 |
function setupEventListeners() {
|
10 |
document.getElementById('saveChangesBtn').addEventListener('click', saveProject);
|
11 |
document.getElementById('publishVersionBtn').addEventListener('click', publishProject);
|
12 |
document.getElementById('addIntentBtn').addEventListener('click', showAddIntentModal);
|
13 |
document.getElementById('newVersionBtn').addEventListener('click', showNewVersionModal);
|
14 |
+
document.getElementById('newProjectBtn').addEventListener('click', showNewProjectModal);
|
15 |
}
|
16 |
|
17 |
function loadProjectDetails() {
|
18 |
fetch('/project/details')
|
19 |
.then(response => response.json())
|
20 |
.then(data => {
|
21 |
+
CURRENT_PROJECT = data.name;
|
22 |
+
CURRENT_VERSION = data.version;
|
23 |
renderProjectDetails(data);
|
24 |
})
|
25 |
.catch(error => {
|
|
|
33 |
container.innerHTML = '';
|
34 |
|
35 |
const published = data.published ? 'β
Yes' : 'β No';
|
36 |
+
container.innerHTML += `<p><strong>Project:</strong> ${data.name}</p>`;
|
37 |
container.innerHTML += `<p><strong>Version:</strong> ${data.version}</p>`;
|
38 |
container.innerHTML += `<p><strong>Published:</strong> ${published}</p>`;
|
39 |
+
|
40 |
const list = document.createElement('ul');
|
41 |
data.intents.forEach(intent => {
|
42 |
const li = document.createElement('li');
|
|
|
54 |
function saveProject() {
|
55 |
fetch('/project/update', { method: 'POST' })
|
56 |
.then(response => response.json())
|
57 |
+
.then(() => alert('Project saved!'))
|
58 |
+
.catch(() => alert('Failed to save project.'));
|
59 |
}
|
60 |
|
61 |
function publishProject() {
|
62 |
fetch('/project/publish', { method: 'POST' })
|
63 |
.then(response => response.json())
|
64 |
+
.then(() => alert('Project published!'))
|
65 |
+
.catch(() => alert('Failed to publish project.'));
|
66 |
}
|
67 |
|
68 |
+
function showNewProjectModal() {
|
69 |
+
const name = prompt('Enter new project name:');
|
70 |
+
if (name) {
|
71 |
+
fetch('/project/new', {
|
72 |
+
method: 'POST',
|
73 |
+
headers: { 'Content-Type': 'application/json' },
|
74 |
+
body: JSON.stringify({ name })
|
75 |
+
})
|
76 |
+
.then(r => r.json())
|
77 |
+
.then(() => alert('Project created!'))
|
78 |
+
.catch(() => alert('Failed to create project.'));
|
79 |
+
}
|
80 |
}
|
81 |
|
82 |
function showNewVersionModal() {
|
|
|
87 |
select.innerHTML = '';
|
88 |
versions.forEach(v => {
|
89 |
const option = document.createElement('option');
|
90 |
+
option.value = v.no;
|
91 |
+
option.textContent = `Version ${v.no}`;
|
92 |
select.appendChild(option);
|
93 |
});
|
94 |
$('#newVersionModal').modal('show');
|
95 |
})
|
96 |
+
.catch(() => alert('Failed to load versions.'));
|
|
|
|
|
97 |
}
|
98 |
|
99 |
function createNewVersion() {
|
|
|
101 |
fetch('/project/new-version', {
|
102 |
method: 'POST',
|
103 |
headers: { 'Content-Type': 'application/json' },
|
104 |
+
body: JSON.stringify({
|
105 |
+
project_name: CURRENT_PROJECT,
|
106 |
+
base_version_no: parseInt(baseVersionId)
|
107 |
+
})
|
108 |
})
|
109 |
+
.then(r => r.json())
|
110 |
+
.then(() => {
|
111 |
alert('New version created!');
|
112 |
loadProjectDetails();
|
113 |
})
|
114 |
+
.catch(() => alert('Failed to create new version.'));
|
115 |
}
|
116 |
|
117 |
function loadSparkProjects() {
|
|
|
130 |
list.appendChild(li);
|
131 |
});
|
132 |
})
|
133 |
+
.catch(() => alert('β Spark service unreachable.'));
|
134 |
+
}
|
135 |
+
|
136 |
+
function showAddIntentModal() {
|
137 |
+
$('#addIntentModal').modal('show');
|
138 |
+
}
|
139 |
+
|
140 |
+
function addHeaderRow() {
|
141 |
+
const container = document.getElementById('headerContainer');
|
142 |
+
const row = document.createElement('div');
|
143 |
+
row.className = 'header-row mb-2';
|
144 |
+
row.innerHTML = `
|
145 |
+
<input type="text" class="form-control d-inline-block header-key" placeholder="Key" style="width: 40%">
|
146 |
+
<input type="text" class="form-control d-inline-block header-value" placeholder="Value" style="width: 40%">
|
147 |
+
<button class="btn btn-danger btn-sm" onclick="this.parentElement.remove()">-</button>`;
|
148 |
+
container.appendChild(row);
|
149 |
+
}
|
150 |
+
|
151 |
+
function saveAPI() {
|
152 |
+
const name = document.getElementById('apiNameInput').value;
|
153 |
+
const url = document.getElementById('apiUrlInput').value;
|
154 |
+
const method = document.getElementById('apiMethodSelect').value;
|
155 |
+
const auth = document.getElementById('apiAuthInput').value;
|
156 |
+
const headers = [];
|
157 |
+
|
158 |
+
document.querySelectorAll('.header-row').forEach(row => {
|
159 |
+
const key = row.querySelector('.header-key').value;
|
160 |
+
const value = row.querySelector('.header-value').value;
|
161 |
+
if (key && value) {
|
162 |
+
headers.push({ key, value });
|
163 |
+
}
|
164 |
+
});
|
165 |
+
|
166 |
+
const apiData = { name, url, method, auth, headers };
|
167 |
+
|
168 |
+
fetch('/project/update-api', {
|
169 |
+
method: 'POST',
|
170 |
+
headers: { 'Content-Type': 'application/json' },
|
171 |
+
body: JSON.stringify({
|
172 |
+
project_name: CURRENT_PROJECT,
|
173 |
+
version_no: CURRENT_VERSION,
|
174 |
+
api_data: apiData
|
175 |
+
})
|
176 |
+
})
|
177 |
+
.then(r => r.json())
|
178 |
+
.then(() => alert('API updated!'))
|
179 |
+
.catch(() => alert('Failed to update API.'));
|
180 |
}
|