Skip to content

Commit

Permalink
Merge pull request #130 from EnigmaVSSUT/dev-v2
Browse files Browse the repository at this point in the history
feat: debouncing
  • Loading branch information
SySagar authored Feb 27, 2024
2 parents 361ef09 + bcb93dc commit 887ae6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/app/events/components/EventList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import EventCard from "./EventCard";
import useDatePicker from "../../../shared/store/useDatePicker";
import { useEffect, useState } from "react";
import { HighlightOff } from "@mui/icons-material";
import { useDebounce } from "../../../hooks/useDebounce";

const datalist1 = [
{
Expand Down Expand Up @@ -37,6 +38,7 @@ export default function EventList() {
const [result, setResult] = useState([]);
const [value, setValue] = useState("");
const pickedDate = useDatePicker((state) => state.pickedDate);
const debouncedQuery = useDebounce(value, 100);

useEffect(() => {
if (pickedDate !== "") {
Expand All @@ -49,10 +51,10 @@ export default function EventList() {
}
}, [pickedDate]);

const filterDataList = () => {
if (value.trim() === "" && pickedDate === "") {
const filterDataList = (debouncedQuery) => {
if (debouncedQuery === "" && pickedDate === "") {
setResult(totalData);
} else if (pickedDate !== "" && value === "") {
} else if (pickedDate !== "" && debouncedQuery === "") {
setResult([])
const filtered = totalData.filter(
(item) =>
Expand All @@ -69,6 +71,10 @@ export default function EventList() {
}
};

useEffect(() => {
filterDataList(debouncedQuery);
}, [debouncedQuery]);

return (
<Stack
gap={3}
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useDebounce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect, useState } from "react";

export const useDebounce = (value, delay) => {
const [debouncedValue, setDebouncedValue] = useState(value);

useEffect(() => {
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);

return () => {
clearTimeout(handler);
};
}, [value, delay]);

return debouncedValue;
};

0 comments on commit 887ae6b

Please sign in to comment.