Skip to content

Commit

Permalink
Merge pull request #60 from codegasms/feat/problems-frontend-integration
Browse files Browse the repository at this point in the history
Problems frontend integration
  • Loading branch information
virinci authored Dec 5, 2024
2 parents ee534f4 + 8416abf commit 79d0efd
Show file tree
Hide file tree
Showing 5 changed files with 1,175 additions and 31 deletions.
64 changes: 33 additions & 31 deletions app/[orgId]/problems/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function ProblemsPage({
}) {
const [problems, setProblems] = useState<Problem[]>([]);
const [selectedProblem, setSelectedProblem] = useState<Problem | null>(null);
// const [isEditorOpen, setIsEditorOpen] = useState(false);
const [isEditorOpen, setIsEditorOpen] = useState(false);

const { toast } = useToast();

Expand All @@ -29,7 +29,7 @@ export default function ProblemsPage({
const response = await fetch(`/api/orgs/${params.orgId}/problems`);
if (!response.ok) throw new Error("Failed to fetch problems");
const data = await response.json();
setProblems(data);
setProblems(data && data.length ? data : mockProblems);
} catch (error) {
console.error("Error fetching problems:", error);
setProblems(mockProblems);
Expand All @@ -38,10 +38,10 @@ export default function ProblemsPage({
fetchProblems();
}, [params.orgId]);

// const handleAdd = () => {
// setSelectedProblem(null);
// setIsEditorOpen(true);
// };
const handleAdd = () => {
setSelectedProblem(null);
setIsEditorOpen(true);
};

// const handleEdit = (problem: Problem) => {
// setSelectedProblem(problem);
Expand All @@ -63,33 +63,35 @@ export default function ProblemsPage({
}
};

// const handleSave = async (problem: Problem) => {
// try {
// const url = selectedProblem
// ? `/api/orgs/${params.orgId}/problems/${selectedProblem.id}`
// : `/api/orgs/${params.orgId}/problems`;
/*
const handleSave = async (problem: Problem) => {
try {
const url = selectedProblem
? `/api/orgs/${params.orgId}/problems/${selectedProblem.id}`
: `/api/orgs/${params.orgId}/problems`;
// const response = await fetch(url, {
// method: selectedProblem ? "PATCH" : "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify({ ...problem, orgId: parseInt(params.orgId) }),
// });
const response = await fetch(url, {
method: selectedProblem ? "PATCH" : "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...problem, orgId: parseInt(params.orgId) }),
});
// if (!response.ok) throw new Error("Failed to save problem");
// const savedProblem = await response.json();
if (!response.ok) throw new Error("Failed to save problem");
const savedProblem = await response.json();
// setProblems((prev) => {
// if (selectedProblem) {
// return prev.map((p) =>
// p.id === selectedProblem.id ? savedProblem : p,
// );
// }
// return [...prev, savedProblem];
// });
// } catch (error) {
// console.error("Error saving problem:", error);
// }
// };
setProblems((prev) => {
if (selectedProblem) {
return prev.map((p) =>
p.id === selectedProblem.id ? savedProblem : p,
);
}
return [...prev, savedProblem];
});
} catch (error) {
console.error("Error saving problem:", error);
}
};
*/

// const handleSavelocal = (updatedProblem: Problem) => {
// setProblems(
Expand All @@ -109,7 +111,7 @@ export default function ProblemsPage({
columns={columns}
title="Problems"
searchableFields={["nameId", "title"]}
// onAdd={handleAdd}
onAdd={handleAdd}
// onEdit={null}
onDelete={handleDelete}
allowDownload={true}
Expand Down
3 changes: 3 additions & 0 deletions app/api/orgs/[orgId]/problems/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ export async function GET(
{ params }: { params: { orgId: string } },
) {
try {
console.log("HERE");
const orgId = await getOrgIdFromNameId(NameIdSchema.parse(params.orgId));
console.log(`orgId: ${orgId}`);
const problems = await problemService.getOrgProblems(orgId);
console.log(`problems: ${problems}`);

const validatedProblems = z.array(problemSchema).parse(problems);
return NextResponse.json(validatedProblems);
Expand Down
2 changes: 2 additions & 0 deletions migrations/0001_next_sharon_carter.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "problems" ADD COLUMN "code" text NOT NULL;--> statement-breakpoint
ALTER TABLE "problems" ADD CONSTRAINT "problems_code_unique" UNIQUE("code");
Loading

0 comments on commit 79d0efd

Please sign in to comment.