From 5c6c89384f5d69dc1f2d4a74102119b58a3c0164 Mon Sep 17 00:00:00 2001
From: Marte Fossum <myf@uio.no>
Date: Wed, 25 Jan 2023 12:55:13 +0100
Subject: [PATCH] GREG-327: Add search field for active guests

---
 frontend/public/locales/en/common.json        |  2 +
 frontend/public/locales/nb/common.json        |  2 +
 frontend/public/locales/nn/common.json        |  2 +
 .../src/routes/sponsor/frontpage/index.tsx    | 58 +++++++++++++++++--
 4 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/frontend/public/locales/en/common.json b/frontend/public/locales/en/common.json
index 2575a064..26411b65 100644
--- a/frontend/public/locales/en/common.json
+++ b/frontend/public/locales/en/common.json
@@ -104,7 +104,9 @@
   "activeGuests": "Confirmed guests",
   "activeGuestsDescription": "Make changes to guest roles.",
   "noActiveGuests": "No active guests",
+  "foundNoGuests": "Found no guests",
   "sentInvitations": "Sent invitations",
+  "placeholder": "Search for guest",
   "sentInvitationsDescription": "Invitations awaiting response from guest.",
   "noInvitations": "No invitations",
   "status": "Status",
diff --git a/frontend/public/locales/nb/common.json b/frontend/public/locales/nb/common.json
index d17983e0..6780ea9e 100644
--- a/frontend/public/locales/nb/common.json
+++ b/frontend/public/locales/nb/common.json
@@ -104,7 +104,9 @@
   "activeGuests": "Godkjente gjester",
   "activeGuestsDescription": "Her kan du endre på gjesteroller",
   "noActiveGuests": "Ingen aktive gjester",
+  "foundNoGuests": "Fant ingen gjester",
   "sentInvitations": "Sendte invitasjoner",
+  "placeholder": "Søk etter gjest",
   "sentInvitationsDescription": "Invitasjoner som venter på at gjesten skal ferdigstille registreringen.",
   "noInvitations": "Ingen invitasjoner",
   "status": "Status",
diff --git a/frontend/public/locales/nn/common.json b/frontend/public/locales/nn/common.json
index 4cafdbd0..c8751c92 100644
--- a/frontend/public/locales/nn/common.json
+++ b/frontend/public/locales/nn/common.json
@@ -104,7 +104,9 @@
   "activeGuests": "Godkjende gjester",
   "activeGuestsDescription": "Endre på gjesteroller.",
   "noActiveGuests": "Ingen aktive gjester",
+  "foundNoGuests": "Fann ingen gjester",
   "sentInvitations": "Sendte invitasjonar",
+  "placeholder": "Søk etter gjest",
   "sentInvitationsDescription": "Invitasjonar som venter på at gjesten skal ferdigstille registreringa.",
   "noInvitations": "Ingen invitasjonar",
   "status": "Status",
diff --git a/frontend/src/routes/sponsor/frontpage/index.tsx b/frontend/src/routes/sponsor/frontpage/index.tsx
index 84bf384b..c509c658 100644
--- a/frontend/src/routes/sponsor/frontpage/index.tsx
+++ b/frontend/src/routes/sponsor/frontpage/index.tsx
@@ -5,6 +5,7 @@ import {
   AccordionDetails,
   AccordionSummary,
   Button,
+  InputAdornment,
   Paper,
   Table,
   TableBody,
@@ -12,10 +13,12 @@ import {
   TableContainer,
   TableHead,
   TableRow,
+  TextField,
   Typography,
 } from '@mui/material'
-import { styled } from '@mui/system'
+import { Box, styled } from '@mui/system'
 import { useState } from 'react'
+import SearchIcon from '@mui/icons-material/Search'
 
 import Loading from 'components/loading'
 import Page from 'components/page'
@@ -316,6 +319,8 @@ const InvitedGuests = ({ persons }: GuestProps) => {
 
 const ActiveGuests = ({ persons }: GuestProps) => {
   const [activeExpanded, setActiveExpanded] = useState(false)
+  const [searchHasInput, setSearchHasInput] = useState(false)
+  const [searchGuests, setSearchGuests] = useState<Guest[]>([])
 
   // Show all verified guests
   let guests = persons.length > 0 ? persons : []
@@ -323,6 +328,21 @@ const ActiveGuests = ({ persons }: GuestProps) => {
     guests = guests.filter((person) => person.verified)
   }
   const [t] = useTranslation(['common'])
+
+  const getSponsorGuests = (event: React.ChangeEvent<HTMLInputElement>) => {
+    if (event.target.value) {
+      setSearchHasInput(true)
+      const guestSearch: Guest[] = guests.filter((guest) =>
+        `${guest.first.toLowerCase()} ${guest.last.toLowerCase()}`.includes(
+          event.target.value.toLowerCase()
+        )
+      )
+      setSearchGuests(guestSearch)
+    } else {
+      setSearchHasInput(false)
+      setSearchGuests([])
+    }
+  }
   return (
     <StyledAccordion
       expanded={activeExpanded}
@@ -344,11 +364,37 @@ const ActiveGuests = ({ persons }: GuestProps) => {
         >
           {t('common:activeGuestsDescription')}
         </Typography>
-        <GuestTable
-          guests={guests}
-          emptyText={t('common:noActiveGuests')}
-          marginWidth={1000}
-        />
+        <Box
+          sx={{
+            marginBottom: '1rem',
+          }}
+        >
+          <TextField
+            variant="standard"
+            InputProps={{
+              endAdornment: (
+                <InputAdornment position="end">
+                  <SearchIcon />
+                </InputAdornment>
+              ),
+            }}
+            placeholder={t('placeholder')}
+            onChange={getSponsorGuests}
+          />
+          {!searchHasInput ? (
+            <GuestTable
+              guests={guests}
+              emptyText={t('common:noActiveGuests')}
+              marginWidth={1000}
+            />
+          ) : (
+            <GuestTable
+              guests={searchGuests}
+              emptyText={t('common:foundNoGuests')}
+              marginWidth={1000}
+            />
+          )}
+        </Box>
       </AccordionDetails>
     </StyledAccordion>
   )
-- 
GitLab