From b148d69133117edb6be27e5adf516809e5336351 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sat, 6 Apr 2024 22:47:12 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20=E7=B5=90=E6=9E=9C=E3=82=92API?= =?UTF-8?q?=E3=81=AB=E3=83=9D=E3=82=B9=E3=83=88=E3=81=99=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: KinjiKawaguchi --- typing-app/.env | 2 +- .../src/components/templates/GameTyping.tsx | 36 +++++++++---------- typing-app/src/libs/api/index.ts | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/typing-app/.env b/typing-app/.env index 15f9205..cc53660 100644 --- a/typing-app/.env +++ b/typing-app/.env @@ -1 +1 @@ -API_URL=http://localhost:8080 \ No newline at end of file +NEXT_PUBLIC_API_URL=http://localhost:8080 \ No newline at end of file diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index 244bd8b..c3e4944 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -1,6 +1,7 @@ import RegisterScore, { ResultScore } from "@/types/RegisterScore"; import { Box } from "@chakra-ui/react"; import Image from "next/image"; +import { client } from "@/libs/api"; import React, { useCallback, useEffect, useRef, useState } from "react"; import ProgressBar from "../atoms/ProgressBar"; import { GameTypingProps } from "../pages/Game"; @@ -13,7 +14,7 @@ import gaugeTimeImg from "../../../public/img/gauge_time.png"; const GameTyping: React.FC = ({ nextPage, subjectText, setResultScore }) => { const [startedAt, setStartedAt] = useState(new Date()); - const totalSeconds = 60; + const totalSeconds = 3; const [count, setCount] = useState(totalSeconds); const damyUserId = "damyId"; @@ -22,7 +23,7 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul const [incorrectType, setIncorrectType] = useState(0); // 誤打数 // スコアデータを送信する - const sendResultData = useCallback(() => { + const sendResultData = useCallback(async () => { // サーバに送信されるデータ const endedAt = new Date(); const actualTypeTimeSeconds = (endedAt.valueOf() - startedAt.valueOf()) / 1000; @@ -37,24 +38,19 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul endedAt: endedAt, }; - // リザルト画面用のデータ - setResultScore({ - keystrokes: registeredScore.keystrokes, - miss: incorrectType, - time: new Date(typeTimeSeconds * 1000), - wpm: (correctType / typeTimeSeconds) * 60, - accuracy: registeredScore.accuracy, - score: registeredScore.score, - }); - fetch(`http://localhost:8080/users/${userId}/scores`, { - method: `POST`, - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(registeredScore), - }).catch((error) => { - console.error(error); - }); + + const { data, error } = await client.POST("/scores", { body: { user_id: "ea450409-14de-463a-847e-bd3e8a6313c8", keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy}}); + if(!error){ + // リザルト画面用のデータ + setResultScore({ + keystrokes: registeredScore.keystrokes, + miss: incorrectType, + time: new Date(typeTimeSeconds * 1000), + wpm: (correctType / typeTimeSeconds) * 60, + accuracy: registeredScore.accuracy, + score: registeredScore.score, + }); + } nextPage(); }, [startedAt, totalSeconds, correctType, incorrectType, setResultScore, userId, nextPage]); diff --git a/typing-app/src/libs/api/index.ts b/typing-app/src/libs/api/index.ts index 1ff62cd..d4cc0a9 100644 --- a/typing-app/src/libs/api/index.ts +++ b/typing-app/src/libs/api/index.ts @@ -1,4 +1,4 @@ import createClient from "openapi-fetch"; import { paths } from "./v1"; -export const client = createClient({ baseUrl: process.env.API_URL }); +export const client = createClient({ baseUrl: process.env.NEXT_PUBLIC_API_URL }); From a622ed2d8938a2489a3d769eb9acaefd57d6aaad Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sun, 7 Apr 2024 00:34:17 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat:=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=81=84=E3=82=8B=E3=83=A6=E3=83=BC=E3=82=B6?= =?UTF-8?q?=E3=83=BC=E3=81=AEid=E3=82=92=E5=8F=96=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/templates/GameTyping.tsx | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index c3e4944..41977f7 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -10,15 +10,15 @@ import styles from "./GameTyping.module.css"; import gaugePositionImg from "../../../public/img/gauge_position.png"; import gaugeSpeedImg from "../../../public/img/gauge_speed.png"; import gaugeTimeImg from "../../../public/img/gauge_time.png"; +import { User } from "@/types/user"; +import { getCurrentUser } from "@/app/actions"; const GameTyping: React.FC = ({ nextPage, subjectText, setResultScore }) => { const [startedAt, setStartedAt] = useState(new Date()); - const totalSeconds = 3; + const totalSeconds = 60; const [count, setCount] = useState(totalSeconds); - const damyUserId = "damyId"; - const userId = damyUserId; // ToDo: 要変更 const [correctType, setCorrectType] = useState(0); // 正打数 const [incorrectType, setIncorrectType] = useState(0); // 誤打数 @@ -38,8 +38,13 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul endedAt: endedAt, }; + const user: User | undefined = await getCurrentUser(); + //TODO:Userが取得できなかった場合のエラーハンドリングを追加 + if (!user) { + return; + } - const { data, error } = await client.POST("/scores", { body: { user_id: "ea450409-14de-463a-847e-bd3e8a6313c8", keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy}}); + const { data, error } = await client.POST("/scores", { body: { user_id: user?.id, keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy}}); if(!error){ // リザルト画面用のデータ setResultScore({ @@ -51,8 +56,9 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul score: registeredScore.score, }); } + nextPage(); - }, [startedAt, totalSeconds, correctType, incorrectType, setResultScore, userId, nextPage]); + }, [startedAt, totalSeconds, correctType, incorrectType, setResultScore, nextPage]); const [typeIndex, setTypeIndex] = useState(0); // 残り時間のカウントダウン @@ -68,14 +74,14 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul }, updateFrequency); return () => clearInterval(timer); } - }, [count, nextPage, sendResultData, startedAt, userId]); // ビルド時の警告防止のためにuserIdも依存リストに追加 + }, [count, nextPage, sendResultData, startedAt]); // 打ち終わった時にスコアを送信する useEffect(() => { if (typeIndex === subjectText.length - 1) { sendResultData(); } - }, [nextPage, userId, sendResultData, subjectText.length, typeIndex]); // ビルド時の警告防止のためにuserIdも依存リストに追加 + }, [nextPage, sendResultData, subjectText.length, typeIndex]); // ビルド時の警告防止のためにuserIdも依存リストに追加 // タイピング速度計算用 const typingQueueListSize = 5; // ここで瞬間タイピング速度計算の粒度を決める 増やすほど変化が穏やかになる From 7d71c49b06f0b3ecffffa3f4a56fe7f822844738 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sun, 7 Apr 2024 00:35:45 +0900 Subject: [PATCH 3/6] =?UTF-8?q?style:prettier=E3=82=92=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/templates/GameTyping.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index 41977f7..1d99194 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -44,8 +44,10 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul return; } - const { data, error } = await client.POST("/scores", { body: { user_id: user?.id, keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy}}); - if(!error){ + const { data, error } = await client.POST("/scores", { + body: { user_id: user?.id, keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy }, + }); + if (!error) { // リザルト画面用のデータ setResultScore({ keystrokes: registeredScore.keystrokes, @@ -74,7 +76,7 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul }, updateFrequency); return () => clearInterval(timer); } - }, [count, nextPage, sendResultData, startedAt]); + }, [count, nextPage, sendResultData, startedAt]); // 打ち終わった時にスコアを送信する useEffect(() => { From 9d2845b7667a7a8484fbf4d625fa1f9fc3b80a5e Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sun, 7 Apr 2024 01:05:37 +0900 Subject: [PATCH 4/6] =?UTF-8?q?chore:post=E3=81=99=E3=82=8B=E9=9A=9B?= =?UTF-8?q?=E3=81=AB=E4=BD=BF=E3=81=86user=E3=82=92nullable=E3=81=A7?= =?UTF-8?q?=E7=84=A1=E3=81=8F=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/templates/GameTyping.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index 1d99194..69494d7 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -45,7 +45,7 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul } const { data, error } = await client.POST("/scores", { - body: { user_id: user?.id, keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy }, + body: { user_id: user.id, keystrokes: registeredScore.keystrokes, accuracy: registeredScore.accuracy }, }); if (!error) { // リザルト画面用のデータ From a76ac2e0d5afcbc34ec87c961d946b4a369cbb8b Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sun, 7 Apr 2024 01:08:43 +0900 Subject: [PATCH 5/6] =?UTF-8?q?chore:=E4=B8=8D=E8=A6=81=E3=81=AA=E3=82=B3?= =?UTF-8?q?=E3=83=A1=E3=83=B3=E3=83=88=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/templates/GameTyping.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index 69494d7..151c77b 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -83,7 +83,7 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul if (typeIndex === subjectText.length - 1) { sendResultData(); } - }, [nextPage, sendResultData, subjectText.length, typeIndex]); // ビルド時の警告防止のためにuserIdも依存リストに追加 + }, [nextPage, sendResultData, subjectText.length, typeIndex]); // タイピング速度計算用 const typingQueueListSize = 5; // ここで瞬間タイピング速度計算の粒度を決める 増やすほど変化が穏やかになる From a5361ccf8e46a5775a998ff03e706220c7e061a4 Mon Sep 17 00:00:00 2001 From: KikyoNanakusa Date: Sun, 7 Apr 2024 01:39:05 +0900 Subject: [PATCH 6/6] =?UTF-8?q?style:prettier=E3=82=92=E9=81=A9=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- typing-app/src/components/templates/GameTyping.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typing-app/src/components/templates/GameTyping.tsx b/typing-app/src/components/templates/GameTyping.tsx index 151c77b..143f23d 100644 --- a/typing-app/src/components/templates/GameTyping.tsx +++ b/typing-app/src/components/templates/GameTyping.tsx @@ -83,7 +83,7 @@ const GameTyping: React.FC = ({ nextPage, subjectText, setResul if (typeIndex === subjectText.length - 1) { sendResultData(); } - }, [nextPage, sendResultData, subjectText.length, typeIndex]); + }, [nextPage, sendResultData, subjectText.length, typeIndex]); // タイピング速度計算用 const typingQueueListSize = 5; // ここで瞬間タイピング速度計算の粒度を決める 増やすほど変化が穏やかになる