From f9f335b58b1d350b90bbb73ea261882309eb2fa0 Mon Sep 17 00:00:00 2001 From: JohanGrims Date: Fri, 18 Oct 2024 14:04:53 +0200 Subject: [PATCH] Refactor search functionality in Answers component --- src/admin/vote/Answers.jsx | 23 +++++++++++++++++++++-- src/admin/vote/Match.tsx | 30 ++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/admin/vote/Answers.jsx b/src/admin/vote/Answers.jsx index b5d090c..edaba53 100644 --- a/src/admin/vote/Answers.jsx +++ b/src/admin/vote/Answers.jsx @@ -13,10 +13,29 @@ import { db } from "../../firebase"; export default function Answers() { const { vote, options, answers } = useLoaderData(); - const [mode, setMode] = React.useState("by-option"); + const search = new URLSearchParams(window.location.search).get("search"); + + const [mode, setMode] = React.useState(search ? "by-name" : "by-option"); const grades = [...new Set(answers.map((answer) => answer.grade))]; + React.useEffect(() => { + if (search) { + const query = search.toLowerCase(); + const elements = document.querySelectorAll("tbody tr"); + elements.forEach((element) => { + if (element.textContent.toLowerCase().includes(query)) { + element.style.display = ""; + } else { + element.style.display = "none"; + } + }); + // write search query to search field + const searchField = document.querySelector("mdui-text-field"); + searchField.value = search; + } + }, []); + return (

Antworten

@@ -192,7 +211,7 @@ export default function Answers() { label="Suchen" onInput={(e) => { const query = e.target.value.toLowerCase(); - const elements = document.querySelectorAll("tr"); + const elements = document.querySelectorAll("tbody tr"); elements.forEach((element) => { if (element.textContent.toLowerCase().includes(query)) { element.style.display = ""; diff --git a/src/admin/vote/Match.tsx b/src/admin/vote/Match.tsx index f26076a..5acfad7 100644 --- a/src/admin/vote/Match.tsx +++ b/src/admin/vote/Match.tsx @@ -1,6 +1,6 @@ import { collection, getDocs } from "firebase/firestore/lite"; import React from "react"; -import { useLoaderData } from "react-router-dom"; +import { Link, useLoaderData } from "react-router-dom"; import { db } from "../../firebase"; import { Class } from "../../types"; @@ -40,26 +40,36 @@ export default function Match() { {s.name} {s.listIndex} - { - choices.filter( - (choice) => - choice.listIndex === s.listIndex.toString() && - choice.grade === c.grade.toString() - )[0]?.name - } + + choice.listIndex === s.listIndex.toString() && + choice.grade === c.grade.toString() + )[0]?.id + }`} + > + { + choices.filter( + (choice) => + choice.listIndex === s.listIndex.toString() && + choice.grade === c.grade.toString() + )[0]?.name + } + {choices.filter( (choice) => choice.listIndex === s.listIndex.toString() && choice.grade === c.grade.toString() ).length > 1 && - "+ " + + " + " + (choices.filter( (choice) => choice.listIndex === s.listIndex.toString() && choice.grade === c.grade.toString() ).length - 1) + - " weitere"} + " weitere"}{" "} ))}