Skip to content

Commit

Permalink
Merge branch develop into feature/#3
Browse files Browse the repository at this point in the history
  • Loading branch information
suehub committed Nov 14, 2023
2 parents 0ad35db + 4ce35d6 commit 485669c
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 30 deletions.
65 changes: 46 additions & 19 deletions src/components/Game/Vote/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useEffect, useState } from "react";
import {
Modal,
ModalOverlay,
Expand All @@ -12,8 +12,9 @@ import {
RadioGroup,
Stack,
} from "@chakra-ui/react";
import useFireFetch from "../../../hooks/useFireFetch";
import { arrayUnion } from "firebase/firestore";
// import useFireFetch from "../../../hooks/useFireFetch";
import { db } from "../../../firebase/firebase";
import { arrayUnion, updateDoc, doc, getDoc } from "firebase/firestore";
import calculateVote from "./CalculateVote";
import { useRecoilValue } from "recoil";
import { userState } from "../../../recoil/atoms/userState";
Expand All @@ -35,28 +36,53 @@ const Vote: React.FC<VoteProps> = ({
gameData,
}): React.ReactElement => {
const [selectedUser, setSelectedUser] = useState<string>("");
const fireFetch = useFireFetch();
const fetchData = fireFetch.useGetSome("game", "id", gameData.id)
.data[0] as GameData;
const [fetchData, setFetchData] = useState<GameData | null>(null);
// const fireFetch = useFireFetch();
useEffect(() => {
const fetchDataFromFirebase = async () => {
try {
const docRef = doc(db, "game", gameData.id);
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
const updatedData = docSnap.data() as GameData;
console.log("updatedData :", updatedData);
setFetchData(updatedData);
}
} catch (error) {
console.error("Error fetching data:", error);
}
};

fetchDataFromFirebase();
}, []);

const user = useRecoilValue(userState);

const handleUserChange = (value: string) => {
setSelectedUser(value);
};

const handleVoteSubmit = () => {
const handleVoteSubmit = async () => {
if (selectedUser !== "") {
fireFetch.updateData("game", gameData.id, {
votedFor: arrayUnion({ by: user.id, liar: selectedUser }),
});
try {
const docRef = doc(db, "game", gameData.id);
await updateDoc(docRef, {
votedFor: arrayUnion({ by: user.id, liar: selectedUser }),
});

const updatedDocSnap = await getDoc(docRef);
const updatedData = updatedDocSnap.data() as GameData;

console.log("fetchData", fetchData);
const voteResult = calculateVote(fetchData);
console.log("submit/ Updated Data:", updatedData);

console.log("Vote result: " + voteResult);
const voteResult = calculateVote(updatedData);
console.log("Vote result: " + voteResult);

onClose(selectedUser);
onClose(selectedUser);
} catch (error) {
console.error("Error updating data:", error);
}
}
};

Expand All @@ -70,11 +96,12 @@ const Vote: React.FC<VoteProps> = ({
<ModalBody>
<RadioGroup value={selectedUser} onChange={handleUserChange}>
<Stack spacing={2}>
{gameData.users.map((user) => (
<Radio key={user} value={user}>
{user}
</Radio>
))}
{fetchData &&
fetchData.users.map((user) => (
<Radio key={user} value={user}>
{user}
</Radio>
))}
</Stack>
</RadioGroup>
</ModalBody>
Expand Down
7 changes: 3 additions & 4 deletions src/components/Login/SignUpModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ const SignUpModal = ({ isOpen, onClose }: SignUpModalProps) => {
// ID 중복 검사
const checkIdDuplication = async (id: string): Promise<boolean> => {
try {
const response = await axios.post(
"https://fastcampus-chat.net/check/id",
{ id },
);
const response = await axios.post("https://fastcampus-chat.net/auth/id", {
id,
});
return response.data.isDuplicated;
} catch (error) {
console.error("Error checking ID duplication", error);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Main/CreateGameModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ const CreateGameModal = ({ setModal, socket }: Props) => {
// 파이어베이스 게임 데이터 생성
const newData = {
...roomData,
users: [...roomData.users, user.id],
users: [user.id],
id: createGame.result.id,
host: user.id,
createdAt: serverTimestamp(),
Expand Down
Loading

0 comments on commit 485669c

Please sign in to comment.