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.
This commit is contained in:
Justin Edmund 2022-02-01 05:07:52 -08:00
parent 7a50c4bce5
commit dd974fde2e
4 changed files with 29 additions and 1 deletions

View file

@ -39,6 +39,7 @@ const ExtraWeapons = (props: Props) => {
unitType={1} unitType={1}
gridWeapon={props.grid[props.offset + i]} gridWeapon={props.grid[props.offset + i]}
onClick={() => { props.onClick(props.offset + i)}} onClick={() => { props.onClick(props.offset + i)}}
updateUncap={updateUncap}
/> />
</li> </li>
) )

View file

@ -1,6 +1,8 @@
import React, { useState } from 'react' import React, { useCallback, useMemo, useState } from 'react'
import UncapStar from '~components/UncapStar' import UncapStar from '~components/UncapStar'
import debounce from 'lodash.debounce'
import './index.scss' import './index.scss'
@ -16,6 +18,15 @@ interface Props {
const UncapIndicator = (props: Props) => { const UncapIndicator = (props: Props) => {
const [uncap, setUncap] = useState(props.uncapLevel) const [uncap, setUncap] = useState(props.uncapLevel)
const debouncedAction = debounce(() => {
console.log("Debouncing...")
props.updateUncap(numStars)
}, 1000)
const delayedAction = useCallback(() => {
debouncedAction()
}, [])
const numStars = setNumStars() const numStars = setNumStars()
function setNumStars() { function setNumStars() {
@ -47,6 +58,8 @@ const UncapIndicator = (props: Props) => {
setUncap(index + 1) setUncap(index + 1)
else else
setUncap(index) setUncap(index)
delayedAction()
} }
return ( return (

View file

@ -42,6 +42,7 @@ const WeaponGrid = (props: Props) => {
exists={false} exists={false}
offset={numWeapons} offset={numWeapons}
onClick={openSearchModal} onClick={openSearchModal}
updateUncap={updateUncap}
/> />
) )
@ -64,6 +65,10 @@ const WeaponGrid = (props: Props) => {
openModal() openModal()
} }
function updateUncap(id: string, uncap: number) {
console.log(`${id} is now ${uncap} stars`)
}
return ( return (
<div id="weapon_grids"> <div id="weapon_grids">
<div id="WeaponGrid"> <div id="WeaponGrid">
@ -74,6 +79,7 @@ const WeaponGrid = (props: Props) => {
unitType={0} unitType={0}
gridWeapon={props.mainhand} gridWeapon={props.mainhand}
onClick={() => { openSearchModal(-1) }} onClick={() => { openSearchModal(-1) }}
updateUncap={updateUncap}
/> />
<ul id="grid_weapons"> <ul id="grid_weapons">
@ -87,6 +93,7 @@ const WeaponGrid = (props: Props) => {
unitType={1} unitType={1}
gridWeapon={props.grid[i]} gridWeapon={props.grid[i]}
onClick={() => { openSearchModal(i) }} onClick={() => { openSearchModal(i) }}
updateUncap={updateUncap}
/> />
</li> </li>
) )

View file

@ -10,6 +10,7 @@ import './index.scss'
interface Props { interface Props {
onClick: () => void onClick: () => void
updateUncap: (id: string, uncap: number) => void
gridWeapon: GridWeapon | undefined gridWeapon: GridWeapon | undefined
position: number position: number
editable: boolean editable: boolean
@ -47,6 +48,11 @@ const WeaponUnit = (props: Props) => {
setImageUrl(imgSrc) setImageUrl(imgSrc)
} }
function passUncapData(uncap: number) {
if (props.gridWeapon)
props.updateUncap(props.gridWeapon.id, uncap)
}
return ( return (
<div> <div>
<div className={classes}> <div className={classes}>
@ -60,6 +66,7 @@ const WeaponUnit = (props: Props) => {
ulb={weapon?.uncap.ulb || false} ulb={weapon?.uncap.ulb || false}
flb={weapon?.uncap.flb || false} flb={weapon?.uncap.flb || false}
uncapLevel={props.gridWeapon?.uncap_level!} uncapLevel={props.gridWeapon?.uncap_level!}
updateUncap={passUncapData}
/> />
</div> </div>
</div> </div>