From 4ca35b938e6ccd07e47b04869fbe30e9109bcbe4 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Mon, 19 Jun 2023 19:48:16 +0200 Subject: hide friends that are already tagged --- asset-sources/fietsboek.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'asset-sources') diff --git a/asset-sources/fietsboek.ts b/asset-sources/fietsboek.ts index 1cadfc2..b885271 100644 --- a/asset-sources/fietsboek.ts +++ b/asset-sources/fietsboek.ts @@ -137,6 +137,23 @@ function checkNameValidity(name: Selector) { // This function is used via a HTML onchange= handler, so make eslint happy checkNameValidity; +/** + * Check whether the given friend is already tagged. + * + * Note that this uses the "HTML list", so it uses the tags that the user is + * currently editing - not the ones from the database! + * + * @param friendId - ID of the friend to check. + * @return Whether the friend is in the list of tagged people. + */ +function friendIsTagged(friendId: number): boolean { + return Array.from(document.querySelectorAll("[name='tagged-friend[]']")) + .map((obj) => obj as HTMLInputElement) + .filter((obj) => !obj.disabled) + .map((obj) => obj.value) + .includes(friendId.toString()); +} + /** * Hit the endpoint to search for friends. This populates the friend selector * when tagging friends. @@ -152,10 +169,15 @@ function searchFriends() { const blueprint = document.querySelector("#friendSearchBlueprint") as HTMLLIElement; // Only show friends with a matching name - const friends = response.filter( + let friends = response.filter( (obj) => obj.name.toLowerCase().indexOf(searchPattern) != -1 ); + // Only show friends that are not yet existing + friends = friends.filter( + (obj) => !friendIsTagged(obj.id) + ); + friends.forEach((friend) => { const copy = blueprint.cloneNode(true) as HTMLLIElement; copy.removeAttribute("id"); -- cgit v1.2.3