delete list interactivity + fix create empty list

This commit is contained in:
Sam Hadow 2024-02-19 00:49:36 +01:00
parent 094439eb73
commit ba72d8bd35
7 changed files with 109 additions and 26 deletions

View File

@ -72,8 +72,9 @@ def createList():
items = data.get('items') items = data.get('items')
list_uuid = create_list(name, description) list_uuid = create_list(name, description)
if list_uuid: if list_uuid:
for item in items.split('#'): if len(items)>0 :
add_to_list(list_uuid, item) for item in items.split('#'):
add_to_list(list_uuid, item)
return jsonify({'created list ': name}), 200 return jsonify({'created list ': name}), 200
else: else:
return jsonify({'create failed': name}), 400 return jsonify({'create failed': name}), 400

View File

@ -270,7 +270,7 @@ def delete_list(list_uuid):
DELETE DELETE
FROM itemlist FROM itemlist
WHERE uuid = %s WHERE uuid = %s
""", (uuid,)) """, (list_uuid,))
cursor.close() cursor.close()
connection.commit() connection.commit()
connection.close() connection.close()

View File

@ -64,6 +64,16 @@
</div> </div>
</div> </div>
<!-- popup delete list -->
<div id="deleteListPopup" class="popup">
<div class="popup-content">
<p>Delete list</p>
<div id="checkbox-container2"></div>
<button id="delete_list_ok" type="button" class="btn btn-secondary">ok</button>
<button id="delete_list_cancel" type="button" class="btn btn-secondary">cancel</button>
</div>
</div>
<!--popup confirmation window--> <!--popup confirmation window-->
<div id="confirmationPopup" class="popup"> <div id="confirmationPopup" class="popup">
<div class="popup-content"> <div class="popup-content">
@ -80,5 +90,5 @@
<script src="{{ url_for('static', filename='fetch.js') }}"></script> <script src="{{ url_for('static', filename='fetch.js') }}"></script>
<script src="{{ url_for('static', filename='popups.js') }}"></script> <script src="{{ url_for('static', filename='popups.js') }}"></script>
<script src="{{ url_for('static', filename='rendergraphs.js') }}"></script> <script src="{{ url_for('static', filename='rendergraphs.js') }}"></script>
<script src="{{ url_for('static', filename='list.js') }}"></script>
<script src="{{ url_for('static', filename='app.js') }}"></script> <script src="{{ url_for('static', filename='app.js') }}"></script>

View File

@ -163,30 +163,30 @@ const apiUrl = `${currentUrl}app/show`;
async function delItem(uuid) { async function delItem(uuid) {
const apiUrl = `${currentUrl}app/delete`; const apiUrl = `${currentUrl}app/delete`;
const postData = { const postData = {
uuid: uuid uuid: uuid
}; };
const requestOptions = { const requestOptions = {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(postData) body: JSON.stringify(postData)
}; };
const response = await fetch(apiUrl, requestOptions) const response = await fetch(apiUrl, requestOptions)
.catch(error => { .catch(error => {
console.error('Error during POST request:', error); console.error('Error during POST request:', error);
}); });
if (response.ok) { if (response.ok) {
console.log(response); console.log(response);
// refresh graphs to stop displaying deleted item // refresh graphs to stop displaying deleted item
refresh_graphs(); refresh_graphs();
} else { } else {
throw new Error('Error in server response'); throw new Error('Error in server response');
} }
} }
// function to create a list // function to create a list
@ -224,6 +224,8 @@ async function addList() {
}); });
if (response.ok) { if (response.ok) {
// refresh filters checkboxes
filters_checkboxes();
createListPopup.style.display = 'none'; createListPopup.style.display = 'none';
console.log(response); console.log(response);
} else { } else {

25
web/list.js Normal file
View File

@ -0,0 +1,25 @@
async function delItemlist(uuid) {
const apiUrl = `${currentUrl}app/delete-list`;
const postData = {
list_uuid: uuid
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(postData)
};
const response = await fetch(apiUrl, requestOptions)
.catch(error => {
console.error('Error during POST request:', error);
});
if (response.ok) {
console.log(response);
} else {
throw new Error('Error in server response');
}
}

View File

@ -52,7 +52,47 @@ document.getElementById("create_list_cancel").addEventListener("click", function
// modify list // modify list
// delete list
document.getElementById("delete_list").addEventListener("click", function () {
// remove div content
const node = document.getElementById("checkbox-container2");
while (node.firstChild) {
node.removeChild(node.lastChild);
}
// generate new checkboxes
const checkboxContainer = d3.select("#checkbox-container2");
fetch_list().then(function(data){
for (const itemlist of data) {
const div = checkboxContainer.append("div");
var label = div.append("label");
var input = label.append("input")
.attr("type", "checkbox")
.attr("class", "checkbox-delete-list")
.attr("id", `checkbox-${itemlist.uuid}`);
label.append("span")
.text(`${itemlist.name}`);
div.append("br");
}
})
deleteListPopup.style.display = 'flex';
});
// confirm delete list
document.getElementById("delete_list_ok").addEventListener("click", function () {
const checked_delete = getCheckedCheckboxIds('.checkbox-delete-list');
const id_array = checked_delete.map(str => str.substring(9));
for (const uuid of id_array) {
delItemlist(uuid);
}
// refresh filters checkboxes
filters_checkboxes();
deleteListPopup.style.display = 'none';
});
// cancel list delete
document.getElementById("delete_list_cancel").addEventListener("click", function () {
deleteListPopup.style.display = 'none';
});
// delete item, pop-up confirm // delete item, pop-up confirm
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {

View File

@ -43,7 +43,7 @@ body {
} }
#checkbox-container { #checkbox-container {
max-height: 200px; max-height: 50%;
overflow-y: auto; overflow-y: auto;
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
@ -56,3 +56,8 @@ body {
align-items: center; align-items: center;
} }
#checkbox-container2 {
max-height: 50%;
overflow-y: auto;
}