Skip to content

[experimental] Use useEffect and make problem pages client side #119

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
92 changes: 54 additions & 38 deletions app/[orgId]/contests/[id]/problems/[problemId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
"use client";

import { CodeEditor } from "@/components/code-editor";
import { notFound } from "next/navigation";
import { useEffect, useState } from "react";

async function getProblem(orgId: string, contestId: string, problemId: string) {
console.log(
`ENV: ${process.env.NEXT_PUBLIC_APP_URL}`,
orgId,
contestId,
problemId,
);

try {
// Fetch the problem with contest context
const response = await fetch(
`${process.env.NEXT_PUBLIC_APP_URL}/api/orgs/${orgId}/contests/${contestId}/problems/${problemId}`,
{
cache: "no-store",
},
);

if (!response.ok) {
throw new Error("Failed to fetch problem");
}

const problem = await response.json();

// Add the contest ID to the problem data
return {
...problem,
contestNameId: contestId,
orgId,
};
} catch (error) {
console.error("Error fetching problem:", error);
return null;
}
}

export default async function Page({
export default function Page({
params,
}: {
params: { orgId: string; id: string; problemId: string };
}) {
const problem = await getProblem(params.orgId, params.id, params.problemId);
const [problem, setProblem] = useState(null);

useEffect(() => {
async function getProblem(
orgId: string,
contestId: string,
problemId: string,
) {
console.log(
`ENV: ${process.env.NEXT_PUBLIC_APP_URL}`,
orgId,
contestId,
problemId,
);

try {
// Fetch the problem with contest context
const response = await fetch(
`/api/orgs/${orgId}/contests/${contestId}/problems/${problemId}`,
{
cache: "no-store",
},
);

if (!response.ok) {
throw new Error("Failed to fetch problem");
}

const problem = await response.json();

// Add the contest ID to the problem data
return {
...problem,
contestNameId: contestId,
orgId,
};
} catch (error) {
console.error("Error fetching problem:", error);
return null;
}
}

getProblem(params.orgId, params.id, params.problemId).then((result) => {
if (!result) {
notFound();
}
setProblem(result);
});
}, [params.orgId, params.id, params.problemId]);

if (!problem) {
notFound();
return null;
}

return (
Expand Down
78 changes: 45 additions & 33 deletions app/[orgId]/problems/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
"use client";

import { CodeEditor } from "@/components/code-editor";
import { notFound } from "next/navigation";
import { useEffect, useState } from "react";
// import { getProblemIdFromCode } from "@/app/api/orgs/[orgId]/problems/service";

async function getProblem(orgId: string, problemId: string) {
console.log(`ENV: ${process.env.NEXT_PUBLIC_APP_URL}`, orgId, problemId);

try {
// const problemIdNumber = await getProblemIdFromCode(orgId, problemId);

const response = await fetch(
`${process.env.NEXT_PUBLIC_APP_URL}/api/orgs/${orgId}/problems/${problemId}`,
{
cache: "no-store",
},
);

if (!response.ok) {
throw new Error("Failed to fetch problem");
}

// Return the problem without adding contestId
const problem = await response.json();
return {
...problem,
orgId,
// No contestId here, as this is a standalone problem
};
} catch (error) {
console.error("Error fetching problem:", error);
return null;
}
}

export default async function Page({
export default function Page({
params,
}: {
params: { orgId: string; id: string };
}) {
const problem = await getProblem(params.orgId, params.id);
const [problem, setProblem] = useState(null);

useEffect(() => {
async function getProblem(orgId: string, problemId: string) {
console.log(`ENV: ${process.env.NEXT_PUBLIC_APP_URL}`, orgId, problemId);

try {
// const problemIdNumber = await getProblemIdFromCode(orgId, problemId);

const response = await fetch(
`/api/orgs/${orgId}/problems/${problemId}`,
{
cache: "no-store",
},
);

if (!response.ok) {
throw new Error("Failed to fetch problem");
}

// Return the problem without adding contestId
const problem = await response.json();
return {
...problem,
orgId,
// No contestId here, as this is a standalone problem
};
} catch (error) {
console.error("Error fetching problem:", error);
return null;
}
}

getProblem(params.orgId, params.id).then((result) => {
if (!result) {
notFound();
}
setProblem(result);
});
}, [params.orgId, params.id]);

if (!problem) {
notFound();
return null;
}

return (
Expand Down