From 15f1e50225f28d066bfaf9f53682cb6a82f5ed61 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Sun, 27 Jun 2021 19:14:41 -0400 Subject: [PATCH] implement usethrottle --- src/components/NewUI/Event.tsx | 14 ++++++++--- src/components/NewUI/useThrottle.ts | 38 +++++++++++++++++++++++++++++ src/components/NewUI/useToggle.ts | 7 ++++++ 3 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 src/components/NewUI/useThrottle.ts create mode 100644 src/components/NewUI/useToggle.ts diff --git a/src/components/NewUI/Event.tsx b/src/components/NewUI/Event.tsx index 023f4dc..bd39638 100644 --- a/src/components/NewUI/Event.tsx +++ b/src/components/NewUI/Event.tsx @@ -1,8 +1,9 @@ -import { useState } from 'react'; +import { useCallback, useState } from 'react'; import UIButton from './UIButton'; import UIPlacesAutocomplete from './UIPlacesAutocomplete'; import UISecondaryBox from './UISecondaryBox'; import UISecondaryHeader from './UISecondaryHeader'; +import useThrottle from './useThrottle'; const green = '#60f760'; const lightgrey = '#e0e0e0'; @@ -201,6 +202,7 @@ const dummyPeopleData: IPerson[] = [ ]; function People({ event }: { event: IEvent }) { const PADDING = '1rem'; + // eslint-disable-next-line const [people, setPeople] = useState(dummyPeopleData); return (
@@ -242,7 +244,13 @@ export default function Event({ event }: { event: IEvent }) { const { name, group, formattedAddress, startTime, endTime } = event; const [haveRide, setHaveRide] = useState(false); const [locationPlaceId, setLocationPlaceId] = useState(null!); - const [interested, setInterested] = useState(false); + const [interested, toggleInterested] = useState(false); + const toggleInterestedThrottled = useThrottle( + useCallback(() => { + toggleInterested((i) => !i); + }, []), + 500 + ); return ( @@ -250,7 +258,7 @@ export default function Event({ event }: { event: IEvent }) {
setInterested((i) => !i)} + onClick={toggleInterestedThrottled} style={{ backgroundColor: interested ? green : lightgrey, color: interested ? 'white' : 'black', diff --git a/src/components/NewUI/useThrottle.ts b/src/components/NewUI/useThrottle.ts new file mode 100644 index 0000000..c399f74 --- /dev/null +++ b/src/components/NewUI/useThrottle.ts @@ -0,0 +1,38 @@ +import { useMemo } from 'react'; + +function throttle any, T>( + this: T, + callback: F, + limit: number, + thisArg?: T +): F { + let waiting = false; + let pendingArgs: null | any[] = null; + const scope = thisArg ?? this; + // @ts-ignore + const fn = function (this: T, ...args: any[]) { + if (!waiting) { + callback.apply(this, args); + waiting = true; + setTimeout(() => { + if (pendingArgs !== null) { + callback.apply(this, pendingArgs); + pendingArgs = null; + } + waiting = false; + }, limit); + } else { + pendingArgs = args; + } + }; + fn.bind(scope); + // @ts-ignore + return fn; +} + +export default function useThrottle any>( + fn: F, + limit: number +) { + return useMemo(() => throttle(fn, limit), [fn, limit]); +} diff --git a/src/components/NewUI/useToggle.ts b/src/components/NewUI/useToggle.ts new file mode 100644 index 0000000..5189579 --- /dev/null +++ b/src/components/NewUI/useToggle.ts @@ -0,0 +1,7 @@ +import { useCallback, useState } from 'react'; + +export default function useToggle(initial: boolean) { + const [value, setValue] = useState(initial); + const toggle = useCallback(() => setValue((v) => !v), []); + return [value, toggle]; +}