diff --git a/src/app.py b/src/app.py index 1af8e26..dd03d13 100644 --- a/src/app.py +++ b/src/app.py @@ -108,6 +108,44 @@ def deleteList(): except: return str('item not found in list'), 400 +@app.route('/app/change-list-name', methods=['POST']) +def changeListName(): + data = request.get_json() + name = data.get('name') + uuid = data.get('uuid') + try: + change_list_name(uuid, name) + return jsonify({'name changed': uuid}), 200 + except: + return str('error changing name'), 400 + +@app.route('/app/change-list-description', methods=['POST']) +def changeListDescription(): + data = request.get_json() + description = data.get('description') + uuid = data.get('uuid') + try: + change_list_description(uuid, description) + return jsonify({'name changed': uuid}), 200 + except: + return str('error changing description'), 400 + +@app.route('/app/change-list-content', methods=['POST']) +def changeListContent(): + data = request.get_json() + uuid = data.get('uuid') + new = data.get('content') + try: + remove_all_from_list(uuid) + if len(new)>0 : + for item in new.split('#'): + add_to_list(uuid, item) + return jsonify({'content changed': uuid}), 200 + except: + return str('error changing content'), 400 + + + @app.route('/app/datahistory',methods = ['GET']) def data_history_request(): if request.method == 'GET': @@ -142,11 +180,17 @@ def data_history_request_filtered(): lists_uuid = [] return jsonify(get_history_filtered(lists_uuid)) -@app.route('/app/datalist',methods = ['GET']) +@app.route('/app/datalist',methods = ['GET', 'POST']) def data_list_request(): if request.method == 'GET': print("fetching data item") return jsonify(get_lists()) + elif request.method == 'POST': + data = request.get_json() + list_uuid = data.get('uuid') + details = get_list(list_uuid) + return jsonify(details), 200 + if __name__ == '__main__': app.run(debug = True) diff --git a/src/db.py b/src/db.py index a126a5a..841de49 100644 --- a/src/db.py +++ b/src/db.py @@ -304,6 +304,19 @@ def remove_from_list(list_uuid, item_uuid): connection.commit() connection.close() +def remove_all_from_list(list_uuid): + '''remove every items from an itemlist''' + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + DELETE + FROM listcontent + WHERE list_uuid = %s + """, (list_uuid,)) + cursor.close() + connection.commit() + connection.close() + def get_lists(): connection = connect_db() cursor = connection.cursor() @@ -316,6 +329,46 @@ def get_lists(): connection.close() return results +def get_list(list_uuid): + '''return details of a single list''' + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + SELECT name, description + FROM itemlist + WHERE uuid = %s + """, (list_uuid,)) + results = cursor.fetchall() + cursor.close() + connection.close() + return results[0] + +def change_list_name(uuid, name): + '''modify itemlist name''' + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + UPDATE itemlist + SET name = %s + WHERE uuid = %s + """, (name, uuid)) + cursor.close() + connection.commit() + connection.close() + +def change_list_description(uuid, description): + '''modify itemlist description''' + connection = connect_db() + cursor = connection.cursor() + cursor.execute(""" + UPDATE itemlist + SET description = %s + WHERE uuid = %s + """, (description, uuid)) + cursor.close() + connection.commit() + connection.close() + def export_csv(): '''join item and history data from database and export it in ./output.csv''' connection = connect_db() diff --git a/web/app.html b/web/app.html index 0906009..153ac81 100644 --- a/web/app.html +++ b/web/app.html @@ -60,7 +60,7 @@
Include items
- + @@ -69,14 +69,18 @@Modify list
Include items
- // // remove div content const node = document.getElementById("modifylist_content"); while (node.firstChild) { node.removeChild(node.lastChild); } - // input name - // input description - // content + // new content + previous_modify.uuid = itemlist_uuid; const existing_content = Object.keys(await fetch_item_filtered([itemlist_uuid])); + previous_modify.content = existing_content; + const list_data = await get_list_details(itemlist_uuid); const modify_content = d3.select("#modifylist_content"); - const checkboxContainer = modify_content.append("div"); + // dropdown menu text + document.getElementById("modify_itemlist_dropdown").innerHTML = `${list_data.name}`; + // input name + previous_modify.name = list_data.name; + const input_name = modify_content.append("input") + .attr("type", "text") + .attr("placeholder", "name") + .attr("value", `${list_data.name}`) + .attr("id", `input_name-${itemlist_uuid}`); + // input description + const input_description = modify_content.append("input") + .attr("type", "text") + .attr("placeholder", "description") + .attr("value", `${list_data.description}`) + .attr("id", `input_description-${itemlist_uuid}`); + previous_modify.description = list_data.description; + // content + const checkboxContainer = modify_content.append("div") + .attr("class", "item_checkboxes"); fetch_item().then(function(data){ Object.keys(data).forEach(uuid => { const item = data[uuid]; @@ -150,7 +165,7 @@ async function toggle_modify(itemlist_uuid) { const checkbox = div.append("input") .attr("type", "checkbox") - .attr("class", "checkbox-itemlist") + .attr("class", "checkbox-modify-itemlist") .attr("id", `checkbox-${uuid}`); if (existing_content.includes(`${uuid}`)) { checkbox.property("checked", true); @@ -167,6 +182,35 @@ async function toggle_modify(itemlist_uuid) { }); }); } +// confirm list modify +document.getElementById("modifylist_ok").addEventListener("click", async function () { + const uuid = previous_modify.uuid; + const new_name = document.getElementById(`input_name-${uuid}`).value; + const new_description = document.getElementById(`input_description-${uuid}`).value; + const checked = getCheckedCheckboxIds('.checkbox-modify-itemlist'); + const new_content = checked.map(str => str.substring(9)); + var diff = false; + if (new_name != previous_modify.name) { + // name different + diff = true; + await changeListName(uuid, new_name); + } + if (new_description != previous_modify.description) { + // description different + diff = true; + await changeListDescription(uuid, new_description); + } + if (!(new_content.every(item => previous_modify.content.includes(item)) && previous_modify.content.every(item => new_content.includes(item)))) { + // content different + diff = true; + await changeListContent(uuid, new_content); + } + if (diff) { + // refresh filters checkboxes + filters_checkboxes(); + } + modifyListPopup.style.display = 'none'; + }); // delete item, pop-up confirm document.addEventListener('DOMContentLoaded', function () { diff --git a/web/style.css b/web/style.css index 32b3c85..917c4a4 100644 --- a/web/style.css +++ b/web/style.css @@ -42,14 +42,14 @@ body { padding: 0px; } -#checkbox-container { +.item_checkboxes { max-height: 50%; overflow-y: auto; display: flex; flex-wrap: wrap; } -#checkbox-container div { +.item_checkboxes div { margin-bottom: 10px; width: calc(50% - 10px); display: flex;