fix(#168): fix refetch status download

VID-168-donwload-video
andreidekterev 2 years ago
parent 9271894154
commit d1eb9868e9
  1. 8
      src/features/MatchPage/components/MatchDownloadPopup/index.tsx
  2. 45
      src/features/MatchSidePlaylists/components/MatchDownloadButton/index.tsx
  3. 2
      src/features/MatchSidePlaylists/components/MatchPlaylists/index.tsx

@ -31,6 +31,7 @@ type Props = {
closePopup: () => void, closePopup: () => void,
isModalOpen: boolean, isModalOpen: boolean,
openDownloadNotification: () => void, openDownloadNotification: () => void,
setPlaylistConfig: (config: DownloadPlaylistProps) => void,
} }
const fileType: Record<'download_single_file'| 'download_files_for_periods', string> = { const fileType: Record<'download_single_file'| 'download_files_for_periods', string> = {
@ -47,6 +48,7 @@ export const MatchDownloadPopup = ({
closePopup, closePopup,
isModalOpen, isModalOpen,
openDownloadNotification, openDownloadNotification,
setPlaylistConfig,
}: Props) => { }: Props) => {
const [downloadConfig, setDownloadConfig] = useState<typeof initialRadioBtns>(initialRadioBtns) const [downloadConfig, setDownloadConfig] = useState<typeof initialRadioBtns>(initialRadioBtns)
@ -59,10 +61,14 @@ export const MatchDownloadPopup = ({
} }
const { refetch } = useQuery( const { refetch } = useQuery(
querieKeys.downloadPlaylist, querieKeys.downloadPlaylist,
() => downloadPlaylist(config), () => {
setPlaylistConfig(config)
downloadPlaylist(config)
},
{ {
enabled: false, // disable this query from automatically running enabled: false, // disable this query from automatically running
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
staleTime: 0,
}, },
) )

@ -11,25 +11,30 @@ import { MatchDownloadPopup } from 'features/MatchPage/components/MatchDownloadP
import { usePageParams } from 'hooks/usePageParams' import { usePageParams } from 'hooks/usePageParams'
import { useQueryClient } from 'react-query' import { useQuery } from 'react-query'
import { DownloadResponse } from 'requests/downloadPlaylist' import { downloadPlaylist, type DownloadPlaylistProps } from 'requests/downloadPlaylist'
import { querieKeys } from 'config' import { querieKeys } from 'config'
import { useInterval } from 'hooks' import { useInterval } from 'hooks'
import { useEffect } from 'react' import { useEffect, useState } from 'react'
import { downloadFile } from 'helpers' import { downloadFile } from 'helpers'
import { DownloadButton, Title } from '../../styled' import { DownloadButton, Title } from '../../styled'
import { Item } from '../MatchPlaylists' import { Item } from '../MatchPlaylists'
type TMatchDownloadButton = { type TMatchDownloadButton = {
close: () => void,
open: () => void, open: () => void,
} }
const INTERVAL_CHECK_DOWNLOAD_STATUS = 60 * 1000 const INTERVAL_CHECK_DOWNLOAD_STATUS = 60 * 1000
export const MatchDownloadButton = ({ open: openDownloadNotification }:TMatchDownloadButton) => { export const MatchDownloadButton = ({
close: closeDownloadNotification,
open: openDownloadNotification,
}:TMatchDownloadButton) => {
const { user, userInfo } = useAuthStore() const { user, userInfo } = useAuthStore()
const { profile } = useMatchPageStore() const { profile } = useMatchPageStore()
const { sportType } = usePageParams() const { sportType } = usePageParams()
const [playlistConfig, setPlaylistConfig] = useState<DownloadPlaylistProps | null>(null)
const { const {
close, close,
@ -59,25 +64,28 @@ export const MatchDownloadButton = ({ open: openDownloadNotification }:TMatchDow
return null return null
} }
const client = useQueryClient() const { data, refetch } = useQuery(
querieKeys.downloadPlaylist,
const data = client.getQueryData<DownloadResponse>(querieKeys.downloadPlaylist) () => playlistConfig && downloadPlaylist(playlistConfig),
{
const handleRefetch = () => { enabled: false,
client.refetchQueries(querieKeys.downloadPlaylist) refetchOnWindowFocus: false,
} staleTime: 0,
},
)
const { start, stop } = useInterval({ const { start, stop } = useInterval({
callback: handleRefetch, callback: refetch,
intervalDuration: INTERVAL_CHECK_DOWNLOAD_STATUS, intervalDuration: INTERVAL_CHECK_DOWNLOAD_STATUS,
startImmediate: false, startImmediate: false,
}) })
useEffect(() => { useEffect(() => {
start() if (!data) return undefined
if (data?.status === 'COMPLETED' && data.urls) { if (data?.status === 'COMPLETED' && data.urls) {
downloadFile(data.urls) downloadFile(data.urls)
closeDownloadNotification()
stop() stop()
} else if (data?.status === 'ERROR') { } else if (data?.status === 'ERROR') {
stop() stop()
@ -85,7 +93,7 @@ export const MatchDownloadButton = ({ open: openDownloadNotification }:TMatchDow
return () => stop() return () => stop()
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [data?.status]) }, [data?.status, refetch])
const isNeedDownloadButton = user && (isAvailableTournaments() || isAvailableTeams()) const isNeedDownloadButton = user && (isAvailableTournaments() || isAvailableTeams())
@ -97,8 +105,15 @@ export const MatchDownloadButton = ({ open: openDownloadNotification }:TMatchDow
isModalOpen={isOpen} isModalOpen={isOpen}
closePopup={close} closePopup={close}
openDownloadNotification={openDownloadNotification} openDownloadNotification={openDownloadNotification}
setPlaylistConfig={setPlaylistConfig}
/> />
<DownloadButton onClick={open}> <DownloadButton onClick={
() => {
open()
start()
}
}
>
<Title> <Title>
<T9n t='download_full_match' /> <T9n t='download_full_match' />
</Title> </Title>

@ -126,7 +126,7 @@ export const MatchPlaylists = forwardRef(
</PlayButton> </PlayButton>
</Item> </Item>
))} ))}
<MatchDownloadButton open={open} /> <MatchDownloadButton open={open} close={close} />
</List> </List>
) )
}, },

Loading…
Cancel
Save