Skip to content

Commit

Permalink
[Fix] 참여 상태의 변화에 따라 화면에 갱신 #1159
Browse files Browse the repository at this point in the history
  • Loading branch information
Junho Jeon committed Dec 14, 2023
1 parent d472338 commit f8f2cad
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
35 changes: 25 additions & 10 deletions components/modal/tournament/TournamentRegistryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,37 @@ export default function TournamentRegistryModal({
return instance
.post(`/pingpong/tournaments/${tournamentId}/users`)
.then((res) => {
alert('토너먼트 신청이 완료됐습니다');
// alert('토너먼트 신청이 완료됐습니다');
setLoading(false);
setRegistState(res.data.status);
if (playerCount < 8) setPlayerCount(playerCount + 1);
return res.data.status;
})
.catch((error) => {
alert('토너먼트 신청 중 에러가 발생했습니다.');
setError('토너먼트 신청 중 에러가 발생했습니다.');
setLoading(false);
});
}, [playerCount]);
}, []);

const unRegistTournament = useCallback(() => {
setLoading(true);
return instance
.delete(`/pingpong/tournaments/${tournamentId}/users`)
.then((res) => {
if (registState === 'WAIT') {
alert('토너먼트 대기가 취소 되었습니다');
// alert('토너먼트 대기가 취소 되었습니다');
} else {
setPlayerCount(playerCount - 1);
alert('토너먼트 등록이 취소 되었습니다');
// setPlayerCount(playerCount - 1);
// alert('토너먼트 등록이 취소 되었습니다');
}
setRegistState(res.data.status);
setLoading(false);
return res.data.status;
})
.catch((error) => {
alert('토너먼트 등록취소 중 에러가 발생했습니다');
setError('토너먼트 등록취소 중 에러가 발생했습니다');
setLoading(false);
});
}, [playerCount]);
}, []);

const getStatus = useCallback(() => {
return instance
Expand All @@ -87,13 +86,29 @@ export default function TournamentRegistryModal({
});
}, []);

const getTournamentInfo = useCallback(() => {
return instance
.get(`/pingpong/tournaments/${tournamentId}`)
.then((res) => {
setPlayerCount(res.data.player_cnt);
return res.data.player_cnt;
})
.catch((error) => {
setError('JJH2');
});
}, [tournamentId]);

useEffect(() => {
getTournamentInfo();
getStatus();
const date = new Date(startTime);
setOpenDate(dateToKRLocaleTimeString(date));
}, []);

console.log(registState);
useEffect(() => {
getTournamentInfo();
}, [registState]);

const closeModalButtonHandler = () => {
setModal({ modalName: null });
};
Expand Down
35 changes: 28 additions & 7 deletions components/tournament/TournamentCard.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useCallback, useEffect, useState } from 'react';
import { useSetRecoilState } from 'recoil';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { BiCalendar } from 'react-icons/bi';
import { MdPeopleAlt } from 'react-icons/md';
import { Modal } from 'types/modalTypes';
import { TournamentInfo } from 'types/tournamentTypes';
import { instance } from 'utils/axios';
import { dateToKRLocaleTimeString , dateToString } from 'utils/handleTime';
import { dateToKRLocaleTimeString } from 'utils/handleTime';
import { errorState } from 'utils/recoil/error';
import { modalState } from 'utils/recoil/modal';
import styles from 'styles/tournament/TournamentCard.module.scss';
Expand All @@ -22,8 +22,10 @@ export default function TournamentCard({
winnerImageUrl,
player_cnt,
}: TournamentInfo) {
const modal = useRecoilValue(modalState);
const setModal = useSetRecoilState<Modal>(modalState);
const [registState, setRegistState] = useState<string>('로딩중');
const [playerCount, setPlayerCount] = useState<number>(player_cnt);
const setError = useSetRecoilState(errorState);

const openTournamentInfoModal = () => {
Expand All @@ -44,9 +46,22 @@ export default function TournamentCard({
});
};

const getTournamentInfo = useCallback(() => {
return instance
.get(`/pingpong/tournaments/${tournamentId}`)
.then((res) => {
setPlayerCount(res.data.player_cnt);
return res.data.player_cnt;
})
.catch((error) => {
setError('JJH2');
});
}, [tournamentId]);

useEffect(() => {
getTournamentInfo();
getStatus();
}, []);
}, [modal]);

const getStatus = useCallback(() => {
return instance
Expand All @@ -62,7 +77,13 @@ export default function TournamentCard({

const start = dateToKRLocaleTimeString(new Date(startTime));
const end = dateToKRLocaleTimeString(new Date(endTime));
const isFull = player_cnt === 8 ? 'full' : 'notFull';
const isFull = playerCount === 8 ? 'full' : 'notFull';

const userState: Record<string, string> = {
BEFORE: '미 참여',
PLAYER: '참여 중',
WAIT: '대기 중',
};

return (
<div
Expand All @@ -74,10 +95,10 @@ export default function TournamentCard({
<div className={styles.title}>{title}</div>
<div className={styles.tag}>
<div className={`${styles.participants} ${styles[isFull]}`}>
<MdPeopleAlt /> {player_cnt} / 8
<MdPeopleAlt /> {playerCount} / 8
</div>
<div className={`${styles.state} ${styles[registState]}`}>
{registState}
{userState[registState]}
</div>
</div>
</div>
Expand Down

0 comments on commit f8f2cad

Please sign in to comment.