Move debounce to WeaponGrid
This commit is contained in:
parent
ca42f4b718
commit
39549f0b94
2 changed files with 49 additions and 37 deletions
|
|
@ -1,11 +1,8 @@
|
||||||
import React, { useCallback, useMemo, useState } from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
import UncapStar from '~components/UncapStar'
|
import UncapStar from '~components/UncapStar'
|
||||||
|
|
||||||
import debounce from 'lodash.debounce'
|
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
type: 'character' | 'weapon' | 'summon'
|
type: 'character' | 'weapon' | 'summon'
|
||||||
rarity?: number
|
rarity?: number
|
||||||
|
|
@ -16,19 +13,19 @@ interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
const UncapIndicator = (props: Props) => {
|
const UncapIndicator = (props: Props) => {
|
||||||
|
const firstUpdate = useRef(true)
|
||||||
const [uncap, setUncap] = useState(props.uncapLevel)
|
const [uncap, setUncap] = useState(props.uncapLevel)
|
||||||
|
|
||||||
const debouncedAction = debounce(() => {
|
useEffect(() => {
|
||||||
console.log("Debouncing...")
|
if (firstUpdate.current) {
|
||||||
props.updateUncap(numStars)
|
firstUpdate.current = false
|
||||||
}, 1000)
|
return
|
||||||
|
}
|
||||||
const delayedAction = useCallback(() => {
|
|
||||||
debouncedAction()
|
props.updateUncap(uncap)
|
||||||
}, [])
|
}, [uncap])
|
||||||
|
|
||||||
const numStars = setNumStars()
|
const numStars = setNumStars()
|
||||||
|
|
||||||
function setNumStars() {
|
function setNumStars() {
|
||||||
let numStars
|
let numStars
|
||||||
|
|
||||||
|
|
@ -52,14 +49,8 @@ const UncapIndicator = (props: Props) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleStar(index: number, empty: boolean) {
|
function toggleStar(index: number, empty: boolean) {
|
||||||
console.log("Toggling star!")
|
if (empty) setUncap(index + 1)
|
||||||
|
else setUncap(index)
|
||||||
if (empty)
|
|
||||||
setUncap(index + 1)
|
|
||||||
else
|
|
||||||
setUncap(index)
|
|
||||||
|
|
||||||
delayedAction()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,15 @@
|
||||||
import React, { useState } from 'react'
|
import React, { useCallback, useMemo, useState } from 'react'
|
||||||
import { useModal as useModal } from '~utils/useModal'
|
import { useModal as useModal } from '~utils/useModal'
|
||||||
|
|
||||||
|
import debounce from 'lodash.debounce'
|
||||||
|
|
||||||
import SearchModal from '~components/SearchModal'
|
import SearchModal from '~components/SearchModal'
|
||||||
import WeaponUnit from '~components/WeaponUnit'
|
import WeaponUnit from '~components/WeaponUnit'
|
||||||
import ExtraWeapons from '~components/ExtraWeapons'
|
import ExtraWeapons from '~components/ExtraWeapons'
|
||||||
|
|
||||||
import './index.scss'
|
import './index.scss'
|
||||||
|
import { delay } from 'lodash'
|
||||||
|
import api from '~utils/api'
|
||||||
|
|
||||||
// GridType
|
// GridType
|
||||||
export enum GridType {
|
export enum GridType {
|
||||||
|
|
@ -35,17 +39,6 @@ const WeaponGrid = (props: Props) => {
|
||||||
const numWeapons: number = 9
|
const numWeapons: number = 9
|
||||||
const searchGrid: GridArray<Weapon> = Object.values(props.grid).map((o) => o.weapon)
|
const searchGrid: GridArray<Weapon> = Object.values(props.grid).map((o) => o.weapon)
|
||||||
|
|
||||||
const extraGrid = (
|
|
||||||
<ExtraWeapons
|
|
||||||
grid={props.grid}
|
|
||||||
editable={props.editable}
|
|
||||||
exists={false}
|
|
||||||
offset={numWeapons}
|
|
||||||
onClick={openSearchModal}
|
|
||||||
updateUncap={updateUncap}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
|
|
||||||
function receiveWeapon(weapon: Weapon, position: number) {
|
function receiveWeapon(weapon: Weapon, position: number) {
|
||||||
props.onSelect(GridType.Weapon, weapon, position)
|
props.onSelect(GridType.Weapon, weapon, position)
|
||||||
}
|
}
|
||||||
|
|
@ -65,10 +58,38 @@ const WeaponGrid = (props: Props) => {
|
||||||
openModal()
|
openModal()
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateUncap(id: string, uncap: number) {
|
async function updateUncap(id: string, level: number) {
|
||||||
console.log(`${id} is now ${uncap} stars`)
|
console.log(id, level)
|
||||||
|
await api.updateUncap('weapon', id, level)
|
||||||
|
.then(response => {
|
||||||
|
console.log(response.data)
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.log(error)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const initiateUncapUpdate = (id: string, uncapLevel: number) => {
|
||||||
|
debouncedAction(id, uncapLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
const debouncedAction = useCallback(
|
||||||
|
() => debounce((id, number) => {
|
||||||
|
updateUncap(id, number)
|
||||||
|
}, 1000), []
|
||||||
|
)()
|
||||||
|
|
||||||
|
const extraGrid = (
|
||||||
|
<ExtraWeapons
|
||||||
|
grid={props.grid}
|
||||||
|
editable={props.editable}
|
||||||
|
exists={false}
|
||||||
|
offset={numWeapons}
|
||||||
|
onClick={openSearchModal}
|
||||||
|
updateUncap={initiateUncapUpdate}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div id="weapon_grids">
|
<div id="weapon_grids">
|
||||||
<div id="WeaponGrid">
|
<div id="WeaponGrid">
|
||||||
|
|
@ -79,7 +100,7 @@ const WeaponGrid = (props: Props) => {
|
||||||
unitType={0}
|
unitType={0}
|
||||||
gridWeapon={props.mainhand}
|
gridWeapon={props.mainhand}
|
||||||
onClick={() => { openSearchModal(-1) }}
|
onClick={() => { openSearchModal(-1) }}
|
||||||
updateUncap={updateUncap}
|
updateUncap={initiateUncapUpdate}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ul id="grid_weapons">
|
<ul id="grid_weapons">
|
||||||
|
|
@ -93,7 +114,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}
|
updateUncap={initiateUncapUpdate}
|
||||||
/>
|
/>
|
||||||
</li>
|
</li>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue