From d767f266f2def607ba0ed215b8fd292daadce1c7 Mon Sep 17 00:00:00 2001 From: Mirlan Date: Mon, 5 Oct 2020 15:00:30 +0600 Subject: [PATCH] Ott 461 matches request proc change (#165) * refactor(#461): matches request split into 4 different funcs (#161) Co-authored-by: mirlan.maksitaliev * refactor(#461): connected profile spec. matches request functions (#162) Co-authored-by: mirlan.maksitaliev * Ott 461 matches request proc change part 3 (#163) * refactor(#461): simplified matches requests a bit * refactor(#461): renamed matches types * refactor(#461): converted flat team to object Co-authored-by: mirlan.maksitaliev * refactor(#461): show sport and tournament names on specific profiles (#164) Co-authored-by: mirlan.maksitaliev --- src/config/index.tsx | 1 + src/config/procedures.tsx | 5 +- src/features/HomePage/hooks.tsx | 33 +++-- src/features/HomePage/index.tsx | 4 +- src/features/MatchCard/CardFinished/index.tsx | 24 ++-- src/features/MatchCard/CardLive/index.tsx | 24 ++-- src/features/MatchCard/CardSoon/index.tsx | 41 +++--- src/features/MatchCard/index.tsx | 23 +++- src/features/Matches/helpers.tsx | 52 -------- src/features/Matches/helpers/addSportType.tsx | 15 +++ .../Matches/helpers/prepareMatches.tsx | 63 ++++++++++ src/features/Matches/hooks.tsx | 42 +++---- src/features/PlayerPage/hooks.tsx | 20 +-- src/features/PlayerPage/index.tsx | 4 +- src/features/TeamPage/hooks.tsx | 20 +-- src/features/TeamPage/index.tsx | 4 +- src/features/TournamentPage/hooks.tsx | 24 ++-- src/features/TournamentPage/index.tsx | 4 +- src/requests/getMatches.tsx | 118 ------------------ src/requests/getMatches/getHomeMatches.tsx | 42 +++++++ src/requests/getMatches/getPlayerMatches.tsx | 34 +++++ src/requests/getMatches/getTeamMatches.tsx | 34 +++++ .../getMatches/getTournamentMatches.tsx | 34 +++++ src/requests/getMatches/index.tsx | 5 + src/requests/getMatches/request.tsx | 29 +++++ src/requests/getMatches/types.tsx | 50 ++++++++ src/requests/index.tsx | 1 + 27 files changed, 466 insertions(+), 284 deletions(-) delete mode 100644 src/features/Matches/helpers.tsx create mode 100644 src/features/Matches/helpers/addSportType.tsx create mode 100644 src/features/Matches/helpers/prepareMatches.tsx delete mode 100644 src/requests/getMatches.tsx create mode 100644 src/requests/getMatches/getHomeMatches.tsx create mode 100644 src/requests/getMatches/getPlayerMatches.tsx create mode 100644 src/requests/getMatches/getTeamMatches.tsx create mode 100644 src/requests/getMatches/getTournamentMatches.tsx create mode 100644 src/requests/getMatches/index.tsx create mode 100644 src/requests/getMatches/request.tsx create mode 100644 src/requests/getMatches/types.tsx diff --git a/src/config/index.tsx b/src/config/index.tsx index 6dccc043..ab2ad3fe 100644 --- a/src/config/index.tsx +++ b/src/config/index.tsx @@ -5,3 +5,4 @@ export * from './procedures' export * from './sportTypes' export * from './profileTypes' export * from './history' +export * from './devices' diff --git a/src/config/procedures.tsx b/src/config/procedures.tsx index 3e9d1dd3..b90d06c6 100644 --- a/src/config/procedures.tsx +++ b/src/config/procedures.tsx @@ -3,13 +3,16 @@ export const PROCEDURES = { create_user: 'create_user', get_cities: 'get_cities', get_match_info: 'get_match_info', - get_matches: 'get_matches', + get_matches_tmp: 'get_matches_tmp', get_player_info: 'get_player_info', + get_player_matches: 'get_player_matches', get_players_teams_tournaments: 'get_players_teams_tournaments', get_sport_list: 'get_sport_list', get_team_info: 'get_team_info', + get_team_matches: 'get_team_matches', get_tournament_info: 'get_tournament_info', get_tournament_list: 'get_tournament_list', + get_tournament_matches: 'get_tournament_matches', get_user_favorites: 'get_user_favorites', get_user_info: 'get_user_info', logout_user: 'logout_user', diff --git a/src/features/HomePage/hooks.tsx b/src/features/HomePage/hooks.tsx index cb3e32cc..5252f422 100644 --- a/src/features/HomePage/hooks.tsx +++ b/src/features/HomePage/hooks.tsx @@ -1,4 +1,6 @@ -import { useMemo } from 'react' +import { useCallback } from 'react' + +import { getHomeMatches } from 'requests' import { useHeaderFiltersStore } from 'features/HeaderFilters' @@ -10,17 +12,22 @@ export const useHomePage = () => { selectedTournamentId, } = useHeaderFiltersStore() - const requestArgs = useMemo(() => ({ - date: selectedDateFormatted, - matchStatus: selectedMatchStatus, - sportType: selectedSportTypeId, - tournamentId: selectedTournamentId, - }), [ - selectedDateFormatted, - selectedMatchStatus, - selectedSportTypeId, - selectedTournamentId, - ]) + const fetchMatches = useCallback( + (limit: number, offset: number) => getHomeMatches({ + date: selectedDateFormatted, + limit, + matchStatus: selectedMatchStatus, + offset, + sportType: selectedSportTypeId, + tournamentId: selectedTournamentId, + }), + [ + selectedDateFormatted, + selectedMatchStatus, + selectedSportTypeId, + selectedTournamentId, + ], + ) - return { requestArgs } + return { fetchMatches } } diff --git a/src/features/HomePage/index.tsx b/src/features/HomePage/index.tsx index 33d60acb..9ce6cf00 100644 --- a/src/features/HomePage/index.tsx +++ b/src/features/HomePage/index.tsx @@ -6,10 +6,10 @@ import { useHomePage } from './hooks' import { Content } from './styled' export const HomePage = () => { - const { requestArgs } = useHomePage() + const { fetchMatches } = useHomePage() return ( - + ) } diff --git a/src/features/MatchCard/CardFinished/index.tsx b/src/features/MatchCard/CardFinished/index.tsx index ac6f44e3..3a0dc32c 100644 --- a/src/features/MatchCard/CardFinished/index.tsx +++ b/src/features/MatchCard/CardFinished/index.tsx @@ -21,6 +21,7 @@ import { type CardFinishedProps = { match: Match, + showSportName?: boolean, } export const CardFinished = ({ @@ -30,13 +31,12 @@ export const CardFinished = ({ preview, sportName, sportType, - team1Name, - team1Score, - team2Name, - team2Score, + team1, + team2, time, tournamentName, }, + showSportName, }: CardFinishedProps) => { const { close, @@ -75,16 +75,20 @@ export const CardFinished = ({ /> - - {tournamentName} + {showSportName && } + {tournamentName && ( + + {tournamentName} + + )} - {team1Name} - {showScore && {team1Score}} + {team1.name} + {showScore && {team1.score}} - {team2Name} - {showScore && {team2Score}} + {team2.name} + {showScore && {team2.score}} diff --git a/src/features/MatchCard/CardLive/index.tsx b/src/features/MatchCard/CardLive/index.tsx index 24f306a5..c8354a0a 100644 --- a/src/features/MatchCard/CardLive/index.tsx +++ b/src/features/MatchCard/CardLive/index.tsx @@ -21,6 +21,7 @@ import { CardLiveHover } from '../CardLiveHover' type CardLiveProps = { match: Match, + showSportName?: boolean, } export const CardLive = ({ @@ -30,13 +31,12 @@ export const CardLive = ({ preview, sportName, sportType, - team1Name, - team1Score, - team2Name, - team2Score, + team1, + team2, time, tournamentName, }, + showSportName, }: CardLiveProps) => { const { close, @@ -75,16 +75,20 @@ export const CardLive = ({ /> - - {tournamentName} + {showSportName && } + {tournamentName && ( + + {tournamentName} + + )} - {team1Name} - {showScore && {team1Score}} + {team1.name} + {showScore && {team1.score}} - {team2Name} - {showScore && {team2Score}} + {team2.name} + {showScore && {team2.score}} diff --git a/src/features/MatchCard/CardSoon/index.tsx b/src/features/MatchCard/CardSoon/index.tsx index 48911498..fbee817d 100644 --- a/src/features/MatchCard/CardSoon/index.tsx +++ b/src/features/MatchCard/CardSoon/index.tsx @@ -2,10 +2,12 @@ import type { BaseSyntheticEvent } from 'react' import React, { useCallback } from 'react' import styled from 'styled-components/macro' -import { devices } from 'config/devices' -import type { Match } from 'features/Matches' +import { ProfileTypes, devices } from 'config' + import { handleImageError } from 'helpers' + +import type { Match } from 'features/Matches' import { SportName } from 'features/Common' import { @@ -53,25 +55,24 @@ const TeamName = styled(CommonTeamName)` type CardSoonProps = { match: Match, + showSportName?: boolean, } export const CardSoon = ({ match: { date, - sportName, sportType, - team1Logo, - team1Name, - team2Logo, - team2Name, + team1, + team2, time, tournamentName, }, + showSportName, }: CardSoonProps) => { const onError = useCallback((e: BaseSyntheticEvent) => handleImageError({ e, sport: sportType, - type: 2, + type: ProfileTypes.TEAMS, }), [sportType]) return ( @@ -85,28 +86,32 @@ export const CardSoon = ({ - - {tournamentName} + {showSportName && } + {tournamentName && ( + + {tournamentName} + + )} - {team1Name} + {team1.name} - {team2Name} + {team2.name} diff --git a/src/features/MatchCard/index.tsx b/src/features/MatchCard/index.tsx index 1aed7895..27900a6e 100644 --- a/src/features/MatchCard/index.tsx +++ b/src/features/MatchCard/index.tsx @@ -1,4 +1,7 @@ import React from 'react' +import { useRouteMatch } from 'react-router' + +import { PAGES } from 'config' import type { Match } from 'features/Matches' import { MatchStatuses } from 'features/HeaderFilters' @@ -11,11 +14,19 @@ type MatchCardProps = { match: Match, } +const cards = { + [MatchStatuses.Finished]: CardFinished, + [MatchStatuses.Live]: CardLive, + [MatchStatuses.Soon]: CardSoon, +} + export const MatchCard = ({ match }: MatchCardProps) => { - switch (match.streamStatus) { - case MatchStatuses.Soon: return - case MatchStatuses.Live: return - case MatchStatuses.Finished: return - default: return null - } + const isHomePage = useRouteMatch(PAGES.home)?.isExact + const Card = cards[match.streamStatus] + return ( + + ) } diff --git a/src/features/Matches/helpers.tsx b/src/features/Matches/helpers.tsx deleted file mode 100644 index fe8b9a64..00000000 --- a/src/features/Matches/helpers.tsx +++ /dev/null @@ -1,52 +0,0 @@ -import map from 'lodash/map' -import flatten from 'lodash/flatten' -import pipe from 'lodash/fp/pipe' -import fpMap from 'lodash/fp/map' - -import format from 'date-fns/format' - -import type { Content } from 'requests' -import { ProfileTypes } from 'config' -import { getProfileLogo, getSportLexic } from 'helpers' - -type Name = 'name_rus' | 'name_eng' - -const prepareMatch = ({ - matches: matchesList, - sport, - ...rest -}: Content, suffix: string) => map(matchesList, ({ - date, - id, - stream_status, - team1, - team2, -}) => ({ - date: format(new Date(date), 'dd.MM.yy'), - id, - preview: '/images/preview.png', - sportName: getSportLexic(sport), - sportType: sport, - streamStatus: stream_status, - team1Logo: getProfileLogo({ - id: team1.id, - profileType: ProfileTypes.TEAMS, - sportType: sport, - }), - team1Name: team1[`name_${suffix}` as Name], - team1Score: team1.score, - team2Logo: getProfileLogo({ - id: team2.id, - profileType: ProfileTypes.TEAMS, - sportType: sport, - }), - team2Name: team2[`name_${suffix}` as Name], - team2Score: team2.score, - time: format(new Date(date), 'HH:mm'), - tournamentName: rest[`name_${suffix}` as Name], -})) - -export const prepareMatches = (content: Array, suffix: string) => pipe( - fpMap((items: Content) => prepareMatch(items, suffix)), - flatten, -)(content) diff --git a/src/features/Matches/helpers/addSportType.tsx b/src/features/Matches/helpers/addSportType.tsx new file mode 100644 index 00000000..94d3c3d5 --- /dev/null +++ b/src/features/Matches/helpers/addSportType.tsx @@ -0,0 +1,15 @@ +import map from 'lodash/map' + +import type { MatchesBySection, Matches } from 'requests' +import { SportTypes } from 'config' + +const addSportTypeToMatches = (matches: Matches, sport: SportTypes) => ( + map(matches, (match) => ({ ...match, sport })) +) + +export const addSportType = (matches: MatchesBySection, sport: SportTypes) => ({ + ...matches, + broadcast: addSportTypeToMatches(matches.broadcast, sport), + features: addSportTypeToMatches(matches.features, sport), + highlights: addSportTypeToMatches(matches.highlights, sport), +}) diff --git a/src/features/Matches/helpers/prepareMatches.tsx b/src/features/Matches/helpers/prepareMatches.tsx new file mode 100644 index 00000000..9f5f0fce --- /dev/null +++ b/src/features/Matches/helpers/prepareMatches.tsx @@ -0,0 +1,63 @@ +import map from 'lodash/map' + +import format from 'date-fns/format' + +import type { Match, Team } from 'requests' +import { ProfileTypes, SportTypes } from 'config' +import { getProfileLogo, getSportLexic } from 'helpers' + +type Name = 'name_rus' | 'name_eng' + +type Args = { + sport: SportTypes, + suffix: string, + team: Team, +} + +const prepareTeam = ({ + sport, + suffix, + team, +}: Args) => ({ + logo: getProfileLogo({ + id: team.id, + profileType: ProfileTypes.TEAMS, + sportType: sport, + }), + name: team[`name_${suffix}` as Name], + score: team.score, +}) + +const prepareMatch = ({ + date, + id, + sport, + stream_status, + team1, + team2, + tournament, +}: Match, suffix: string) => ({ + date: format(new Date(date), 'dd.MM.yy'), + id, + preview: '/images/preview.png', + sportName: getSportLexic(sport), + sportType: sport, + streamStatus: stream_status, + team1: prepareTeam({ + sport, + suffix, + team: team1, + }), + team2: prepareTeam({ + sport, + suffix, + team: team2, + }), + time: format(new Date(date), 'HH:mm'), + tournamentName: tournament?.[`name_${suffix}` as Name], +}) + +export const prepareMatches = (matches: Array, suffix: string) => map( + matches, + (match: Match) => prepareMatch(match, suffix), +) diff --git a/src/features/Matches/hooks.tsx b/src/features/Matches/hooks.tsx index aea0f6aa..ebb05e79 100644 --- a/src/features/Matches/hooks.tsx +++ b/src/features/Matches/hooks.tsx @@ -6,59 +6,51 @@ import { useRef, } from 'react' -import type { Matches } from 'requests' -import { getMatches } from 'requests' +import type { MatchesBySection } from 'requests' import { useRequest } from 'hooks' import { useLexicsStore } from 'features/LexicsStore' -import { prepareMatches } from './helpers' +import { prepareMatches } from './helpers/prepareMatches' export type Match = ReturnType[number] -type RequestArgs = Omit[0], 'limit' | 'offset'> - export type Props = { - requestArgs: RequestArgs, + fetch: (limit: number, offset: number) => Promise, } const MATCHES_LIMIT = 60 -export const useMatches = ({ requestArgs }: Props) => { +const initialState = { + broadcast: [], + features: [], + hasNextPage: true, + highlights: [], + isVideoSections: false, +} + +export const useMatches = ({ fetch }: Props) => { const { suffix } = useLexicsStore() const { isFetching, request: requestMatches, - } = useRequest(getMatches) + } = useRequest(fetch) const pageRef = useRef(0) - const [matches, setMatches] = useState({ - broadcast: [], - features: [], - hasNextPage: true, - highlights: [], - isVideoSections: false, - }) + const [matches, setMatches] = useState(initialState) const { hasNextPage } = matches const fetchMatches = useCallback((page: number) => ( - requestMatches({ - ...requestArgs, - limit: MATCHES_LIMIT, - offset: page * MATCHES_LIMIT, - }) - ), [ - requestMatches, - requestArgs, - ]) + requestMatches(MATCHES_LIMIT, page * MATCHES_LIMIT) + ), [requestMatches]) const fetchMoreMatches = useCallback(async () => { if (!hasNextPage || isFetching) return const newMatches = await fetchMatches(pageRef.current) - setMatches((oldMatches): Matches => { + setMatches((oldMatches) => { const broadcast = [...oldMatches.broadcast, ...newMatches.broadcast] return { ...oldMatches, diff --git a/src/features/PlayerPage/hooks.tsx b/src/features/PlayerPage/hooks.tsx index b663903c..f75a8b78 100644 --- a/src/features/PlayerPage/hooks.tsx +++ b/src/features/PlayerPage/hooks.tsx @@ -1,15 +1,16 @@ import { useEffect, useState, - useMemo, + useCallback, } from 'react' import { useSportNameParam, usePageId } from 'hooks' import type { PlayerProfile } from 'requests/getPlayerInfo' -import { getPlayerInfo } from 'requests/getPlayerInfo' +import { getPlayerInfo, getPlayerMatches } from 'requests' import { useLexicsStore } from 'features/LexicsStore' +import { addSportType } from 'features/Matches/helpers/addSportType' type Firstname = 'firstname_eng' | 'firstname_rus' type Lastname = 'lastname_eng' | 'lastname_rus' @@ -42,14 +43,19 @@ export const usePlayerPage = () => { getPlayerInfo(playerId, sportType).then(setPlayerProfile) }, [playerId, sportType]) - const requestArgs = useMemo(() => ({ - playerId, - sportType, - }), [playerId, sportType]) + const fetchMatches = useCallback( + (limit: number, offset: number) => getPlayerMatches({ + limit, + offset, + playerId, + sportType, + }).then((matches) => addSportType(matches, sportType)), + [playerId, sportType], + ) return { + fetchMatches, infoItems, name: fullName, - requestArgs, } } diff --git a/src/features/PlayerPage/index.tsx b/src/features/PlayerPage/index.tsx index d75ec122..d7b0c1f3 100644 --- a/src/features/PlayerPage/index.tsx +++ b/src/features/PlayerPage/index.tsx @@ -9,9 +9,9 @@ import { usePlayerPage } from './hooks' export const PlayerPage = () => { const { + fetchMatches, infoItems, name, - requestArgs, } = usePlayerPage() return ( @@ -21,7 +21,7 @@ export const PlayerPage = () => { name={name} infoItems={infoItems} /> - + ) } diff --git a/src/features/TeamPage/hooks.tsx b/src/features/TeamPage/hooks.tsx index 4d370b7b..0b2e4c3c 100644 --- a/src/features/TeamPage/hooks.tsx +++ b/src/features/TeamPage/hooks.tsx @@ -1,15 +1,16 @@ import { useEffect, useState, - useMemo, + useCallback, } from 'react' import type { TeamInfo } from 'requests' -import { getTeamInfo } from 'requests' +import { getTeamInfo, getTeamMatches } from 'requests' import { useLexicsStore } from 'features/LexicsStore' import { useSportNameParam, usePageId } from 'hooks' +import { addSportType } from 'features/Matches/helpers/addSportType' type Name = 'name_rus' | 'name_eng' @@ -36,15 +37,20 @@ export const useTeamPage = () => { teamId, ]) - const requestArgs = useMemo(() => ({ - sportType, - teamId, - }), [teamId, sportType]) + const fetchMatches = useCallback( + (limit: number, offset: number) => getTeamMatches({ + limit, + offset, + sportType, + teamId, + }).then((matches) => addSportType(matches, sportType)), + [teamId, sportType], + ) return { + fetchMatches, infoItems: [country], name, - requestArgs, sportType, } } diff --git a/src/features/TeamPage/index.tsx b/src/features/TeamPage/index.tsx index 47805c5f..daf3f56e 100644 --- a/src/features/TeamPage/index.tsx +++ b/src/features/TeamPage/index.tsx @@ -10,9 +10,9 @@ import { Content } from './styled' export const TeamPage = () => { const { + fetchMatches, infoItems, name, - requestArgs, } = useTeamPage() return ( @@ -23,7 +23,7 @@ export const TeamPage = () => { name={name} infoItems={infoItems} /> - + ) diff --git a/src/features/TournamentPage/hooks.tsx b/src/features/TournamentPage/hooks.tsx index 5fd4a7a8..58af3f58 100644 --- a/src/features/TournamentPage/hooks.tsx +++ b/src/features/TournamentPage/hooks.tsx @@ -1,16 +1,17 @@ import { useEffect, useState, - useMemo, + useCallback, } from 'react' -import { useLexicsStore } from 'features/LexicsStore' - import type { TournamentInfo } from 'requests' -import { getTournamentInfo } from 'requests' +import { getTournamentInfo, getTournamentMatches } from 'requests' import { useSportNameParam, usePageId } from 'hooks' +import { useLexicsStore } from 'features/LexicsStore' +import { addSportType } from 'features/Matches/helpers/addSportType' + type Name = 'name_rus' | 'name_eng' export const useTournamentPage = () => { @@ -36,14 +37,19 @@ export const useTournamentPage = () => { tournamentId, ]) - const requestArgs = useMemo(() => ({ - sportType, - tournamentId, - }), [tournamentId, sportType]) + const fetchMatches = useCallback( + (limit: number, offset: number) => getTournamentMatches({ + limit, + offset, + sportType, + tournamentId, + }).then((matches) => addSportType(matches, sportType)), + [tournamentId, sportType], + ) return { + fetchMatches, infoItems: [country], name, - requestArgs, } } diff --git a/src/features/TournamentPage/index.tsx b/src/features/TournamentPage/index.tsx index 41ba841a..0c4df901 100644 --- a/src/features/TournamentPage/index.tsx +++ b/src/features/TournamentPage/index.tsx @@ -10,9 +10,9 @@ import { Content } from './styled' export const TournamentPage = () => { const { + fetchMatches, infoItems, name, - requestArgs, } = useTournamentPage() return ( @@ -23,7 +23,7 @@ export const TournamentPage = () => { name={name} infoItems={infoItems} /> - + ) diff --git a/src/requests/getMatches.tsx b/src/requests/getMatches.tsx deleted file mode 100644 index f4dfd31e..00000000 --- a/src/requests/getMatches.tsx +++ /dev/null @@ -1,118 +0,0 @@ -import { - DATA_URL, - PROCEDURES, - SportTypes, -} from 'config' -import { callApi, getResponseData } from 'helpers' - -import { MatchStatuses } from 'features/HeaderFilters' - -const proc = PROCEDURES.get_matches - -type Data = { - is_video_sections: boolean, - video_content: VideoContent, -} - -type VideoContent = { - broadcast: Items, - features: Items, - highlights: Items, - show: boolean, -} - -type Items = { - content: Array | null, - name: string, -} - -export type Content = { - id: number, - matches: Array, - name_eng: string, - name_rus: string, - sport: SportTypes, -} - -type Match = { - date: string, - has_video: boolean, - id: number, - round_id: number | null, - stream_status: MatchStatuses, - team1: Team, - team2: Team, -} - -type Team = { - id: number, - name_eng: string, - name_rus: string, - score: number, -} - -type Args = { - date?: string, - limit?: number, - matchStatus?: MatchStatuses | null, - offset?: number, - playerId?: number | null, - sportType: SportTypes | null, - teamId?: number | null, - tournamentId?: number | null, -} - -export type Matches = { - broadcast: Array, - features: Array, - hasNextPage?: boolean, - highlights: Array, - isVideoSections: boolean, -} - -export const getMatches = async ({ - date, - limit, - matchStatus, - offset, - playerId, - sportType, - teamId, - tournamentId, -}: Args) => { - const config = { - body: { - params: { - _p_date: date, - _p_limit: limit, - _p_offset: offset, - _p_player_id: playerId || null, - _p_sport: sportType, - _p_stream_status: matchStatus, - _p_team_id: teamId || null, - _p_tournament_id: tournamentId || null, - }, - proc, - }, - } - - const data: Data = await callApi({ - config, - url: DATA_URL, - }).then(getResponseData(proc)) - - const { - broadcast, - features, - highlights, - show, - } = data.video_content - - return { - broadcast: broadcast.content || [], - features: features.content || [], - hasNextPage: Boolean(show), - highlights: highlights.content || [], - isVideoSections: data.is_video_sections, - } -} diff --git a/src/requests/getMatches/getHomeMatches.tsx b/src/requests/getMatches/getHomeMatches.tsx new file mode 100644 index 00000000..747abe94 --- /dev/null +++ b/src/requests/getMatches/getHomeMatches.tsx @@ -0,0 +1,42 @@ +import { PROCEDURES, SportTypes } from 'config' + +import { MatchStatuses } from 'features/HeaderFilters' + +import type { MatchesBySection } from './types' +import { requestMatches } from './request' + +const proc = PROCEDURES.get_matches_tmp + +type Args = { + date: string, + limit: number, + matchStatus: MatchStatuses | null, + offset: number, + sportType: SportTypes | null, + tournamentId: number | null, +} + +export const getHomeMatches = async ({ + date, + limit, + matchStatus, + offset, + sportType, + tournamentId, +}: Args): Promise => { + const config = { + body: { + params: { + _p_date: date, + _p_limit: limit, + _p_offset: offset, + _p_sport: sportType, + _p_stream_status: matchStatus, + _p_tournament_id: tournamentId, + }, + proc, + }, + } + + return requestMatches(config) +} diff --git a/src/requests/getMatches/getPlayerMatches.tsx b/src/requests/getMatches/getPlayerMatches.tsx new file mode 100644 index 00000000..b7cae6a4 --- /dev/null +++ b/src/requests/getMatches/getPlayerMatches.tsx @@ -0,0 +1,34 @@ +import { PROCEDURES, SportTypes } from 'config' + +import type { MatchesBySection } from './types' +import { requestMatches } from './request' + +const proc = PROCEDURES.get_player_matches + +type Args = { + limit: number, + offset: number, + playerId: number, + sportType: SportTypes, +} + +export const getPlayerMatches = async ({ + limit, + offset, + playerId, + sportType, +}: Args): Promise => { + const config = { + body: { + params: { + _p_limit: limit, + _p_offset: offset, + _p_player_id: playerId, + _p_sport: sportType, + }, + proc, + }, + } + + return requestMatches(config) +} diff --git a/src/requests/getMatches/getTeamMatches.tsx b/src/requests/getMatches/getTeamMatches.tsx new file mode 100644 index 00000000..dd86ab0c --- /dev/null +++ b/src/requests/getMatches/getTeamMatches.tsx @@ -0,0 +1,34 @@ +import { PROCEDURES, SportTypes } from 'config' + +import type { MatchesBySection } from './types' +import { requestMatches } from './request' + +const proc = PROCEDURES.get_team_matches + +type Args = { + limit: number, + offset: number, + sportType: SportTypes, + teamId: number, +} + +export const getTeamMatches = async ({ + limit, + offset, + sportType, + teamId, +}: Args): Promise => { + const config = { + body: { + params: { + _p_limit: limit, + _p_offset: offset, + _p_sport: sportType, + _p_team_id: teamId, + }, + proc, + }, + } + + return requestMatches(config) +} diff --git a/src/requests/getMatches/getTournamentMatches.tsx b/src/requests/getMatches/getTournamentMatches.tsx new file mode 100644 index 00000000..70369b16 --- /dev/null +++ b/src/requests/getMatches/getTournamentMatches.tsx @@ -0,0 +1,34 @@ +import { PROCEDURES, SportTypes } from 'config' + +import type { MatchesBySection } from './types' +import { requestMatches } from './request' + +const proc = PROCEDURES.get_tournament_matches + +type Args = { + limit: number, + offset: number, + sportType: SportTypes, + tournamentId: number, +} + +export const getTournamentMatches = async ({ + limit, + offset, + sportType, + tournamentId, +}: Args): Promise => { + const config = { + body: { + params: { + _p_limit: limit, + _p_offset: offset, + _p_sport: sportType, + _p_tournament_id: tournamentId, + }, + proc, + }, + } + + return requestMatches(config) +} diff --git a/src/requests/getMatches/index.tsx b/src/requests/getMatches/index.tsx new file mode 100644 index 00000000..3e17f564 --- /dev/null +++ b/src/requests/getMatches/index.tsx @@ -0,0 +1,5 @@ +export * from './types' +export * from './getHomeMatches' +export * from './getTeamMatches' +export * from './getPlayerMatches' +export * from './getTournamentMatches' diff --git a/src/requests/getMatches/request.tsx b/src/requests/getMatches/request.tsx new file mode 100644 index 00000000..edc8f21b --- /dev/null +++ b/src/requests/getMatches/request.tsx @@ -0,0 +1,29 @@ +import { DATA_URL } from 'config' +import { callApi, getResponseData } from 'helpers' + +import type { MatchesResponse, MatchesBySection } from './types' + +type Config = { + body: { + proc: string, + }, +} + +export const requestMatches = async (config: Config): Promise => { + const { + is_video_sections, + show, + video_content: data, + }: MatchesResponse = await callApi({ + config, + url: DATA_URL, + }).then(getResponseData(config.body.proc)) + + return { + broadcast: data.broadcast || [], + features: data.features || [], + hasNextPage: Boolean(show), + highlights: data.highlights || [], + isVideoSections: Boolean(is_video_sections), + } +} diff --git a/src/requests/getMatches/types.tsx b/src/requests/getMatches/types.tsx new file mode 100644 index 00000000..4db90d8f --- /dev/null +++ b/src/requests/getMatches/types.tsx @@ -0,0 +1,50 @@ +import { SportTypes } from 'config' + +import { MatchStatuses } from 'features/HeaderFilters' + +type Tournament = { + id: number, + name_eng: string, + name_rus: string, +} + +export type Team = { + id: number, + name_eng: string, + name_rus: string, + score: number, +} + +export type Match = { + date: string, + has_video: boolean, + id: number, + round_id: number | null, + sport: SportTypes, + stream_status: MatchStatuses, + team1: Team, + team2: Team, + tournament: Tournament, +} + +export type Matches = Array + +type VideoContent = { + broadcast: Matches, + features: Matches, + highlights: Matches, +} + +export type MatchesResponse = { + is_video_sections: boolean | null, + show: boolean, + video_content: VideoContent, +} + +export type MatchesBySection = { + broadcast: Matches, + features: Matches, + hasNextPage: boolean, + highlights: Matches, + isVideoSections: boolean, +} diff --git a/src/requests/index.tsx b/src/requests/index.tsx index 6713131b..502d5a61 100644 --- a/src/requests/index.tsx +++ b/src/requests/index.tsx @@ -16,3 +16,4 @@ export * from './getMatchInfo' export * from './reportPlayerProgress' export * from './getVideos' export * from './saveUserInfo' +export * from './getPlayerInfo'