Skip to content

Commit

Permalink
Remove dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunkomath committed Jan 12, 2025
1 parent 647432b commit 0543615
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 208 deletions.
105 changes: 51 additions & 54 deletions app/(dashboard)/[tenant]/projects/[projectId]/events/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,74 @@

import { calendarEvent, eventInvite } from "@/drizzle/schema";
import { generateObjectDiffMessage, logActivity } from "@/lib/activity";
import { toDateString, toEndOfDay } from "@/lib/utils/date";
import { database } from "@/lib/utils/useDatabase";
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import { and, eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { type Frequency, RRule } from "rrule";

dayjs.extend(timezone);
dayjs.extend(utc);

export async function createEvent(payload: FormData) {
const { userId, orgSlug } = await getOwner();
const projectId = +(payload.get("projectId") as string);
function handleEventPayload(payload: FormData): {
name: string;
description: string;
start: Date;
end: Date | null;
allDay: boolean;
repeatRule?: string;
invites: string[];
} {
const name = payload.get("name") as string;
const description = payload.get("description") as string;
const start = dayjs.utc(payload.get("start") as string);
const start = new Date(payload.get("start") as string);
const allDay = (payload.get("allDay") as string) === "on";
const repeat = payload.get("repeat") as string;
const invites = ((payload.get("invites") as string) ?? "")
.split(",")
.filter(Boolean);

let end = payload.get("end") ? dayjs.utc(payload.get("end") as string) : null;
let end = payload.get("end") ? new Date(payload.get("end") as string) : null;
if (allDay && end) {
end = end.endOf("day");
end = toEndOfDay(end);
}

const repeatRule = repeat
? new RRule({
freq: repeat as unknown as Frequency,
dtstart: start.toDate(),
until: end ? end.toDate() : null,
dtstart: start,
until: end ?? null,
tzid: "UTC",
})
: undefined;

return {
name,
description,
start,
end,
allDay,
repeatRule: repeatRule?.toString(),
invites,
};
}

export async function createEvent(payload: FormData) {
const { userId, orgSlug } = await getOwner();
const projectId = +(payload.get("projectId") as string);

const { name, description, start, end, allDay, repeatRule, invites } =
handleEventPayload(payload);

const db = await database();
const createdEvent = await db
const createdEvent = db
.insert(calendarEvent)
.values({
name,
description,
start: start.toDate(),
end: end?.toDate(),
start,
end,
allDay,
repeatRule: repeatRule?.toString(),
repeatRule,
projectId,
createdByUser: userId,
createdAt: new Date(),
Expand All @@ -60,8 +79,7 @@ export async function createEvent(payload: FormData) {
.get();

for (const userId of invites) {
await db
.insert(eventInvite)
db.insert(eventInvite)
.values({
eventId: createdEvent.id,
userId,
Expand All @@ -81,43 +99,23 @@ export async function createEvent(payload: FormData) {

revalidatePath(`/${orgSlug}/projects/${projectId}/events`);
redirect(
`/${orgSlug}/projects/${projectId}/events?on=${start.tz(timezone).format("YYYY-MM-DD")}`,
`/${orgSlug}/projects/${projectId}/events?on=${toDateString(start, timezone)}`,
);
}

export async function updateEvent(payload: FormData) {
const { orgSlug } = await getOwner();
const id = +(payload.get("id") as string);
const name = payload.get("name") as string;
const description = payload.get("description") as string;
const start = dayjs.utc(payload.get("start") as string);
const allDay = (payload.get("allDay") as string) === "on";
const repeat = payload.get("repeat") as string;
const projectId = payload.get("projectId") as string;
const invites = ((payload.get("invites") as string) ?? "")
.split(",")
.filter(Boolean);

let end = payload.get("end") ? dayjs.utc(payload.get("end") as string) : null;
if (allDay && end) {
end = end.endOf("day");
}
const projectId = +(payload.get("projectId") as string);

const repeatRule = repeat
? new RRule({
freq: repeat as unknown as Frequency,
dtstart: start.toDate(),
until: end ? end.toDate() : null,
tzid: "UTC",
})
: undefined;
const { name, description, start, end, allDay, repeatRule, invites } =
handleEventPayload(payload);

const db = await database();
await db.delete(eventInvite).where(eq(eventInvite.eventId, id)).run();
db.delete(eventInvite).where(eq(eventInvite.eventId, id)).run();

for (const userId of invites) {
await db
.insert(eventInvite)
db.insert(eventInvite)
.values({
eventId: id,
userId,
Expand All @@ -135,13 +133,12 @@ export async function updateEvent(payload: FormData) {
})
.execute();

await db
.update(calendarEvent)
db.update(calendarEvent)
.set({
name,
description,
start: start.toDate(),
end: end?.toDate(),
start,
end,
allDay,
repeatRule: repeatRule?.toString(),
updatedAt: new Date(),
Expand All @@ -160,8 +157,8 @@ export async function updateEvent(payload: FormData) {
{
name,
description,
start: start.toDate(),
end: end?.toDate(),
start,
end,
allDay,
},
)}`,
Expand All @@ -172,7 +169,7 @@ export async function updateEvent(payload: FormData) {

revalidatePath(`/${orgSlug}/projects/${projectId}/events`);
redirect(
`/${orgSlug}/projects/${projectId}/events?on=${start.tz(timezone).format("YYYY-MM-DD")}`,
`/${orgSlug}/projects/${projectId}/events?on=${toDateString(start, timezone)}`,
);
}

Expand All @@ -182,7 +179,7 @@ export async function deleteEvent(payload: FormData) {
const projectId = payload.get("projectId") as string;

const db = await database();
const eventDetails = await db
const eventDetails = db
.delete(calendarEvent)
.where(eq(calendarEvent.id, id))
.returning()
Expand Down
21 changes: 10 additions & 11 deletions app/(dashboard)/[tenant]/projects/[projectId]/events/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { CommentsSection } from "@/components/project/comment/comments-section";
import EventsCalendar from "@/components/project/events/events-calendar";
import { buttonVariants } from "@/components/ui/button";
import { calendarEvent } from "@/drizzle/schema";
import {
toDateStringWithDay,
toEndOfDay,
toStartOfDay,
} from "@/lib/utils/date";
import { database } from "@/lib/utils/useDatabase";
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import {
and,
asc,
Expand All @@ -32,9 +34,6 @@ type Props = {
}>;
};

dayjs.extend(utc);
dayjs.extend(timezone);

export default async function EventDetails(props: Props) {
const searchParams = await props.searchParams;
const params = await props.params;
Expand All @@ -44,11 +43,11 @@ export default async function EventDetails(props: Props) {

const timezone = await getTimezone();

const selectedDate = on ? dayjs(new Date(on)) : dayjs().tz(timezone);
const dayCommentId = `${projectId}${selectedDate.year()}${selectedDate.month()}${selectedDate.date()}`;
const selectedDate = on ? new Date(on) : new Date();
const dayCommentId = `${projectId}${selectedDate.getFullYear()}${selectedDate.getMonth()}${selectedDate.getDay()}`;

const startOfDay = selectedDate.startOf("day").toDate();
const endOfDay = selectedDate.endOf("day").toDate();
const startOfDay = toStartOfDay(selectedDate);
const endOfDay = toEndOfDay(selectedDate);

const db = await database();
const events = await db.query.calendarEvent
Expand Down Expand Up @@ -98,7 +97,7 @@ export default async function EventDetails(props: Props) {
actionLink={`/${orgSlug}/projects/${projectId}/events/new`}
>
<div className="font-medium text-gray-500">
{selectedDate.tz(timezone).format("dddd, MMMM D, YYYY")}
{toDateStringWithDay(selectedDate, timezone)}
</div>
</PageTitle>

Expand Down
9 changes: 7 additions & 2 deletions app/(dashboard)/[tenant]/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PageTitle from "@/components/layout/page-title";
import { ProjecItem } from "@/components/project/project-item";
import { buttonVariants } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { getOwner } from "@/lib/utils/useOwner";
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
import { getProjectsForOwner } from "@/lib/utils/useProjects";
import Link from "next/link";

Expand All @@ -19,6 +19,7 @@ export default async function Projects(props: Props) {
const searchParams = await props.searchParams;
const { orgSlug } = await getOwner();
const statuses = searchParams?.status?.split(",") ?? ["active"];
const timezone = await getTimezone();

const { projects, archivedProjects } = await getProjectsForOwner({
search: searchParams.search,
Expand Down Expand Up @@ -66,7 +67,11 @@ export default async function Projects(props: Props) {

<div className="grid grid-cols-1 gap-4 px-4 sm:grid-cols-2 lg:px-0">
{projects.map((project) => (
<ProjecItem key={project.id} project={project} />
<ProjecItem
key={project.id}
project={project}
timezone={timezone}
/>
))}
</div>
</div>
Expand Down
Loading

0 comments on commit 0543615

Please sign in to comment.