create list interactivity
This commit is contained in:
parent
c540e2f497
commit
9214896c75
14
src/app.py
14
src/app.py
@ -11,14 +11,14 @@ CORS(app)
|
|||||||
def serve_index():
|
def serve_index():
|
||||||
return render_template('app.html')
|
return render_template('app.html')
|
||||||
|
|
||||||
@app.route('/app/init', methods = ['GET'])
|
@app.route('/init', methods = ['GET'])
|
||||||
def init_db():
|
def init_db():
|
||||||
status = check_schema()
|
status = check_schema()
|
||||||
if not status:
|
if not status:
|
||||||
initialize()
|
initialize()
|
||||||
return str(status)
|
return str(status)
|
||||||
|
|
||||||
@app.route('/app/update', methods = ['GET'])
|
@app.route('/update', methods = ['GET'])
|
||||||
def update_hist():
|
def update_hist():
|
||||||
print("update")
|
print("update")
|
||||||
update_items()
|
update_items()
|
||||||
@ -69,8 +69,14 @@ def createList():
|
|||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
name = data.get('name')
|
name = data.get('name')
|
||||||
description = data.get('description')
|
description = data.get('description')
|
||||||
create_list(name, description)
|
items = data.get('items')
|
||||||
return jsonify({'created list ': name}), 200
|
list_uuid = create_list(name, description)
|
||||||
|
if list_uuid:
|
||||||
|
for item in items.split('#'):
|
||||||
|
add_to_list(list_uuid, item)
|
||||||
|
return jsonify({'created list ': name}), 200
|
||||||
|
else:
|
||||||
|
return jsonify({'create failed': name}), 400
|
||||||
|
|
||||||
@app.route('/app/add-to-list', methods=['POST'])
|
@app.route('/app/add-to-list', methods=['POST'])
|
||||||
def addToList():
|
def addToList():
|
||||||
|
31
src/db.py
31
src/db.py
@ -164,19 +164,38 @@ def delete_item(uuid):
|
|||||||
def create_list(name, description):
|
def create_list(name, description):
|
||||||
'''
|
'''
|
||||||
initialize an itemlist in the database
|
initialize an itemlist in the database
|
||||||
|
return list uuid
|
||||||
|
return None if a list with this name already exist
|
||||||
'''
|
'''
|
||||||
if len(name) <= 50:
|
if len(name) <= 50:
|
||||||
connection = connect_db()
|
connection = connect_db()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
|
|
||||||
cursor.execute("""
|
cursor.execute("""
|
||||||
INSERT INTO itemlist (uuid, name, description)
|
SELECT uuid
|
||||||
VALUES (nextval('uuid_list_sequence'), %s, %s)
|
FROM itemlist
|
||||||
""", (name, description))
|
WHERE name = %s
|
||||||
|
""", (name,))
|
||||||
|
|
||||||
|
result = cursor.rowcount
|
||||||
|
|
||||||
|
if result == 0:
|
||||||
|
cursor.execute("""
|
||||||
|
INSERT INTO itemlist (uuid, name, description)
|
||||||
|
VALUES (nextval('uuid_list_sequence'), %s, %s)
|
||||||
|
""", (name, description))
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT uuid
|
||||||
|
FROM itemlist
|
||||||
|
WHERE name = %s
|
||||||
|
""", (name,))
|
||||||
|
result = cursor.fetchall()[0][0]
|
||||||
|
else:
|
||||||
|
result = None
|
||||||
|
|
||||||
connection.commit()
|
connection.commit()
|
||||||
connection.close()
|
connection.close()
|
||||||
return 1
|
return result
|
||||||
else:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def add_to_list(list_uuid, item_uuid):
|
def add_to_list(list_uuid, item_uuid):
|
||||||
'''
|
'''
|
||||||
|
@ -28,6 +28,7 @@
|
|||||||
<p>Create new list</p>
|
<p>Create new list</p>
|
||||||
<input id="listName" type="text" placeholder="name">
|
<input id="listName" type="text" placeholder="name">
|
||||||
<input id="listDescription" type="text" placeholder="description">
|
<input id="listDescription" type="text" placeholder="description">
|
||||||
|
<button id="create_list_add" type="button">add</button>
|
||||||
<button id="create_list_cancel" type="button">cancel</button>
|
<button id="create_list_cancel" type="button">cancel</button>
|
||||||
<p>Include items</p>
|
<p>Include items</p>
|
||||||
<div id="checkbox-container"></div>
|
<div id="checkbox-container"></div>
|
||||||
|
57
web/app.js
57
web/app.js
@ -141,3 +141,60 @@ async function delItem(uuid) {
|
|||||||
throw new Error('Error in server response');
|
throw new Error('Error in server response');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function to create a list
|
||||||
|
document.getElementById("create_list_add").addEventListener("click", addList);
|
||||||
|
async function addList() {
|
||||||
|
|
||||||
|
const checked = getCheckedCheckboxIds('.checkbox-itemlist');
|
||||||
|
const items_uuid = checked.map(str => str.substring(9)).join('#');
|
||||||
|
|
||||||
|
const listname = document.getElementById("listName").value;
|
||||||
|
|
||||||
|
if (listname.length !== 0) {
|
||||||
|
const apiUrl = `${currentUrl}app/create-list`;
|
||||||
|
const postData = {
|
||||||
|
name: listname,
|
||||||
|
description: document.getElementById("listDescription").value,
|
||||||
|
items: items_uuid
|
||||||
|
};
|
||||||
|
|
||||||
|
// clear input fields
|
||||||
|
document.getElementById("listName").value='';
|
||||||
|
document.getElementById("listDescription").value='';
|
||||||
|
|
||||||
|
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) {
|
||||||
|
createListPopup.style.display = 'none';
|
||||||
|
console.log(response);
|
||||||
|
} else {
|
||||||
|
throw new Error('Error in server response');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.log("cannot create list without a name")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// get an array of id of checked checkboxes of a specific class name
|
||||||
|
function getCheckedCheckboxIds(classname) {
|
||||||
|
const checkboxes = d3.selectAll(classname);
|
||||||
|
const checkedIds = checkboxes.filter(function() {
|
||||||
|
return this.checked;
|
||||||
|
}).nodes().map(function(checkbox) {
|
||||||
|
return checkbox.id;
|
||||||
|
});
|
||||||
|
return checkedIds;
|
||||||
|
}
|
||||||
|
@ -9,15 +9,12 @@ document.addEventListener("keydown", (event) => {
|
|||||||
|
|
||||||
// create new list
|
// create new list
|
||||||
document.getElementById("create_list").addEventListener("click", function () {
|
document.getElementById("create_list").addEventListener("click", function () {
|
||||||
|
|
||||||
|
|
||||||
// remove div content
|
// remove div content
|
||||||
const node = document.getElementById("checkbox-container");
|
const node = document.getElementById("checkbox-container");
|
||||||
while (node.firstChild) {
|
while (node.firstChild) {
|
||||||
node.removeChild(node.lastChild);
|
node.removeChild(node.lastChild);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// generate new checkboxes
|
// generate new checkboxes
|
||||||
const checkboxContainer = d3.select("#checkbox-container");
|
const checkboxContainer = d3.select("#checkbox-container");
|
||||||
fetch_item().then(function(data){
|
fetch_item().then(function(data){
|
||||||
@ -27,10 +24,8 @@ document.getElementById("create_list").addEventListener("click", function () {
|
|||||||
|
|
||||||
const checkbox = div.append("input")
|
const checkbox = div.append("input")
|
||||||
.attr("type", "checkbox")
|
.attr("type", "checkbox")
|
||||||
.attr("id", `checkbox-${uuid}`)
|
.attr("class", "checkbox-itemlist")
|
||||||
.on("change", function() {
|
.attr("id", `checkbox-${uuid}`);
|
||||||
console.log(this.checked ? uuid : null);
|
|
||||||
});
|
|
||||||
|
|
||||||
const link = `https://fr.aliexpress.com/item/${item.itemid}.html`;
|
const link = `https://fr.aliexpress.com/item/${item.itemid}.html`;
|
||||||
const image = div.append("img")
|
const image = div.append("img")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user