Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ably api #4

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/nextjs/app/api/ably/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { NextResponse } from "next/server";

export const GET = async () => {
return new NextResponse(JSON.stringify(process.env.ABLY_API_KEY), { status: 200 });
};
12 changes: 10 additions & 2 deletions packages/nextjs/app/game/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import { useAccount } from "wagmi";
import doodleConfig from "~~/doodle.config";
import useGameData from "~~/hooks/doodleExchange/useGameData";
import { Game, Player as playerType } from "~~/types/game/game";
import { joinGame, updateGameRound, updatePlayerRound } from "~~/utils/doodleExchange/api/apiUtils";
import { fetchAblyApiKey, joinGame, updateGameRound, updatePlayerRound } from "~~/utils/doodleExchange/api/apiUtils";
import { notification } from "~~/utils/scaffold-eth";

const GamePage = () => {
const ablyApiKey = process.env.NEXT_PUBLIC_ABLY_API_KEY || doodleConfig.ably_api_key;
const { id } = useParams();
const { loadGameState, updateGameState, updatePlayerState } = useGameData();
const { address: connectedAddress } = useAccount();
Expand All @@ -25,10 +24,19 @@ const GamePage = () => {
const [game, setGame] = useState<Game>();
const [player, setPlayer] = useState<playerType>();
const [token, setToken] = useState("");
const [ablyApiKey, setAblyApiKey] = useState("");

const [isUpdatingRound, setIsUpdatingRound] = useState(false);
const [countdown, setCountdown] = useState(20);

useEffect(() => {
fetchAblyApiKey().then((key: string) => {
const ablyApiKey = key || doodleConfig.ably_api_key;
setAblyApiKey(ablyApiKey);
console.log("ablyApiKey", ablyApiKey);
});
}, []);

useEffect(() => {
const loadGame = async () => {
const game = loadGameState();
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;

// Routes that don't require authentication
const publicRoutes = ["/api/host/create", "/api/player/join"];
const publicRoutes = ["/api/host/create", "/api/player/join", "/api/ably"];

if (publicRoutes.includes(pathname)) {
return NextResponse.next();
Expand Down
12 changes: 12 additions & 0 deletions packages/nextjs/utils/doodleExchange/api/apiUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { saveGameState } from "../game";
import { notification } from "~~/utils/scaffold-eth";

export const fetchAblyApiKey = async () => {
const response = await fetch("/api/ably", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});

const ablyApiKey = await response.json();
return ablyApiKey;
};

export const joinGame = async (invite: string, address: string) => {
const response = await fetch("/api/player/join", {
method: "PATCH",
Expand Down
Loading