Add search functionality to MyChores component
This commit is contained in:
parent
09c25cea0e
commit
ae963286ec
1 changed files with 51 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { Add, EditCalendar } from '@mui/icons-material'
|
import { Add, CancelRounded, EditCalendar } from '@mui/icons-material'
|
||||||
import {
|
import {
|
||||||
Badge,
|
Badge,
|
||||||
Box,
|
Box,
|
||||||
|
@ -6,6 +6,7 @@ import {
|
||||||
CircularProgress,
|
CircularProgress,
|
||||||
Container,
|
Container,
|
||||||
IconButton,
|
IconButton,
|
||||||
|
Input,
|
||||||
List,
|
List,
|
||||||
ListItem,
|
ListItem,
|
||||||
Menu,
|
Menu,
|
||||||
|
@ -13,6 +14,7 @@ import {
|
||||||
Snackbar,
|
Snackbar,
|
||||||
Typography,
|
Typography,
|
||||||
} from '@mui/joy'
|
} from '@mui/joy'
|
||||||
|
import Fuse from 'fuse.js'
|
||||||
import { useContext, useEffect, useRef, useState } from 'react'
|
import { useContext, useEffect, useRef, useState } from 'react'
|
||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
import { UserContext } from '../../contexts/UserContext'
|
import { UserContext } from '../../contexts/UserContext'
|
||||||
|
@ -27,6 +29,7 @@ const MyChores = () => {
|
||||||
const [chores, setChores] = useState([])
|
const [chores, setChores] = useState([])
|
||||||
const [filteredChores, setFilteredChores] = useState([])
|
const [filteredChores, setFilteredChores] = useState([])
|
||||||
const [selectedFilter, setSelectedFilter] = useState('All')
|
const [selectedFilter, setSelectedFilter] = useState('All')
|
||||||
|
const [searchTerm, setSearchTerm] = useState('')
|
||||||
const [activeUserId, setActiveUserId] = useState(0)
|
const [activeUserId, setActiveUserId] = useState(0)
|
||||||
const [performers, setPerformers] = useState([])
|
const [performers, setPerformers] = useState([])
|
||||||
const [anchorEl, setAnchorEl] = useState(null)
|
const [anchorEl, setAnchorEl] = useState(null)
|
||||||
|
@ -158,6 +161,28 @@ const MyChores = () => {
|
||||||
setFilteredChores(newFilteredChores)
|
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) {
|
if (userProfile === null) {
|
||||||
return (
|
return (
|
||||||
<Container className='flex h-full items-center justify-center'>
|
<Container className='flex h-full items-center justify-center'>
|
||||||
|
@ -258,6 +283,31 @@ const MyChores = () => {
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
</Menu>
|
</Menu>
|
||||||
</List>
|
</List>
|
||||||
|
{/* Search box to filter */}
|
||||||
|
<Box>
|
||||||
|
<Input
|
||||||
|
placeholder='Search'
|
||||||
|
value={searchTerm}
|
||||||
|
sx={{
|
||||||
|
mt: 1,
|
||||||
|
mb: 1,
|
||||||
|
borderRadius: 20,
|
||||||
|
// border: '1px solid',
|
||||||
|
borderColor: 'text.disabled',
|
||||||
|
padding: 1,
|
||||||
|
}}
|
||||||
|
onChange={handleSearchChange}
|
||||||
|
endDecorator={
|
||||||
|
<CancelRounded
|
||||||
|
onClick={() => {
|
||||||
|
setSearchTerm('')
|
||||||
|
setFilteredChores(chores)
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
|
||||||
{/* </Sheet> */}
|
{/* </Sheet> */}
|
||||||
{filteredChores.length === 0 && (
|
{filteredChores.length === 0 && (
|
||||||
<Box
|
<Box
|
||||||
|
|
Loading…
Add table
Reference in a new issue