Skip to content

Commit

Permalink
feat: add error catch into some points
Browse files Browse the repository at this point in the history
  • Loading branch information
iaurg committed Sep 6, 2023
1 parent 7b872bb commit 57d2c0f
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 6 deletions.
38 changes: 38 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.44.3",
"react-hot-toast": "^2.4.1",
"react-konva": "^18.2.9",
"react-popper": "^2.3.0",
"socket.io-client": "^4.6.2",
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/(private)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Chat from "@/components/Chat";
import Header from "@/components/Header";
import Providers from "../login/providers";
import { ChatProvider } from "@/contexts/ChatContext";
import { Toaster } from "react-hot-toast";

export const metadata: Metadata = {
title: "Game | 42 Transcendence",
Expand All @@ -21,6 +22,7 @@ export default function RootPrivateLayout({
return (
<html lang="en">
<body className="bg-black42-100 flex">
<Toaster />
<Providers>
<div className="flex flex-col md:flex-row">
<Sidebar />
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/app/error.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client"; // Error components must be Client Components

import { useEffect } from "react";

export default function Error({
error,
reset,
}: {
error: Error;
reset: () => void;
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error);
}, [error]);

return (
<div>
<h2>Something went wrong!</h2>
<button
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
);
}
2 changes: 2 additions & 0 deletions frontend/src/app/login/auth/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "react-hot-toast";

// TODO: refactor states and input to be a client component, and use the client component here, also remove use client from top

Expand All @@ -22,6 +23,7 @@ export default function AuthPage() {

const handleSubmit = () => {
// Handle submission logic here
toast.success("Código validado com sucesso!");
console.log("Submitted code:", code);
router.push("/game");
};
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/login/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata } from "next";
import Providers from "./providers";
import { Toaster } from "react-hot-toast";

export const metadata: Metadata = {
title: "42 Transcendence",
Expand All @@ -14,6 +15,7 @@ export default function RootLoginPublicLayout({
return (
<html lang="en">
<body>
<Toaster />
<Providers>{children}</Providers>
</body>
</html>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Image from "next/image";
import { Toaster } from "react-hot-toast";

export default function Page() {
return (
<div className="max-w-screen-xl m-auto">
<Toaster />
<div className="flex flex-col h-screen md:flex-row">
<div className="flex flex-col md:flex-1">
<div className="flex flex-col flex-1 justify-center items-center md:items-start mx-4">
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/contexts/GameContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export type MovePlayerData = {
direction: string;
};

export type GameLayout = 'default' | 'sunlight' | 'moonlight' | 'dark';
export type GameLayout = "default" | "sunlight" | "moonlight" | "dark";

type GameContextType = {
waitingPlayer2: boolean;
Expand All @@ -79,7 +79,7 @@ export const GameProvider = ({ children }: GameProviderProps) => {
const [clientId, setClientId] = useState("");
const [gameData, setGameData] = useState({} as GameData);
const [gameFinishedData, setGameFinishedData] = useState({} as GameData);
const [gameLayout, setGameLayout] = useState<GameLayout>('default');
const [gameLayout, setGameLayout] = useState<GameLayout>("default");

const socket = useRef<Socket | null>(null);

Expand Down Expand Up @@ -180,8 +180,8 @@ export const GameProvider = ({ children }: GameProviderProps) => {
gameAbandoned,
gameFinishedData,
gameData,
gameLayout,
setGameLayout
gameLayout,
setGameLayout,
}}
>
{children}
Expand Down
13 changes: 11 additions & 2 deletions frontend/src/services/queryClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { QueryClient } from "@tanstack/react-query";
import { QueryCache, QueryClient } from "@tanstack/react-query";
import toast from "react-hot-toast";

export const queryClient = new QueryClient();
export const queryClient = new QueryClient({
queryCache: new QueryCache({
onError: (error: any, query) => {
if (query.state.data !== undefined) {
toast.error(`Something went wrong: ${error.message}`);
}
},
}),
});

0 comments on commit 57d2c0f

Please sign in to comment.