Skip to content

Commit

Permalink
feat: 結果をAPIにポストするように変更
Browse files Browse the repository at this point in the history
Co-authored-by: KinjiKawaguchi <[email protected]>
  • Loading branch information
KikyoNanakusa and KinjiKawaguchi committed Apr 6, 2024
1 parent 0a53533 commit b148d69
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion typing-app/.env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
API_URL=http://localhost:8080
NEXT_PUBLIC_API_URL=http://localhost:8080
36 changes: 16 additions & 20 deletions typing-app/src/components/templates/GameTyping.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,7 +14,7 @@ import gaugeTimeImg from "../../../public/img/gauge_time.png";
const GameTyping: React.FC<GameTypingProps> = ({ nextPage, subjectText, setResultScore }) => {
const [startedAt, setStartedAt] = useState(new Date());

const totalSeconds = 60;
const totalSeconds = 3;
const [count, setCount] = useState(totalSeconds);
const damyUserId = "damyId";

Expand All @@ -22,7 +23,7 @@ const GameTyping: React.FC<GameTypingProps> = ({ 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;
Expand All @@ -37,24 +38,19 @@ const GameTyping: React.FC<GameTypingProps> = ({ 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]);

Expand Down
2 changes: 1 addition & 1 deletion typing-app/src/libs/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createClient from "openapi-fetch";
import { paths } from "./v1";

export const client = createClient<paths>({ baseUrl: process.env.API_URL });
export const client = createClient<paths>({ baseUrl: process.env.NEXT_PUBLIC_API_URL });

0 comments on commit b148d69

Please sign in to comment.