From ae963286ec7fa1ce90adaca1b2afead01fa51eef Mon Sep 17 00:00:00 2001 From: Mo Tarbin Date: Sat, 6 Jul 2024 03:49:51 -0400 Subject: Add search functionality to MyChores component --- src/views/Chores/MyChores.jsx | 52 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'src/views') diff --git a/src/views/Chores/MyChores.jsx b/src/views/Chores/MyChores.jsx index 98fd443..da74918 100644 --- a/src/views/Chores/MyChores.jsx +++ b/src/views/Chores/MyChores.jsx @@ -1,4 +1,4 @@ -import { Add, EditCalendar } from '@mui/icons-material' +import { Add, CancelRounded, EditCalendar } from '@mui/icons-material' import { Badge, Box, @@ -6,6 +6,7 @@ import { CircularProgress, Container, IconButton, + Input, List, ListItem, Menu, @@ -13,6 +14,7 @@ import { Snackbar, Typography, } from '@mui/joy' +import Fuse from 'fuse.js' import { useContext, useEffect, useRef, useState } from 'react' import { useNavigate } from 'react-router-dom' import { UserContext } from '../../contexts/UserContext' @@ -27,6 +29,7 @@ const MyChores = () => { const [chores, setChores] = useState([]) const [filteredChores, setFilteredChores] = useState([]) const [selectedFilter, setSelectedFilter] = useState('All') + const [searchTerm, setSearchTerm] = useState('') const [activeUserId, setActiveUserId] = useState(0) const [performers, setPerformers] = useState([]) const [anchorEl, setAnchorEl] = useState(null) @@ -158,6 +161,28 @@ const MyChores = () => { setFilteredChores(newFilteredChores) } + const searchOptions = { + // keys to search in + keys: ['name', 'labels'], + includeScore: true, // Optional: if you want to see how well each result matched the search term + isCaseSensitive: false, + findAllMatches: true, + } + const fuse = new Fuse(chores, searchOptions) + + const handleSearchChange = e => { + const search = e.target.value + if (search === '') { + setFilteredChores(chores) + setSearchTerm('') + return + } + + const term = search.toLowerCase() + setSearchTerm(term) + setFilteredChores(fuse.search(term).map(result => result.item)) + } + if (userProfile === null) { return ( @@ -258,6 +283,31 @@ const MyChores = () => { + {/* Search box to filter */} + + { + setSearchTerm('') + setFilteredChores(chores) + }} + /> + } + /> + + {/* */} {filteredChores.length === 0 && (