From adc3560803946c151217213b9fb660349ad134d3 Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Tue, 30 Apr 2024 20:15:32 -0400 Subject: [PATCH 1/6] changed date field to store ISOstring and date format --- src/components/user/AddFile.tsx | 3 +-- src/server/model/index.ts | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/user/AddFile.tsx b/src/components/user/AddFile.tsx index ca0737a9..e1c49dad 100644 --- a/src/components/user/AddFile.tsx +++ b/src/components/user/AddFile.tsx @@ -107,7 +107,6 @@ const AddFile = ({ const router = useRouter(); const [error, setError] = useState(false); const [selectedTags, setSelectedTags] = useState([]); - const handleResetState = () => { setStartDate(new Date()); setSelectedTags([]); @@ -175,7 +174,7 @@ const AddFile = ({ className="mb-4 h-12 w-full cursor-pointer rounded-lg pl-4" selected={startDate} onChange={(date) => date && setStartDate(date)} - dateFormat="dd MMMM yyyy" + dateFormat="YYYY-MM-dd" excludeDates={excludeDates} /> {excludedDatesString.includes(startDate.toDateString()) ? ( diff --git a/src/server/model/index.ts b/src/server/model/index.ts index 1d43796d..d22568d2 100644 --- a/src/server/model/index.ts +++ b/src/server/model/index.ts @@ -17,6 +17,7 @@ export const File = z.object({ date: z.string().transform((val) => { const date = new Date(val); date.setHours(0, 0, 0, 0); + date.toISOString(); return date; }), filetype: z.string(), From 0358d913ce2a0ee79a4849386d9c1b344ea42280 Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Tue, 30 Apr 2024 20:20:28 -0400 Subject: [PATCH 2/6] removed console log --- src/components/user/AddFile.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/user/AddFile.tsx b/src/components/user/AddFile.tsx index e1c49dad..4badd668 100644 --- a/src/components/user/AddFile.tsx +++ b/src/components/user/AddFile.tsx @@ -97,7 +97,6 @@ const AddFile = ({ const currFiles = Object.values(files); const excludeDates = currFiles.map((fileObj) => fileObj.date); - console.log(excludeDates); const excludedDatesString = excludeDates .map((dateObj) => dateObj.toDateString()) .filter((date) => editFile?.date.toDateString() !== date ?? true); From efa5614683ec010bbe70f15a4a4a95983775e99c Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Wed, 1 May 2024 03:11:15 -0400 Subject: [PATCH 3/6] Parse File Date with UTC, Front side restore offset due to Prisma --- src/components/senior/DisplaySenior.tsx | 10 ++++++++-- src/server/model/index.ts | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/senior/DisplaySenior.tsx b/src/components/senior/DisplaySenior.tsx index 6837fb77..3f85149d 100644 --- a/src/components/senior/DisplaySenior.tsx +++ b/src/components/senior/DisplaySenior.tsx @@ -48,6 +48,12 @@ const DisplaySenior = (props: DisplayProps) => { seniorId: senior.id, }); }; + const seniorFiles = senior.Files.map((file) => { + const userTimeZoneOffset = new Date().getTimezoneOffset(); + const newDate = new Date(file.date.getTime() + userTimeZoneOffset * 60000); + const { date, ...other } = file; + return { date: newDate, ...other }; + }); return (
@@ -79,7 +85,7 @@ const DisplaySenior = (props: DisplayProps) => { setFileEdit={canAddFile ? setFileEdit : undefined} /> )} - elements={senior.Files.sort( + elements={seniorFiles.sort( (fileA, fileB) => fileA.date.getTime() - fileB.date.getTime() )} search={(file, filter) => formatFileDate(file.date).includes(filter)} @@ -88,7 +94,7 @@ const DisplaySenior = (props: DisplayProps) => { { const date = new Date(val); - date.setHours(0, 0, 0, 0); - date.toISOString(); + date.setUTCHours(0, 0, 0, 0); return date; }), filetype: z.string(), From f3ed21609ce77686e735fa980ba1ea2b79bfe263 Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Wed, 1 May 2024 04:04:41 -0400 Subject: [PATCH 4/6] Restored against Moment converts Dates to local time --- src/app/api/file/[fileId]/route.ts | 7 +++++-- src/app/api/file/route.ts | 6 +++++- src/components/user/AddFile.tsx | 3 +-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/app/api/file/[fileId]/route.ts b/src/app/api/file/[fileId]/route.ts index c7beea90..4abdbc54 100644 --- a/src/app/api/file/[fileId]/route.ts +++ b/src/app/api/file/[fileId]/route.ts @@ -58,8 +58,11 @@ export const PATCH = withSession(async ({ params, session, req }) => { { status: 400 } ); } - - const formatted_date = moment(fileData.date).format("L"); + const userTimeZoneOffset = new Date().getTimezoneOffset(); + const newDate = new Date( + fileData.date.getTime() + userTimeZoneOffset * 60000 + ); + const formatted_date = moment(newDate).format("L"); const body = { name: formatted_date }; diff --git a/src/app/api/file/route.ts b/src/app/api/file/route.ts index ef78c1f7..ce055aac 100644 --- a/src/app/api/file/route.ts +++ b/src/app/api/file/route.ts @@ -65,7 +65,11 @@ export const POST = withSession(async (request) => { const parentID = foundSenior.folder; - const formatted_date = moment(fileData.date).format("L"); + const userTimeZoneOffset = new Date().getTimezoneOffset(); + const newDate = new Date( + fileData.date.getTime() + userTimeZoneOffset * 60000 + ); + const formatted_date = moment(newDate).format("L"); const fileMetadata = { name: [formatted_date], diff --git a/src/components/user/AddFile.tsx b/src/components/user/AddFile.tsx index 4badd668..a338e139 100644 --- a/src/components/user/AddFile.tsx +++ b/src/components/user/AddFile.tsx @@ -154,7 +154,6 @@ const AddFile = ({ } return !error ? ( - //
Create New File
@@ -173,7 +172,7 @@ const AddFile = ({ className="mb-4 h-12 w-full cursor-pointer rounded-lg pl-4" selected={startDate} onChange={(date) => date && setStartDate(date)} - dateFormat="YYYY-MM-dd" + dateFormat="dd MMMM yyyy" excludeDates={excludeDates} /> {excludedDatesString.includes(startDate.toDateString()) ? ( From 15730bb63691400d1f53e9f19d7720e005c045bb Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Wed, 1 May 2024 19:44:22 -0400 Subject: [PATCH 5/6] address PR review --- src/app/api/file/[fileId]/route.ts | 3 +-- src/app/api/file/route.ts | 3 +-- src/components/senior/DisplaySenior.tsx | 13 ++++++------- src/server/model/index.ts | 3 +++ 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/app/api/file/[fileId]/route.ts b/src/app/api/file/[fileId]/route.ts index 4abdbc54..bc59d195 100644 --- a/src/app/api/file/[fileId]/route.ts +++ b/src/app/api/file/[fileId]/route.ts @@ -58,9 +58,8 @@ export const PATCH = withSession(async ({ params, session, req }) => { { status: 400 } ); } - const userTimeZoneOffset = new Date().getTimezoneOffset(); const newDate = new Date( - fileData.date.getTime() + userTimeZoneOffset * 60000 + fileData.date.getTime() + new Date().getTimezoneOffset() * 60000 ); const formatted_date = moment(newDate).format("L"); diff --git a/src/app/api/file/route.ts b/src/app/api/file/route.ts index ce055aac..b3808b74 100644 --- a/src/app/api/file/route.ts +++ b/src/app/api/file/route.ts @@ -65,9 +65,8 @@ export const POST = withSession(async (request) => { const parentID = foundSenior.folder; - const userTimeZoneOffset = new Date().getTimezoneOffset(); const newDate = new Date( - fileData.date.getTime() + userTimeZoneOffset * 60000 + fileData.date.getTime() + new Date().getTimezoneOffset() * 60000 ); const formatted_date = moment(newDate).format("L"); diff --git a/src/components/senior/DisplaySenior.tsx b/src/components/senior/DisplaySenior.tsx index 3f85149d..ebedb595 100644 --- a/src/components/senior/DisplaySenior.tsx +++ b/src/components/senior/DisplaySenior.tsx @@ -48,13 +48,12 @@ const DisplaySenior = (props: DisplayProps) => { seniorId: senior.id, }); }; - const seniorFiles = senior.Files.map((file) => { - const userTimeZoneOffset = new Date().getTimezoneOffset(); - const newDate = new Date(file.date.getTime() + userTimeZoneOffset * 60000); - const { date, ...other } = file; - return { date: newDate, ...other }; - }); - + const seniorFiles = senior.Files.map((file) => ({ + ...file, + date: new Date( + file.date.getTime() + new Date().getTimezoneOffset() * 60000 + ), + })); return (

diff --git a/src/server/model/index.ts b/src/server/model/index.ts index d740c0f1..c96705ff 100644 --- a/src/server/model/index.ts +++ b/src/server/model/index.ts @@ -13,6 +13,9 @@ export const seniorSchema = z.object({ ChapterID: z.string(), }) satisfies z.ZodType; +/* https://stackoverflow.com/questions/49407453/setting-sethours-zero-not-working-in-nodejs + Stores Date object at 0:00:00 UTC time +*/ export const File = z.object({ date: z.string().transform((val) => { const date = new Date(val); From cc249eec0b851e37b375a666f68d9de6a27c2910 Mon Sep 17 00:00:00 2001 From: Johnny Tan Date: Thu, 2 May 2024 13:49:54 -0400 Subject: [PATCH 6/6] add offSetDate time helper function --- src/app/api/file/[fileId]/route.ts | 6 ++---- src/app/api/file/route.ts | 6 ++---- src/components/senior/DisplaySenior.tsx | 12 ++++++++---- src/utils/index.tsx | 4 ++++ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/app/api/file/[fileId]/route.ts b/src/app/api/file/[fileId]/route.ts index bc59d195..9bb48af5 100644 --- a/src/app/api/file/[fileId]/route.ts +++ b/src/app/api/file/[fileId]/route.ts @@ -8,6 +8,7 @@ import { prisma } from "@server/db/client"; import { withSession } from "@server/decorator"; import { driveV3 } from "@server/service"; import moment from "moment"; +import { offSetDateToUTC } from "@utils"; export const PATCH = withSession(async ({ params, session, req }) => { const body = await req.json(); @@ -58,10 +59,7 @@ export const PATCH = withSession(async ({ params, session, req }) => { { status: 400 } ); } - const newDate = new Date( - fileData.date.getTime() + new Date().getTimezoneOffset() * 60000 - ); - const formatted_date = moment(newDate).format("L"); + const formatted_date = moment(offSetDateToUTC(fileData.date)).format("L"); const body = { name: formatted_date }; diff --git a/src/app/api/file/route.ts b/src/app/api/file/route.ts index b3808b74..040379ab 100644 --- a/src/app/api/file/route.ts +++ b/src/app/api/file/route.ts @@ -5,6 +5,7 @@ import { prisma } from "@server/db/client"; import { withSession } from "@server/decorator"; import { driveV3 } from "@server/service"; import moment from "moment"; +import { offSetDateToUTC } from "@utils"; export const POST = withSession(async (request) => { const body = await request.req.json(); @@ -65,10 +66,7 @@ export const POST = withSession(async (request) => { const parentID = foundSenior.folder; - const newDate = new Date( - fileData.date.getTime() + new Date().getTimezoneOffset() * 60000 - ); - const formatted_date = moment(newDate).format("L"); + const formatted_date = moment(offSetDateToUTC(fileData.date)).format("L"); const fileMetadata = { name: [formatted_date], diff --git a/src/components/senior/DisplaySenior.tsx b/src/components/senior/DisplaySenior.tsx index ebedb595..97fd7269 100644 --- a/src/components/senior/DisplaySenior.tsx +++ b/src/components/senior/DisplaySenior.tsx @@ -2,7 +2,13 @@ import SearchableContainer from "@components/SearchableContainer"; import { Prisma, User } from "@prisma/client"; -import { compareUser, formatFileDate, fullName, seniorFullName } from "@utils"; +import { + compareUser, + formatFileDate, + fullName, + offSetDateToUTC, + seniorFullName, +} from "@utils"; import { File } from "@components/file"; import AddFile from "@components/file/AddFile"; import { v4 as uuid } from "uuid"; @@ -50,9 +56,7 @@ const DisplaySenior = (props: DisplayProps) => { }; const seniorFiles = senior.Files.map((file) => ({ ...file, - date: new Date( - file.date.getTime() + new Date().getTimezoneOffset() * 60000 - ), + date: offSetDateToUTC(file.date), })); return (
diff --git a/src/utils/index.tsx b/src/utils/index.tsx index 97c8e523..30c20ca2 100644 --- a/src/utils/index.tsx +++ b/src/utils/index.tsx @@ -86,3 +86,7 @@ export const sortedStudents = (students: User[]) => { return students.sort(comparePositions); }; + +export const offSetDateToUTC = (date: Date) => { + return new Date(date.getTime() + date.getTimezoneOffset() * 60000); +};