Skip to content

Commit d49d44d

Browse files
authored
Merge pull request #89 from techulus/main
Remove server actions used for fetching
2 parents c849724 + 0c16b6f commit d49d44d

File tree

20 files changed

+192
-132
lines changed

20 files changed

+192
-132
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { getUserNotifications } from "@/app/(dashboard)/[tenant]/settings/actions";
2+
import type { NextRequest } from "next/server";
3+
import { NextResponse } from "next/server";
4+
5+
export async function GET(_: NextRequest) {
6+
try {
7+
const notifications = await getUserNotifications();
8+
return NextResponse.json(notifications);
9+
} catch (error) {
10+
return NextResponse.json(
11+
{ error: (error as Error).message },
12+
{ status: 500 },
13+
);
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getProjectById } from "@/lib/utils/useProjects";
2+
import { NextResponse } from "next/server";
3+
4+
export async function GET(
5+
_: Request,
6+
{ params }: { params: Promise<{ id: string }> },
7+
) {
8+
const { id } = await params;
9+
try {
10+
const project = await getProjectById(id, true);
11+
return NextResponse.json(project);
12+
} catch (error) {
13+
return NextResponse.json(
14+
{ error: (error as Error).message },
15+
{ status: 500 },
16+
);
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getProjectsForOwner } from "@/lib/utils/useProjects";
2+
import { NextResponse } from "next/server";
3+
4+
export async function GET(_: Request) {
5+
try {
6+
const projects = await getProjectsForOwner({
7+
statuses: ["active"],
8+
});
9+
return NextResponse.json(projects);
10+
} catch (error) {
11+
return NextResponse.json(
12+
{ error: (error as Error).message },
13+
{ status: 500 },
14+
);
15+
}
16+
}

app/(dashboard)/[tenant]/projects/[projectId]/events/[eventId]/edit/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import EventForm from "@/components/form/event";
33
import PageTitle from "@/components/layout/page-title";
44
import { calendarEvent } from "@/drizzle/schema";
55
import { database } from "@/lib/utils/useDatabase";
6-
import { allUsers } from "@/lib/utils/useUser";
6+
import { getAllUsers } from "@/lib/utils/useUser";
77
import { eq } from "drizzle-orm";
88
import { notFound } from "next/navigation";
99

@@ -18,7 +18,7 @@ export default async function EditEvent(props: Props) {
1818
const params = await props.params;
1919
const { projectId, eventId } = params;
2020

21-
const users = await allUsers();
21+
const users = await getAllUsers();
2222
const db = await database();
2323
const event = await db.query.calendarEvent.findFirst({
2424
where: eq(calendarEvent.id, +eventId),

app/(dashboard)/[tenant]/projects/[projectId]/events/new/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import PageSection from "@/components/core/section";
22
import EventForm from "@/components/form/event";
33
import PageTitle from "@/components/layout/page-title";
44
import { getOwner } from "@/lib/utils/useOwner";
5-
import { allUsers } from "@/lib/utils/useUser";
5+
import { getAllUsers } from "@/lib/utils/useUser";
66

77
type Props = {
88
params: Promise<{
@@ -17,7 +17,7 @@ export default async function CreateEvent(props: Props) {
1717
const params = await props.params;
1818
const searchParams = await props.searchParams;
1919

20-
const users = await allUsers();
20+
const users = await getAllUsers();
2121

2222
return (
2323
<>

app/(dashboard)/[tenant]/projects/[projectId]/page.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ import {
1919
import { toDateStringWithDay } from "@/lib/utils/date";
2020
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
2121
import { getProjectById } from "@/lib/utils/useProjects";
22-
import {
23-
CalendarPlusIcon,
24-
ListPlusIcon,
25-
PencilIcon,
26-
PlusIcon,
27-
} from "lucide-react";
22+
import { CalendarPlusIcon, ListPlusIcon, PlusIcon } from "lucide-react";
2823
import Link from "next/link";
2924
import { notFound } from "next/navigation";
25+
import { Suspense } from "react";
3026
import { archiveProject, deleteProject, unarchiveProject } from "../actions";
3127

3228
type Props = {
@@ -208,13 +204,11 @@ export default async function ProjectDetails(props: Props) {
208204
<ul className="grid grid-cols-2 gap-x-4 gap-y-4 md:grid-cols-4 lg:grid-cols-6">
209205
{project.documents.map((document) => (
210206
<div key={document.id}>
211-
{/* @ts-ignore */}
212207
<DocumentHeader document={document} />
213208
</div>
214209
))}
215210
{project.documentFolders.map((folder) => (
216211
<div key={folder.id}>
217-
{/* @ts-ignore */}
218212
<DocumentFolderHeader documentFolder={folder} />
219213
</div>
220214
))}
@@ -256,8 +250,7 @@ export default async function ProjectDetails(props: Props) {
256250
</div>
257251
</div>
258252

259-
<div className="mx-auto max-w-5xl p-4 xl:p-0 lg:py-8">
260-
{/* @ts-ignore */}
253+
<div className="mx-auto max-w-5xl p-4 lg:py-8">
261254
<CommentsSection
262255
type="project"
263256
parentId={project.id}

app/(dashboard)/[tenant]/projects/[projectId]/tasklists/[tasklistId]/page.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ import { task, taskList, user } from "@/drizzle/schema";
66
import { toDateStringWithDay } from "@/lib/utils/date";
77
import { database } from "@/lib/utils/useDatabase";
88
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
9+
import { getAllUsers } from "@/lib/utils/useUser";
910
import { and, asc, eq } from "drizzle-orm";
1011
import { CheckCircle, ClockIcon } from "lucide-react";
1112
import { notFound } from "next/navigation";
12-
import { createTask, partialUpdateTaskList } from "../actions";
13+
import {
14+
createTask,
15+
getActiveTaskLists,
16+
partialUpdateTaskList,
17+
} from "../actions";
1318

1419
type Props = {
1520
params: Promise<{
@@ -65,6 +70,9 @@ export default async function TaskLists(props: Props) {
6570
return notFound();
6671
}
6772

73+
const allTaskLists = await getActiveTaskLists(+projectId);
74+
const allUsers = await getAllUsers(true);
75+
6876
const totalCount = list.tasks.length;
6977
const doneCount = list.tasks.filter((task) => task.status === "done").length;
7078

@@ -126,6 +134,8 @@ export default async function TaskLists(props: Props) {
126134
createTask={createTask}
127135
orgSlug={orgSlug}
128136
partialUpdateTaskList={partialUpdateTaskList}
137+
taskLists={allTaskLists}
138+
users={allUsers}
129139
hideHeader
130140
/>
131141

app/(dashboard)/[tenant]/projects/[projectId]/tasklists/page.tsx

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ import { buttonVariants } from "@/components/ui/button";
55
import { task, taskList } from "@/drizzle/schema";
66
import { database } from "@/lib/utils/useDatabase";
77
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
8+
import { getAllUsers } from "@/lib/utils/useUser";
89
import { and, asc, eq, or } from "drizzle-orm";
910
import Link from "next/link";
10-
import { createTask, partialUpdateTaskList } from "./actions";
11+
import {
12+
createTask,
13+
getActiveTaskLists,
14+
partialUpdateTaskList,
15+
} from "./actions";
1116

1217
type Props = {
1318
params: Promise<{
@@ -31,42 +36,46 @@ export default async function TaskLists(props: Props) {
3136
);
3237

3338
const db = await database();
34-
const taskLists = await db.query.taskList
35-
.findMany({
36-
where: and(eq(taskList.projectId, +projectId), or(...statusFilter)),
37-
with: {
38-
tasks: {
39-
orderBy: [asc(task.position)],
39+
const [taskLists, archivedTaskLists, allTaskLists, allUsers] =
40+
await Promise.all([
41+
db.query.taskList
42+
.findMany({
43+
where: and(eq(taskList.projectId, +projectId), or(...statusFilter)),
4044
with: {
41-
creator: {
42-
columns: {
43-
firstName: true,
44-
imageUrl: true,
45-
},
46-
},
47-
assignee: {
48-
columns: {
49-
firstName: true,
50-
imageUrl: true,
45+
tasks: {
46+
orderBy: [asc(task.position)],
47+
with: {
48+
creator: {
49+
columns: {
50+
firstName: true,
51+
imageUrl: true,
52+
},
53+
},
54+
assignee: {
55+
columns: {
56+
firstName: true,
57+
imageUrl: true,
58+
},
59+
},
5160
},
5261
},
5362
},
54-
},
55-
},
56-
})
57-
.execute();
58-
59-
const archivedTaskLists = await db.query.taskList
60-
.findMany({
61-
columns: {
62-
id: true,
63-
},
64-
where: and(
65-
eq(taskList.projectId, +projectId),
66-
eq(taskList.status, "archived"),
67-
),
68-
})
69-
.execute();
63+
})
64+
.execute(),
65+
db.query.taskList
66+
.findMany({
67+
columns: {
68+
id: true,
69+
},
70+
where: and(
71+
eq(taskList.projectId, +projectId),
72+
eq(taskList.status, "archived"),
73+
),
74+
})
75+
.execute(),
76+
getActiveTaskLists(+projectId),
77+
getAllUsers(true),
78+
]);
7079

7180
return (
7281
<>
@@ -95,6 +104,8 @@ export default async function TaskLists(props: Props) {
95104
partialUpdateTaskList={partialUpdateTaskList}
96105
orgSlug={orgSlug}
97106
timezone={timezone}
107+
taskLists={allTaskLists}
108+
users={allUsers}
98109
compact
99110
/>
100111
))}

app/(dashboard)/[tenant]/today/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ export default async function Today(props: {
125125
]);
126126

127127
const dueToday = tasksDueToday.filter(
128-
(t) => t.taskList.status !== "archived",
128+
(t) => t.taskList?.status !== "archived",
129129
);
130130

131-
const overDue = overDueTasks.filter((t) => t.taskList.status !== "archived");
131+
const overDue = overDueTasks.filter((t) => t.taskList?.status !== "archived");
132132

133133
const filteredEvents = events.filter((event) =>
134134
filterByRepeatRule(event, new Date(today), timezone),
@@ -214,6 +214,7 @@ export default async function Today(props: {
214214
</>
215215
);
216216
}
217+
217218
function TaskItem(
218219
tenant: string,
219220
task: {

components/core/notifications.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { getUserNotifications } from "@/app/(dashboard)/[tenant]/settings/actions";
3+
import type { Notification } from "@/drizzle/types";
44
import { cn } from "@/lib/utils";
55
import { TurboWire } from "@turbowire/web";
66
import { Bell, Dot } from "lucide-react";
@@ -25,16 +25,18 @@ function Notifications({
2525
const isActive = pathname === `/${tenant}/notifications`;
2626

2727
const checkNotifications = useCallback(() => {
28-
getUserNotifications().then((notifications) => {
29-
setUnreadCount(notifications.filter((x) => !x.read).length);
30-
});
28+
fetch("/api/user/notifications")
29+
.then((res) => res.json())
30+
.then((data) => {
31+
setUnreadCount(data.filter((x: Notification) => !x.read).length);
32+
});
3133
}, []);
3234

3335
useEffect(() => {
34-
checkNotifications();
35-
3636
if (!notificationsWire) return;
3737

38+
checkNotifications();
39+
3840
const wire = new TurboWire(notificationsWire);
3941
wire.connect((message) => {
4042
try {

0 commit comments

Comments
 (0)