Skip to content

Commit

Permalink
Merge pull request #214 from Andres2D/decouple-leave-match-on-details
Browse files Browse the repository at this point in the history
Decouple leave match on details
  • Loading branch information
Andres2D authored Feb 21, 2023
2 parents eb28768 + b0e5fa5 commit c546293
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 49 deletions.
2 changes: 1 addition & 1 deletion components/layout/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const Navbar: NextPage = () => {
>
Log out
</Button>
<p>Version 2.6.1</p>
<p>Version 2.6.2</p>
</DrawerFooter>
</DrawerContent>
</Drawer>
Expand Down
53 changes: 9 additions & 44 deletions components/matches/detail/match-detail.tsx
Original file line number Diff line number Diff line change
@@ -1,54 +1,26 @@
import { Button, useDisclosure, useToast } from '@chakra-ui/react';
import { Button } from '@chakra-ui/react';
import type { NextPage } from 'next';
import { useSelector } from 'react-redux';
import { useMutation } from 'react-query';
import { useRouter } from 'next/router';
import { useDispatch, useSelector } from 'react-redux';
import { RootState } from '../../../interfaces/State';
import ChangePlayerModal from './avatar/change-player.modal';
import FieldLayout from './field/field';
import styles from './match-detail.module.scss';
import LeaveIcon from '../../../assets/svg/leave.svg';
import ModalAlert from '../../layout/modal-alert';
import { leaveMatch } from '../../../services/api-configuration';
import MatchDetailsSkeleton from '../../skeletons/match-details-skeleton';
import { initialState } from '../../../store/match-details.slice';
import LeaveMatchModal from '../modals/leave-match.modal';
import { matchesListActions } from '../../../store/matches-list.slice';

const MatchDetailsLayout: NextPage = () => {
const matchDetails = useSelector((state: RootState) => state.matchDetails);
const {
isOpen: leaveMatchModalIsOpen,
onOpen: leaveMatchModalOnOpen,
onClose: leaveMatchModalOnClose
} = useDisclosure();
const router = useRouter();
const toast = useToast();

const { mutate: mutateLeaveMatch } = useMutation(leaveMatch, {
onSuccess: async (response) => {
if(response.ok) {
router.push('/matches', undefined, { })
} else {
toast({
title: 'Leave match',
description: "Something went wrong, please try again later.",
status: 'error',
duration: 9000,
isClosable: true,
});
leaveMatchModalOnClose();
}
},
onError: () => {
// TODO: handle error
}
});
const dispatch = useDispatch();

if(matchDetails === initialState) {
return <MatchDetailsSkeleton />
}

const handleLeaveMatch = () => {
mutateLeaveMatch(matchDetails.match._id);
const handleLeaveMatchModalOpen = () => {
dispatch(matchesListActions.setMatchModalAction({action: 'isLeaveMatch', value: true }));
};

return (
Expand All @@ -65,20 +37,13 @@ const MatchDetailsLayout: NextPage = () => {
colorScheme='telegram'
className={styles.leaveButton}
rightIcon={<LeaveIcon />}
onClick={leaveMatchModalOnOpen}
onClick={handleLeaveMatchModalOpen}
>
Leave match
</Button>
}
</section>
<ModalAlert
isOpen={leaveMatchModalIsOpen}
onClose={leaveMatchModalOnClose}
onContinue={handleLeaveMatch}
title='Leave match'
description={`Are you sure? You would request to a team mate to add you again afterwards`}
continueLabel='Leave match'
/>
<LeaveMatchModal navigateToMatches/>
</>
);
};
Expand Down
13 changes: 12 additions & 1 deletion components/matches/modals/leave-match.modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import { useDisclosure, useToast } from '@chakra-ui/react';
import type { NextPage } from 'next';
import { useMutation } from 'react-query';
import { useSelector, useDispatch } from 'react-redux';
import { useRouter } from 'next/router';
import { useEffect } from 'react';
import ModalAlert from '../../layout/modal-alert';
import { RootState } from '../../../interfaces/State';
import { matchesListActions } from '../../../store/matches-list.slice';
import { leaveMatch } from '../../../services/api-configuration';

const LeaveMatchModal: NextPage = () => {
interface Props {
navigateToMatches?: boolean;
}

const LeaveMatchModal: NextPage<Props> = ({navigateToMatches}) => {
const { isOpen, onOpen, onClose } = useDisclosure();
const matchesState = useSelector((state: RootState) => state.matchesList);
const dispatch = useDispatch();
const toast = useToast();
const router = useRouter();

useEffect(() => {
if(matchesState.isLeaveMatch) {
Expand All @@ -26,6 +32,11 @@ const LeaveMatchModal: NextPage = () => {
dispatch(matchesListActions.deleteLeaveMatch({id: matchesState.selectedMatch?._id!}));
dispatch(matchesListActions.setSelectedMatch(undefined));
handleCloseModal();

if(navigateToMatches) {
router.push('/matches', undefined, { })
}

toast({
title: 'Leave match',
description: 'You left the match.',
Expand Down
15 changes: 15 additions & 0 deletions pages/matches/[...matchId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { IMatchDetailsResponse } from '../../../interfaces/Match';
import { getMatch } from '../../../server/matches';
import { getProfile } from '../../../server/player';
import { matchDetailsActions } from '../../../store/match-details.slice';
import { matchesListActions } from '../../../store/matches-list.slice';
import { profileActions } from '../../../store/profile.slice';

interface Props {
Expand All @@ -27,6 +28,7 @@ const MatchDetails: NextPage<Props> = ({match, profile}) => {
const flagResponse = getFlagSvg(parsedProfile.nationality, true);

useEffect(() => {

dispatch(profileActions.setProfile({
_id: parsedProfile._id,
overall: 0,
Expand All @@ -36,12 +38,25 @@ const MatchDetails: NextPage<Props> = ({match, profile}) => {
image: parsedProfile.image,
nationality: parsedProfile.nationality,
}));

}, []);

const matchDetail: IMatchDetailsResponse = JSON.parse(match);

useEffect(() => {
dispatch(matchDetailsActions.setMatchState({...matchDetail, playersSelected: [], changePlayerModalActive: false}));

dispatch(matchesListActions.setSelectedMatch({
_id: matchDetail.match._id,
away_team: matchDetail.match.away_team,
awayScore: matchDetail.match.awayScore,
date: matchDetail.match.date,
fullTime: matchDetail.match.fullTime,
home_team: matchDetail.match.home_team,
homeScore: matchDetail.match.homeScore,
location: matchDetail.match.location
}));

}, [dispatch, matchDetail])

return (
Expand Down
3 changes: 1 addition & 2 deletions pages/matches/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ const Matches: NextPage<Props> = ({matches, profile}) => {
image: parsedProfile.image,
nationality: parsedProfile.nationality,
}));
console.log(matchesList);


dispatch(matchesListActions.setMatchesList(matchesList));

}, []);
Expand Down
1 change: 0 additions & 1 deletion store/matches-list.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const initialState: MatchesListState = {

const setMatchesList: CaseReducer<MatchesListState, PayloadAction<FullMatch[]>> =
(state: MatchesListState, action: PayloadAction<FullMatch[]>) => {
console.log('action', action);
state.matches = action.payload;
};

Expand Down

1 comment on commit c546293

@vercel
Copy link

@vercel vercel bot commented on c546293 Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cotejoapp – ./

cotejoapp-andres2d.vercel.app
cotejoapp.vercel.app
cotejoapp-git-main-andres2d.vercel.app

Please sign in to comment.