From 38b1d6209210ff3575e8544dffbbb1e802f70d72 Mon Sep 17 00:00:00 2001 From: Mirlan Date: Wed, 9 Dec 2020 14:47:52 +0600 Subject: [PATCH] fix(645): added sportTypes to matches request results in profile pages (#243) --- src/features/PlayerPage/hooks.tsx | 3 +- src/features/TeamPage/hooks.tsx | 3 +- src/features/TournamentPage/hooks.tsx | 3 +- src/requests/getMatches/getHomeMatches.tsx | 3 +- src/requests/getMatches/getPlayerMatches.tsx | 5 +++ src/requests/getMatches/getPreviews.tsx | 35 +++++++++++++++++-- src/requests/getMatches/getTeamMatches.tsx | 5 +++ .../getMatches/getTournamentMatches.tsx | 5 +++ src/requests/getMatches/request.tsx | 34 +++--------------- 9 files changed, 57 insertions(+), 39 deletions(-) diff --git a/src/features/PlayerPage/hooks.tsx b/src/features/PlayerPage/hooks.tsx index 21387970..8d0f4843 100644 --- a/src/features/PlayerPage/hooks.tsx +++ b/src/features/PlayerPage/hooks.tsx @@ -11,7 +11,6 @@ import { getPlayerInfo, getPlayerMatches } from 'requests' import { useName } from 'features/Name' import { useLexicsStore } from 'features/LexicsStore' -import { addSportType } from 'features/Matches/helpers/addSportType' import { useMatchSwitchesStore } from 'features/MatchSwitches' export const usePlayerPage = () => { @@ -54,7 +53,7 @@ export const usePlayerPage = () => { offset, playerId, sportType, - }).then((matches) => addSportType(matches, sportType)), + }), [ playerId, sportType, diff --git a/src/features/TeamPage/hooks.tsx b/src/features/TeamPage/hooks.tsx index a6cd4138..9f104b92 100644 --- a/src/features/TeamPage/hooks.tsx +++ b/src/features/TeamPage/hooks.tsx @@ -10,7 +10,6 @@ import { getTeamInfo, getTeamMatches } from 'requests' import { useSportNameParam, usePageId } from 'hooks' import { useName } from 'features/Name' -import { addSportType } from 'features/Matches/helpers/addSportType' import { useMatchSwitchesStore } from 'features/MatchSwitches' export const useTeamPage = () => { @@ -36,7 +35,7 @@ export const useTeamPage = () => { offset, sportType, teamId, - }).then((matches) => addSportType(matches, sportType)), + }), [ teamId, sportType, diff --git a/src/features/TournamentPage/hooks.tsx b/src/features/TournamentPage/hooks.tsx index f22dc39a..276b442b 100644 --- a/src/features/TournamentPage/hooks.tsx +++ b/src/features/TournamentPage/hooks.tsx @@ -10,7 +10,6 @@ import { getTournamentInfo, getTournamentMatches } from 'requests' import { useSportNameParam, usePageId } from 'hooks' import { useName } from 'features/Name' -import { addSportType } from 'features/Matches/helpers/addSportType' import { useMatchSwitchesStore } from 'features/MatchSwitches' export const useTournamentPage = () => { @@ -36,7 +35,7 @@ export const useTournamentPage = () => { offset, sportType, tournamentId, - }).then((matches) => addSportType(matches, sportType)), + }), [ tournamentId, sportType, diff --git a/src/requests/getMatches/getHomeMatches.tsx b/src/requests/getMatches/getHomeMatches.tsx index 8c7f8c65..e11847fc 100644 --- a/src/requests/getMatches/getHomeMatches.tsx +++ b/src/requests/getMatches/getHomeMatches.tsx @@ -4,6 +4,7 @@ import { MatchStatuses } from 'features/HeaderFilters' import type { MatchesBySection } from './types' import { requestMatches } from './request' +import { getMatchesPreviews } from './getPreviews' const proc = PROCEDURES.get_matches @@ -41,5 +42,5 @@ export const getHomeMatches = async ({ }, } - return requestMatches(config) + return requestMatches(config).then(getMatchesPreviews) } diff --git a/src/requests/getMatches/getPlayerMatches.tsx b/src/requests/getMatches/getPlayerMatches.tsx index c1377dce..908751b5 100644 --- a/src/requests/getMatches/getPlayerMatches.tsx +++ b/src/requests/getMatches/getPlayerMatches.tsx @@ -1,7 +1,10 @@ import { PROCEDURES, SportTypes } from 'config' +import { addSportType } from 'features/Matches/helpers/addSportType' + import type { MatchesBySection } from './types' import { requestMatches } from './request' +import { getMatchesPreviews } from './getPreviews' const proc = PROCEDURES.get_player_matches @@ -34,4 +37,6 @@ export const getPlayerMatches = async ({ } return requestMatches(config) + .then((matches) => addSportType(matches, sportType)) + .then(getMatchesPreviews) } diff --git a/src/requests/getMatches/getPreviews.tsx b/src/requests/getMatches/getPreviews.tsx index 3bac301f..5befda65 100644 --- a/src/requests/getMatches/getPreviews.tsx +++ b/src/requests/getMatches/getPreviews.tsx @@ -8,7 +8,7 @@ import { getMatchesPreviewImages } from 'requests' import { MatchStatuses } from 'features/HeaderFilters' -import type { Matches } from './types' +import type { MatchesBySection, Matches } from './types' const combinePreviews = (matches: Matches, previews: Previews) => ( map(matches, (match) => { @@ -23,7 +23,7 @@ const combinePreviews = (matches: Matches, previews: Previews) => ( /** * Запрашивает превью картинок матчей с видео и статус которых Завершенный */ -export const getPreviews = async (matches: Matches) => { +const getPreviews = async (matches: Matches) => { const previewsData = reduce( matches, (acc: PreviewsData, { @@ -48,3 +48,34 @@ export const getPreviews = async (matches: Matches) => { const previews = await getMatchesPreviewImages(previewsData) return combinePreviews(matches, previews) } + +export const getMatchesPreviews = async (matches: MatchesBySection) => { + if (matches.isVideoSections) { + const [ + broadcast, + features, + highlights, + ] = await Promise.all( + [ + getPreviews(matches.broadcast), + getPreviews(matches.features), + getPreviews(matches.highlights), + ], + ) + return { + ...matches, + broadcast, + features, + highlights, + } + } + + const broadcast = await getPreviews(matches.broadcast) + + return { + ...matches, + broadcast, + features: [], + highlights: [], + } +} diff --git a/src/requests/getMatches/getTeamMatches.tsx b/src/requests/getMatches/getTeamMatches.tsx index 150bf3f7..f30176e6 100644 --- a/src/requests/getMatches/getTeamMatches.tsx +++ b/src/requests/getMatches/getTeamMatches.tsx @@ -1,6 +1,9 @@ import { PROCEDURES, SportTypes } from 'config' +import { addSportType } from 'features/Matches/helpers/addSportType' + import type { MatchesBySection } from './types' +import { getMatchesPreviews } from './getPreviews' import { requestMatches } from './request' const proc = PROCEDURES.get_team_matches @@ -34,4 +37,6 @@ export const getTeamMatches = async ({ } return requestMatches(config) + .then((matches) => addSportType(matches, sportType)) + .then(getMatchesPreviews) } diff --git a/src/requests/getMatches/getTournamentMatches.tsx b/src/requests/getMatches/getTournamentMatches.tsx index fa760874..50b01d09 100644 --- a/src/requests/getMatches/getTournamentMatches.tsx +++ b/src/requests/getMatches/getTournamentMatches.tsx @@ -1,6 +1,9 @@ import { PROCEDURES, SportTypes } from 'config' +import { addSportType } from 'features/Matches/helpers/addSportType' + import type { MatchesBySection } from './types' +import { getMatchesPreviews } from './getPreviews' import { requestMatches } from './request' const proc = PROCEDURES.get_tournament_matches @@ -34,4 +37,6 @@ export const getTournamentMatches = async ({ } return requestMatches(config) + .then((matches) => addSportType(matches, sportType)) + .then(getMatchesPreviews) } diff --git a/src/requests/getMatches/request.tsx b/src/requests/getMatches/request.tsx index 12c8e7f5..cdfeeb41 100644 --- a/src/requests/getMatches/request.tsx +++ b/src/requests/getMatches/request.tsx @@ -2,7 +2,6 @@ import { DATA_URL } from 'config' import { callApi } from 'helpers' import type { MatchesResponse, MatchesBySection } from './types' -import { getPreviews } from './getPreviews' type Config = { body: { @@ -20,36 +19,11 @@ export const requestMatches = async (config: Config): Promise url: DATA_URL, }) - const isVideoSections = Boolean(is_video_sections) - - if (isVideoSections) { - const [ - broadcast, - features, - highlights, - ] = await Promise.all( - [ - getPreviews(data.broadcast), - getPreviews(data.features), - getPreviews(data.highlights), - ], - ) - return { - broadcast, - features, - hasNextPage: Boolean(show), - highlights, - isVideoSections, - } - } - - const broadcast = await getPreviews(data.broadcast) - return { - broadcast, - features: [], + broadcast: data.broadcast || [], + features: data.features || [], hasNextPage: Boolean(show), - highlights: [], - isVideoSections, + highlights: data.highlights || [], + isVideoSections: Boolean(is_video_sections), } }