Skip to content

Commit 9599cbd

Browse files
[FIX] userState에서 데이터를 가져오기로 변경
1 parent 12d90da commit 9599cbd

5 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/app/Game/QuestionPhase/VoteForAccuse.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { Alert, AlertTitle, Box } from '@mui/material';
44

55
import useHandler from '@/app/hooks/useHandler';
66

7-
import { selectInvitationCode, selectPlayers, selectSpy } from '@/redux/slices/gameSlice';
7+
import { selectPlayers, selectSpy } from '@/redux/slices/gameSlice';
88
import { selectNominator, selectNominee, selectVotes } from '@/redux/slices/questionPhaseSlice';
9-
import { UserState, selectId } from '@/redux/slices/userSlice';
9+
import { UserState, selectId, selectUser } from '@/redux/slices/userSlice';
1010

1111
import { UserId } from '@/types/UserId';
1212
import PlayersWhoDidNotVote from '@/app/components/PlayersWhoDidNotVote';
@@ -18,7 +18,7 @@ import { LOCAL_STORAGE_END_TIME, LOCAL_STORAGE_PAUSE_START_TIME } from '@/consta
1818
export default function VoteForAccuse() {
1919
const nomineeId = useSelector(selectNominee);
2020
const nominatorId = useSelector(selectNominator);
21-
const invitationCode = useSelector(selectInvitationCode);
21+
const { invitationCode } = useSelector(selectUser);
2222
const players = useSelector(selectPlayers);
2323
const votes = useSelector(selectVotes);
2424
const spy = useSelector(selectSpy);

src/app/Game/VotePhase/VoteForm.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Dispatch, SetStateAction } from 'react';
22
import { useSelector } from 'react-redux';
3-
import { selectInvitationCode, selectPlayers } from '@/redux/slices/gameSlice';
4-
import { UserState, selectId } from '@/redux/slices/userSlice';
3+
import { selectPlayers } from '@/redux/slices/gameSlice';
4+
import { UserState, selectId, selectUser } from '@/redux/slices/userSlice';
55
import useHandler from '@/app/hooks/useHandler';
66

77
import VoteButton from './VoteButton';
@@ -16,7 +16,7 @@ interface Props {
1616

1717
const VoteForm = ({ votedTo, setVotedTo, setHasVote }: Props) => {
1818
const myUserId = useSelector(selectId);
19-
const invitationCode = useSelector(selectInvitationCode);
19+
const { invitationCode } = useSelector(selectUser);
2020
const opponents = Object.values(useSelector(selectPlayers)).filter((player: UserState) => player.id !== myUserId);
2121
if (!myUserId) throw new Error('UserID가 존재하지 않음');
2222
if (!invitationCode) throw new Error('초대 코드가 존재하지 않음');

src/app/hooks/useGameStartSync.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { setFinalVotes } from '@/redux/slices/votePhaseSlice';
99
import { resetQuestionPhase, setNominator, setNominee, setVotes } from '@/redux/slices/questionPhaseSlice';
1010
import isGameData from '@/validators/isGameData';
1111
import GameData from '@/types/GameData';
12-
import { LOCAL_STORAGE_ID, LOCAL_STORAGE_INVITATION_CODE } from '@/constants/localStorage';
1312

1413
export default function useGameStartSync() {
1514
const { invitationCode, id } = useSelector(selectUser);
@@ -21,10 +20,6 @@ export default function useGameStartSync() {
2120
const dispatch = useDispatch();
2221

2322
useEffect(() => {
24-
if (typeof window !== undefined) {
25-
localStorage.setItem(LOCAL_STORAGE_ID, id);
26-
localStorage.setItem(LOCAL_STORAGE_INVITATION_CODE, invitationCode);
27-
}
2823
const unsubscribe = onValue(gameRef, snapshot => {
2924
const currentData = snapshot.val() as GameData;
3025
if (isGameData(currentData)) {

src/app/hooks/useHandler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import GameData from '@/types/GameData';
1616
import Players from '@/types/Players';
1717
import createNewGame from '../../utils/createNewGame';
1818
import { cleanupGame } from '@/utils/cleanupGame';
19+
import { LOCAL_STORAGE_ID, LOCAL_STORAGE_INVITATION_CODE } from '@/constants/localStorage';
1920

2021
const useHandler = () => {
2122
if (!db) throw new Error();
@@ -47,6 +48,8 @@ const useHandler = () => {
4748
}),
4849
);
4950
dispatch(setIsGameMaster(true));
51+
localStorage.setItem(LOCAL_STORAGE_ID, id);
52+
localStorage.setItem(LOCAL_STORAGE_INVITATION_CODE, invitationCode);
5053
};
5154

5255
const dispatchUserDetails = (myUser: UserState) => {
@@ -70,6 +73,8 @@ const useHandler = () => {
7073
return;
7174
}
7275
dispatchUserDetails(myUser);
76+
localStorage.setItem(LOCAL_STORAGE_ID, myUser.id);
77+
localStorage.setItem(LOCAL_STORAGE_INVITATION_CODE, invitationCode);
7378
await update(userRef, myUser);
7479
};
7580

src/app/page.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Lobby from './Lobby/Lobby';
99
import Game from './Game/Game';
1010

1111
import { enterRoomByInvitationCode, selectUser, setUserId } from '../redux/slices/userSlice';
12-
import { selectInvitationCode } from '@/redux/slices/roomSlice';
12+
import { selectInvitationCode, setInvitationCode } from '@/redux/slices/roomSlice';
1313

1414
import db from '../../firebase/firebase.config';
1515
import { LOCAL_STORAGE_ID, LOCAL_STORAGE_INVITATION_CODE } from '@/constants/localStorage';
@@ -35,26 +35,44 @@ export default function Home() {
3535
const fetchGameFromFirebase = async (invitationCode: InvitationCode) => {
3636
const gameRef = ref(db, 'games/' + invitationCode);
3737
const snapshot = await get(gameRef);
38+
return snapshot.exists();
39+
};
3840

41+
const fetchRoomFromFirebase = async (invitationCode: InvitationCode) => {
42+
const roomRef = ref(db, 'rooms/' + invitationCode);
43+
const snapshot = await get(roomRef);
3944
return snapshot.exists();
4045
};
4146

42-
const handleGameData = (exists: boolean, storagedId: UserId, storagedInvitationCode: InvitationCode) => {
43-
if (exists) {
44-
dispatch(setUserId(storagedId));
47+
const handleStateRestoration = (
48+
gameExists: boolean,
49+
roomExists: boolean,
50+
storagedId: UserId,
51+
storagedInvitationCode: InvitationCode,
52+
) => {
53+
dispatch(setUserId(storagedId));
54+
if (gameExists) {
55+
// 게임 진행 중 상태 복원
4556
dispatch(enterRoomByInvitationCode(storagedInvitationCode));
57+
dispatch(setInvitationCode(null)); // 게임 상태에서는 room 코드를 null로 설정
58+
} else if (roomExists) {
59+
// 로비 상태 복원
60+
dispatch(setInvitationCode(storagedInvitationCode));
4661
} else {
62+
// 유효하지 않은 상태 (게임도 방도 없음)
4763
localStorage.removeItem(LOCAL_STORAGE_ID);
4864
localStorage.removeItem(LOCAL_STORAGE_INVITATION_CODE);
65+
dispatch(setUserId(null)); // 유저 상태도 초기화
4966
}
5067
};
5168

5269
const fetchData = async () => {
5370
const { storagedId, storagedInvitationCode } = getLocalStorageData();
5471

5572
if (storagedId && storagedInvitationCode) {
56-
const exists = await fetchGameFromFirebase(storagedInvitationCode);
57-
handleGameData(exists, storagedId, storagedInvitationCode);
73+
const gameExists = await fetchGameFromFirebase(storagedInvitationCode);
74+
const roomExists = await fetchRoomFromFirebase(storagedInvitationCode);
75+
handleStateRestoration(gameExists, roomExists, storagedId, storagedInvitationCode);
5876
}
5977
};
6078

0 commit comments

Comments
 (0)