From dd974fde2e9aa84f42c44aa446eb4136daec8bd5 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Tue, 1 Feb 2022 05:07:52 -0800 Subject: [PATCH] WIP: Use debounce to intelligently send uncap level to server We are using debounce to send the uncap level to the server without making a ton of requests if the user is feeling clicky. This is a WIP because it doesn't send to the server yet. I'm having issues setting the correct initial state from the props. --- components/ExtraWeapons/index.tsx | 1 + components/UncapIndicator/index.tsx | 15 ++++++++++++++- components/WeaponGrid/index.tsx | 7 +++++++ components/WeaponUnit/index.tsx | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/components/ExtraWeapons/index.tsx b/components/ExtraWeapons/index.tsx index 65792741..de72c162 100644 --- a/components/ExtraWeapons/index.tsx +++ b/components/ExtraWeapons/index.tsx @@ -39,6 +39,7 @@ const ExtraWeapons = (props: Props) => { unitType={1} gridWeapon={props.grid[props.offset + i]} onClick={() => { props.onClick(props.offset + i)}} + updateUncap={updateUncap} /> ) diff --git a/components/UncapIndicator/index.tsx b/components/UncapIndicator/index.tsx index d19fbb40..a0971315 100644 --- a/components/UncapIndicator/index.tsx +++ b/components/UncapIndicator/index.tsx @@ -1,6 +1,8 @@ -import React, { useState } from 'react' +import React, { useCallback, useMemo, useState } from 'react' import UncapStar from '~components/UncapStar' +import debounce from 'lodash.debounce' + import './index.scss' @@ -16,6 +18,15 @@ interface Props { const UncapIndicator = (props: Props) => { const [uncap, setUncap] = useState(props.uncapLevel) + const debouncedAction = debounce(() => { + console.log("Debouncing...") + props.updateUncap(numStars) + }, 1000) + + const delayedAction = useCallback(() => { + debouncedAction() + }, []) + const numStars = setNumStars() function setNumStars() { @@ -47,6 +58,8 @@ const UncapIndicator = (props: Props) => { setUncap(index + 1) else setUncap(index) + + delayedAction() } return ( diff --git a/components/WeaponGrid/index.tsx b/components/WeaponGrid/index.tsx index cfd01ef8..7e1ea5d7 100644 --- a/components/WeaponGrid/index.tsx +++ b/components/WeaponGrid/index.tsx @@ -42,6 +42,7 @@ const WeaponGrid = (props: Props) => { exists={false} offset={numWeapons} onClick={openSearchModal} + updateUncap={updateUncap} /> ) @@ -64,6 +65,10 @@ const WeaponGrid = (props: Props) => { openModal() } + function updateUncap(id: string, uncap: number) { + console.log(`${id} is now ${uncap} stars`) + } + return (
@@ -74,6 +79,7 @@ const WeaponGrid = (props: Props) => { unitType={0} gridWeapon={props.mainhand} onClick={() => { openSearchModal(-1) }} + updateUncap={updateUncap} />
    @@ -87,6 +93,7 @@ const WeaponGrid = (props: Props) => { unitType={1} gridWeapon={props.grid[i]} onClick={() => { openSearchModal(i) }} + updateUncap={updateUncap} /> ) diff --git a/components/WeaponUnit/index.tsx b/components/WeaponUnit/index.tsx index a7f7b1d1..dc99d4c6 100644 --- a/components/WeaponUnit/index.tsx +++ b/components/WeaponUnit/index.tsx @@ -10,6 +10,7 @@ import './index.scss' interface Props { onClick: () => void + updateUncap: (id: string, uncap: number) => void gridWeapon: GridWeapon | undefined position: number editable: boolean @@ -47,6 +48,11 @@ const WeaponUnit = (props: Props) => { setImageUrl(imgSrc) } + function passUncapData(uncap: number) { + if (props.gridWeapon) + props.updateUncap(props.gridWeapon.id, uncap) + } + return (
    @@ -60,6 +66,7 @@ const WeaponUnit = (props: Props) => { ulb={weapon?.uncap.ulb || false} flb={weapon?.uncap.flb || false} uncapLevel={props.gridWeapon?.uncap_level!} + updateUncap={passUncapData} />