Skip to content

Commit

Permalink
Fetch section tasks from API, update next.js
Browse files Browse the repository at this point in the history
  • Loading branch information
m-danya committed Nov 6, 2024
1 parent eda54ac commit a13f34d
Show file tree
Hide file tree
Showing 7 changed files with 586 additions and 99 deletions.
11 changes: 8 additions & 3 deletions frontend/app/(main)/section/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"use client";
import { TaskList } from "@/components/tasks/task-list";
export default function Page() {
return <TaskList />;

export default async function Page({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params;
return <TaskList sectionId={id} />;
}
22 changes: 7 additions & 15 deletions frontend/components/tasks/task-list.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import { Task } from "@/components/tasks/task";
import { useSection } from "@/hooks/use-section";
import {
DndContext,
closestCenter,
Expand All @@ -17,22 +18,10 @@ import {
} from "@dnd-kit/sortable";
import { useState } from "react";

const exampleTasks = [
{ id: 1, name: "Watch 'BoJack Horseman'", isCompleted: true },
{
id: 2,
name: "Task with description",
description: "some description here",
isCompleted: true,
},
{ id: 3, name: "Clean the house" },
{ id: 4, name: "Some completed archived task" },
{ id: 5, name: "Go to the gym", description: "💪🏻💪🏻💪🏻" },
{ id: 6, name: "A task with date", date: "2024-12-28" },
];
export function TaskList({sectionId} : {sectionId: string}) {
const { section, isLoading, isError } = useSection(sectionId);
const [tasks, setTasks] = useState(section?.tasks || []);

export function TaskList() {
const [tasks, setTasks] = useState(exampleTasks);
const dndSensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 1 } }),
useSensor(KeyboardSensor, {
Expand Down Expand Up @@ -64,6 +53,9 @@ export function TaskList() {
});
}
}
if (!section) {
return "Loading..";
}

return (
<DndContext
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/tasks/task.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function Task({ task, handleToggleTaskCompleted }) {
})
}
/>
<div className="flex flex-col items-center">{task.name}</div>
<div className="flex flex-col items-center">{task.title}</div>
</div>
{task.description && (
<div className="ml-9 text-gray-400 pt-0.5">{task.description}</div>
Expand Down
11 changes: 11 additions & 0 deletions frontend/hooks/use-section.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import useSWR from "swr";
import { fetcher } from "@/hooks/fetcher";

export const useSection = (sectionId) => {
const { data, error, isLoading } = useSWR(`/api/section/${sectionId}`, fetcher);
return {
section: data,
isLoading,
isError: error,
};
};
Loading

0 comments on commit a13f34d

Please sign in to comment.