Skip to content

Commit

Permalink
로그인 후 bingo 판 생성하고 /bingo로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
4roring committed Nov 22, 2024
1 parent 4675a7e commit 6407b7c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 34 deletions.
49 changes: 32 additions & 17 deletions src/modules/Bingo/BingoContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState, useEffect } from "react";
import BingoPresenter from "./BingoPresenter";
import { useLocation } from 'react-router-dom';
import { useLocation } from "react-router-dom";
import {
getBingoBoard,
getSelectedWords,
Expand All @@ -10,7 +10,7 @@ import {
singUpUser,
createUserBingoInteraction,
getUserLatestInteraction,
getUserName
getUserName,
} from "../../api/bingo_api.ts";
import {
defafultBingoBoard,
Expand All @@ -25,10 +25,9 @@ const useInput = (initialValue: string) => {
return { value, onChange };
};

const BingoContainer = () => {
const BingoContainer = () => {
const location = useLocation();
if (location.search === "?logout")
{
if (location.search === "?logout") {
localStorage.setItem("myWordList", "");
localStorage.setItem("recentWords", "");
localStorage.setItem("recentSendUser", "");
Expand All @@ -49,8 +48,12 @@ const BingoContainer = () => {
{ value: string; status: number }[]
>([]);
const [opponentID, setOpponentID] = useState("");
const [recentWords, setRecentWords] = useState(localStorage.getItem("recentWords") || "");
const [recentSendUser, setRecentSendUser] = useState(localStorage.getItem("recentSendUser") || "");
const [recentWords, setRecentWords] = useState(
localStorage.getItem("recentWords") || ""
);
const [recentSendUser, setRecentSendUser] = useState(
localStorage.getItem("recentSendUser") || ""
);
const MyID = useInput(localStorage.getItem("myID") || "");
const [userSelectedWords, setUserSelectedWords] = useState<string[]>([]);
const initBingoBoard = async () => {
Expand All @@ -61,24 +64,35 @@ const BingoContainer = () => {
bingoBoard.forEach((item, index) => {
return (boardData[index] = {
value: item.value,
status: [myWord1, myWord2, myWord3, myWord4].includes(item.value) ? 1 : 0,
selected: [myWord1, myWord2, myWord3, myWord4].includes(item.value) ? 1 : 0,
status: [myWord1, myWord2, myWord3, myWord4].includes(item.value)
? 1
: 0,
selected: [myWord1, myWord2, myWord3, myWord4].includes(item.value)
? 1
: 0,
});
});
localStorage.setItem("myWordList", [myWord1, myWord2, myWord3, myWord4].join("|"));
localStorage.setItem(
"myWordList",
[myWord1, myWord2, myWord3, myWord4].join("|")
);

if (MyID.value != "") {
const result = await singUpUser(MyID.value);
if (result === false && !confirm("이미 누군가 사용중인 계정입니다. 정말 로그인하시겠습니까?") && !confirm("정말 로그인하시겠습니까???"))
{
if (
result === false &&
!confirm("이미 누군가 사용중인 계정입니다. 정말 로그인하시겠습니까?") &&
!confirm("정말 로그인하시겠습니까???")
) {
localStorage.setItem("myWordList", "");
localStorage.setItem("recentWords", "");
localStorage.setItem("recentSendUser", "");
localStorage.setItem("myID", "");
return
return;
}

const user = await getUser(MyID.value);

await createBingoBoard(user.user_id, boardData);
}
};
Expand All @@ -88,8 +102,10 @@ const BingoContainer = () => {
const userLatestInteraction = await getUserLatestInteraction(user.user_id);

if (userLatestInteraction) {
const sendUserName = await getUserName(userLatestInteraction.send_user_id);
const wordList = userLatestInteraction.word_id_list
const sendUserName = await getUserName(
userLatestInteraction.send_user_id
);
const wordList = userLatestInteraction.word_id_list;
localStorage.setItem("recentWords", wordList);
localStorage.setItem("recentSendUser", sendUserName);
setRecentWords(wordList);
Expand Down Expand Up @@ -121,8 +137,7 @@ const BingoContainer = () => {
useEffect(() => {
const fetchData = async () => {
const user = await getUser(MyID.value);
if (user.user_id === null)
return
if (user.user_id === null) return;
const fetchedBingoWords = await getBingoBoard(user.user_id);
const fetchedSelectedWords = await getSelectedWords(user.user_id);
setBingoWords(fetchedBingoWords);
Expand Down
28 changes: 13 additions & 15 deletions src/modules/Bingo/BingoPresenter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ import SqaureTextBox from "./components/SqaureTextBox.tsx";
import SelectBox from "./components/SelectBox.tsx";
import { defafultBingoBoard } from "./components/DefaultBingoBoard.ts";

const careerOptionsArray = defafultBingoBoard.slice(0, 4);
const careerOptions = careerOptionsArray.map((data) => data.value);

const careerOptionsArray = defafultBingoBoard.slice(0, 4)
const careerOptions = careerOptionsArray.map(data => data.value)
const positionOptionArray = defafultBingoBoard.slice(4, 10);
const positionOption = positionOptionArray.map((data) => data.value);

const positionOptionArray = defafultBingoBoard.slice(4, 10)
const positionOption = positionOptionArray.map(data => data.value)

const pseudolabOptionArray = defafultBingoBoard.slice(10, 14)
const pseudolabOption = pseudolabOptionArray.map(data => data.value)

const talkOptionArray = defafultBingoBoard.slice(14, 20)
const talkOption = talkOptionArray.map(data => data.value)
const pseudolabOptionArray = defafultBingoBoard.slice(10, 14);
const pseudolabOption = pseudolabOptionArray.map((data) => data.value);

const talkOptionArray = defafultBingoBoard.slice(14, 20);
const talkOption = talkOptionArray.map((data) => data.value);

const Wrapper = styled(Container)({
width: "60vw",
Expand Down Expand Up @@ -92,7 +90,7 @@ type BingoPresenterProps = {
const BingoPresenter = (props: BingoPresenterProps) => {
return (
<>
{props.userSelectedWords.length === 0 ? (
{props.myID.length === 0 ? (
<Wrapper>
<InputBox
placeholder="나의 닉네임을 입력"
Expand Down Expand Up @@ -167,15 +165,15 @@ const BingoPresenter = (props: BingoPresenterProps) => {
draggable // 드래그 가능
pauseOnHover // 마우스를 올리면 알람 정지
theme="light"
// limit={1} // 알람 개수 제한
// limit={1} // 알람 개수 제한
/>
</Wrapper>
) : (
<Wrapper>
<MyInfo>My Id: {props.myID}</MyInfo>
{props.userSelectedWords.map((word) => (
{/* {props.userSelectedWords.map((word) => (
<LongTextBox key={word} text={word} />
))}
))} */}
<RecentSendUserWrapper>
{props.recentSendUser === "" ? (
""
Expand Down Expand Up @@ -224,7 +222,7 @@ const BingoPresenter = (props: BingoPresenterProps) => {
draggable // 드래그 가능
pauseOnHover // 마우스를 올리면 알람 정지
theme="light"
// limit={1} // 알람 개수 제한
// limit={1} // 알람 개수 제한
/>
<Button onClick={props.onRefreshBingoWords}>내 빙고판 갱신</Button>
</FormControl>
Expand Down
18 changes: 16 additions & 2 deletions src/modules/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { toast, ToastContainer } from "react-toastify";
import { styled } from "@mui/system";
import { useState } from "react";
import { SHA256 } from "crypto-js";
import { singUpUser } from "../../api/bingo_api";
import {
singUpUser,
createBingoBoard,
getBingoBoard,
} from "../../api/bingo_api";

const StyledContainer = styled(Container)({
textAlign: "center",
Expand All @@ -21,7 +25,7 @@ const Home = () => {
console.log(loginId);
const hash_password = SHA256(password).toString();

const result = singUpUser(loginId, hash_password);
const result = await singUpUser(loginId, hash_password);
if (result.ok === false) {
toast.error(result.message);
localStorage.setItem("myWordList", "");
Expand All @@ -30,6 +34,16 @@ const Home = () => {
localStorage.setItem("myID", "");
}
localStorage.setItem("myID", loginId);

const bingoData = await getBingoBoard(loginId);
if (bingoData.length == 0) {
const boardData: {
[key: string]: { value: string; status: number; selected: number };
} = {};
await createBingoBoard(result.user_id, boardData);
}

window.location.href = "/bingo";
};

return (
Expand Down

0 comments on commit 6407b7c

Please sign in to comment.