From 2fafcc4bebb7289290ec5ca9372cd45f620accdc Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Fri, 1 Jul 2022 23:42:38 +0200 Subject: add ability to actually tag friends --- fietsboek/routes.py | 1 + fietsboek/static/fietsboek.js | 52 ++++++++++++++++++++++++++++++-- fietsboek/templates/edit.jinja2 | 2 +- fietsboek/templates/edit_form.jinja2 | 37 ++++++++++++++++++++++- fietsboek/templates/finish_upload.jinja2 | 2 +- fietsboek/templates/layout.jinja2 | 4 +++ fietsboek/views/edit.py | 14 ++++++++- fietsboek/views/profile.py | 15 +++++++++ fietsboek/views/upload.py | 15 ++++++++- 9 files changed, 134 insertions(+), 8 deletions(-) diff --git a/fietsboek/routes.py b/fietsboek/routes.py index 018c1d3..fb8536e 100644 --- a/fietsboek/routes.py +++ b/fietsboek/routes.py @@ -33,3 +33,4 @@ def includeme(config): config.add_route('add-friend', '/me/send-friend-request') config.add_route('delete-friend', '/me/delete-friend') config.add_route('accept-friend', '/me/accept-friend') + config.add_route('json-friends', '/me/friends.json') diff --git a/fietsboek/static/fietsboek.js b/fietsboek/static/fietsboek.js index 5a26cbc..f21f0b5 100644 --- a/fietsboek/static/fietsboek.js +++ b/fietsboek/static/fietsboek.js @@ -47,10 +47,48 @@ function checkNameValidity(name) { } } +function searchFriends() { + let searchPattern = document.querySelector("#friendSearchQuery").value.toLowerCase(); + let friendSearch = document.querySelector("#friendSearch"); + friendSearch.innerHTML = ""; + fetch(FRIENDS_URL) + .then((response) => response.json()) + .then((response) => { + let blueprint = document.querySelector("#friendSearchBlueprint"); + + // Only show friends with a matching name + let friends = response.filter((obj) => obj.name.toLowerCase().indexOf(searchPattern) != -1); + + friends.forEach((friend) => { + let copy = blueprint.cloneNode(true); + copy.removeAttribute("id"); + copy.querySelector(".friend-name").textContent = friend.name; + copy.querySelector("button").addEventListener("click", (event) => { + let button = event.target.closest("button"); + button.parentNode.parentNode.removeChild(button.parentNode); + + let added = document.querySelector("#friendAddedBlueprint").cloneNode(true); + added.removeAttribute("id"); + added.querySelector(".friend-name").textContent = friend.name + added.querySelector("input").value = friend.id; + added.querySelector("input").removeAttribute("disabled"); + added.querySelector("button").addEventListener("click", removeFriendClicked); + document.querySelector('#taggedFriends').appendChild(added); + }); + friendSearch.appendChild(copy); + }); + }); +} + +function removeFriendClicked(event) { + let button = event.target.closest("button"); + button.parentNode.parentNode.removeChild(button.parentNode); +} + document.addEventListener('DOMContentLoaded', function(event) { - /* Enable the "Add tag" button */ + /* Enable the "Add tag" button in the track edit page */ let $ = (selector) => document.querySelector(selector); - let button = $("#add-tag-btn"); + var button = $("#add-tag-btn"); if (button) { button.addEventListener('click', function() { let node = document.createElement("span"); @@ -71,8 +109,16 @@ document.addEventListener('DOMContentLoaded', function(event) { $("#formTags").appendChild(space); $("#new-tag").value = ""; updateTagList(); - }) + }); + } + + /* Enable the "Add friend" button in the track edit page */ + var button = $("#add-friend-btn"); + if (button) { + button.addEventListener('click', () => searchFriends()); } + /* Also enable any "Remove friend" buttons */ + document.querySelectorAll(".remove-friend-button").forEach((t) => t.addEventListener("click", removeFriendClicked)); /* Enable clicking on a tag to remove it */ document.querySelectorAll(".tag-badge").forEach((t) => t.addEventListener("click", tagClicked)); diff --git a/fietsboek/templates/edit.jinja2 b/fietsboek/templates/edit.jinja2 index 33d18f5..b0f9849 100644 --- a/fietsboek/templates/edit.jinja2 +++ b/fietsboek/templates/edit.jinja2 @@ -9,7 +9,7 @@