From 2ab756114d10eb9e1188ba0e3b23e9919c652fce Mon Sep 17 00:00:00 2001 From: Andres2D Date: Sat, 18 Feb 2023 16:23:58 -0500 Subject: [PATCH] Add real time update on update match --- components/matches/match-list.tsx | 19 +++++++++++++++++-- services/api-configuration.ts | 6 +++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/components/matches/match-list.tsx b/components/matches/match-list.tsx index e2f5432..ade5f1f 100644 --- a/components/matches/match-list.tsx +++ b/components/matches/match-list.tsx @@ -123,6 +123,23 @@ const MatchList: NextPage = ({ matches }) => { const { mutate: mutateUpdateMatch } = useMutation(updateMatch, { onSuccess: async (response) => { if (response.ok) { + setMatchesList((matches) => + matches.map((match) => { + if(match._id === selectedMatch?._id) { + return { + ...match, + location: response.data.match.location, + date: response.data.match.date, + fullTime: response.data.match.fullTime, + homeScore: response.data.match.homeScore, + awayScore: response.data.match.awayScore + }; + }else { + return match; + } + }) + ); + fulltimeModalOnClose(); updateMatchModalOnClose(); setSelectedMatch(undefined); @@ -213,8 +230,6 @@ const MatchList: NextPage = ({ matches }) => { location: place || selectedMatch?.location || '' }; - console.log(request); - mutateUpdateMatch(request); }; diff --git a/services/api-configuration.ts b/services/api-configuration.ts index 739c050..bd31ad1 100644 --- a/services/api-configuration.ts +++ b/services/api-configuration.ts @@ -1,8 +1,8 @@ -import { create } from 'apisauce' +import { ApiResponse, create } from 'apisauce' import { ICreateMatchRequest, FullMatch } from '../interfaces/Match'; import { UpdateProfileRequest, RegisterPlayerRequest } from '../interfaces/Player'; import { UpdateTeamRequest } from '../interfaces/Team'; -import { IUpdateTeamPlayerRequest, IChangePlayerRequest, LeftMatchRequest } from '../interfaces/TeamPlayer'; +import { IUpdateTeamPlayerRequest, IChangePlayerRequest } from '../interfaces/TeamPlayer'; const api = create({ baseURL: '/api', @@ -17,7 +17,7 @@ export const changePlayer = (request: IUpdateTeamPlayerRequest | IChangePlayerRe export const leaveMatch = (id: string) => api.delete('/team-player', { idMatch: id }); export const createMatch = (request: ICreateMatchRequest) => api.post('/match', request); -export const updateMatch = (match: FullMatch) => api.put('/match', match); +export const updateMatch = (match: FullMatch): Promise> => api.put('/match', match); export const deleteMatch = (id: string) => api.delete('/match', { id }); export const updateTeam = (request: UpdateTeamRequest) => api.put('/team', request);