Ott 338 player progress report (#128)
* feat(#338): added useInterval hook * feat(#338): added progress reporter request * refactor(#338): renamed hook * feat(#338): added match player progress reporting * fix(#338): fixed bug when player paused while buffering * fix(#338): reset tournament on sport reset * fix(#338): review fix Co-authored-by: mirlan.maksitaliev <mirlan.maksitaliev@instatsport.com>keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
parent
988a0142a3
commit
a312a9b47a
@ -0,0 +1,37 @@ |
|||||||
|
import { useEffect, useState } from 'react' |
||||||
|
|
||||||
|
import type { MatchInfo } from 'requests' |
||||||
|
import { getMatchInfo } from 'requests' |
||||||
|
|
||||||
|
import { useLexicsStore } from 'features/LexicsStore' |
||||||
|
|
||||||
|
import { useSportNameParam, usePageId } from 'hooks' |
||||||
|
|
||||||
|
type Name = 'name_rus' | 'name_eng' |
||||||
|
|
||||||
|
export const useMatchProfileCard = () => { |
||||||
|
const [matchProfile, setMatchProfile] = useState<MatchInfo>(null) |
||||||
|
const { sportType } = useSportNameParam() |
||||||
|
const pageId = usePageId() |
||||||
|
const { suffix } = useLexicsStore() |
||||||
|
|
||||||
|
const matchProfileNames = { |
||||||
|
team1Name: matchProfile?.team1[`name_${suffix}` as Name], |
||||||
|
team2Name: matchProfile?.team2[`name_${suffix}` as Name], |
||||||
|
tournament: matchProfile?.tournament[`name_${suffix}` as Name], |
||||||
|
} |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
getMatchInfo(sportType, pageId) |
||||||
|
.then(setMatchProfile) |
||||||
|
}, |
||||||
|
[ |
||||||
|
sportType, |
||||||
|
pageId, |
||||||
|
]) |
||||||
|
|
||||||
|
return { |
||||||
|
matchProfile, |
||||||
|
matchProfileNames, |
||||||
|
} |
||||||
|
} |
||||||
@ -1,37 +1,43 @@ |
|||||||
import { useEffect, useState } from 'react' |
import { useCallback, useRef } from 'react' |
||||||
|
|
||||||
import type { MatchInfo } from 'requests' |
import { reportPlayerProgress } from 'requests' |
||||||
import { getMatchInfo } from 'requests' |
|
||||||
|
|
||||||
import { useLexicsStore } from 'features/LexicsStore' |
import { |
||||||
|
useSportNameParam, |
||||||
|
usePageId, |
||||||
|
useInterval, |
||||||
|
} from 'hooks' |
||||||
|
|
||||||
import { useSportNameParam, usePageId } from 'hooks' |
const reportRequestInterval = 30000 |
||||||
|
|
||||||
type Name = 'name_rus' | 'name_eng' |
export const usePlayerProgressReporter = () => { |
||||||
|
|
||||||
export const useMatchPage = () => { |
|
||||||
const [matchProfile, setMatchProfile] = useState<MatchInfo>(null) |
|
||||||
const { sportType } = useSportNameParam() |
const { sportType } = useSportNameParam() |
||||||
const pageId = usePageId() |
const matchId = usePageId() |
||||||
const { suffix } = useLexicsStore() |
const secondsRef = useRef(0) |
||||||
|
|
||||||
const matchProfileNames = { |
const intervalCallback = () => { |
||||||
team1Name: matchProfile?.team1[`name_${suffix}` as Name], |
reportPlayerProgress({ |
||||||
team2Name: matchProfile?.team2[`name_${suffix}` as Name], |
matchId, |
||||||
tournament: matchProfile?.tournament[`name_${suffix}` as Name], |
seconds: secondsRef.current, |
||||||
|
sport: sportType, |
||||||
|
}) |
||||||
} |
} |
||||||
|
const { start, stop } = useInterval({ |
||||||
useEffect(() => { |
callback: intervalCallback, |
||||||
getMatchInfo(sportType, pageId) |
intervalDuration: reportRequestInterval, |
||||||
.then(setMatchProfile) |
}) |
||||||
}, |
|
||||||
[ |
const onPlayingChange = useCallback((playing: boolean) => { |
||||||
sportType, |
if (playing) { |
||||||
pageId, |
start() |
||||||
]) |
} else { |
||||||
|
stop() |
||||||
return { |
|
||||||
matchProfile, |
|
||||||
matchProfileNames, |
|
||||||
} |
} |
||||||
|
}, [start, stop]) |
||||||
|
|
||||||
|
const onPlayerProgressChange = useCallback((seconds: number) => { |
||||||
|
secondsRef.current = seconds |
||||||
|
}, []) |
||||||
|
|
||||||
|
return { onPlayerProgressChange, onPlayingChange } |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,37 @@ |
|||||||
|
import { useEffect, useRef } from 'react' |
||||||
|
|
||||||
|
import noop from 'lodash/noop' |
||||||
|
|
||||||
|
import { useToggle } from 'hooks' |
||||||
|
|
||||||
|
type Args = { |
||||||
|
callback: () => void, |
||||||
|
intervalDuration: number, |
||||||
|
startImmediate?: boolean, |
||||||
|
} |
||||||
|
|
||||||
|
export const useInterval = ({ |
||||||
|
callback = noop, |
||||||
|
intervalDuration, |
||||||
|
startImmediate = true, |
||||||
|
}: Args) => { |
||||||
|
const { |
||||||
|
close: stop, |
||||||
|
isOpen: isRunning, |
||||||
|
open: start, |
||||||
|
} = useToggle(startImmediate) |
||||||
|
const savedCallback = useRef(callback) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
savedCallback.current = callback |
||||||
|
}, [callback]) |
||||||
|
|
||||||
|
useEffect(() => { |
||||||
|
if (!isRunning) return undefined |
||||||
|
|
||||||
|
const id = setInterval(savedCallback.current, intervalDuration) |
||||||
|
return () => clearInterval(id) |
||||||
|
}, [isRunning, intervalDuration]) |
||||||
|
|
||||||
|
return { start, stop } |
||||||
|
} |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
import { |
||||||
|
DATA_URL, |
||||||
|
PROCEDURES, |
||||||
|
SportTypes, |
||||||
|
} from 'config' |
||||||
|
import { callApi } from 'helpers' |
||||||
|
|
||||||
|
const proc = PROCEDURES.save_user_match_second |
||||||
|
|
||||||
|
type Args = { |
||||||
|
half?: number, |
||||||
|
matchId: number, |
||||||
|
seconds: number, |
||||||
|
sport: SportTypes, |
||||||
|
} |
||||||
|
|
||||||
|
export const reportPlayerProgress = ({ |
||||||
|
half, |
||||||
|
matchId, |
||||||
|
seconds, |
||||||
|
sport, |
||||||
|
}: Args) => { |
||||||
|
const config = { |
||||||
|
body: { |
||||||
|
params: { |
||||||
|
_p_half: half || null, |
||||||
|
_p_match_id: matchId, |
||||||
|
_p_second: seconds, |
||||||
|
_p_sport: sport, |
||||||
|
}, |
||||||
|
proc, |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
callApi({ |
||||||
|
config, |
||||||
|
url: DATA_URL, |
||||||
|
}) |
||||||
|
} |
||||||
Loading…
Reference in new issue