Skip to content

Commit

Permalink
Merge pull request #179 from JumboCode/johnny/addFileDate-Change
Browse files Browse the repository at this point in the history
File Date field set at 0:00 UTC + offset on retrieval with Prisma
  • Loading branch information
johnny-t06 authored May 2, 2024
2 parents 9a5f005 + cc249ee commit 407c7aa
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/app/api/file/[fileId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -58,8 +59,7 @@ export const PATCH = withSession(async ({ params, session, req }) => {
{ status: 400 }
);
}

const formatted_date = moment(fileData.date).format("L");
const formatted_date = moment(offSetDateToUTC(fileData.date)).format("L");

const body = { name: formatted_date };

Expand Down
3 changes: 2 additions & 1 deletion src/app/api/file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -65,7 +66,7 @@ export const POST = withSession(async (request) => {

const parentID = foundSenior.folder;

const formatted_date = moment(fileData.date).format("L");
const formatted_date = moment(offSetDateToUTC(fileData.date)).format("L");

const fileMetadata = {
name: [formatted_date],
Expand Down
17 changes: 13 additions & 4 deletions src/components/senior/DisplaySenior.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -48,7 +54,10 @@ const DisplaySenior = (props: DisplayProps) => {
seniorId: senior.id,
});
};

const seniorFiles = senior.Files.map((file) => ({
...file,
date: offSetDateToUTC(file.date),
}));
return (
<div className="flex flex-col gap-y-6">
<h1 className="text-4xl font-bold text-[#000022]">
Expand Down Expand Up @@ -79,7 +88,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)}
Expand All @@ -88,7 +97,7 @@ const DisplaySenior = (props: DisplayProps) => {
<AddFile
seniorId={senior.id}
seniorFolder={senior.folder}
files={senior.Files}
files={seniorFiles}
key={addFileId}
editFile={editFile}
setEditFile={setFileEdit}
Expand Down
3 changes: 0 additions & 3 deletions src/components/user/AddFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -107,7 +106,6 @@ const AddFile = ({
const router = useRouter();
const [error, setError] = useState<boolean>(false);
const [selectedTags, setSelectedTags] = useState<TagProps[]>([]);

const handleResetState = () => {
setStartDate(new Date());
setSelectedTags([]);
Expand Down Expand Up @@ -156,7 +154,6 @@ const AddFile = ({
}

return !error ? (
// <Popup className="h-[32rem] w-full overflow-y-auto sm:h-[44rem] sm:w-[36rem]">
<Popup className="h-fit w-full overflow-y-auto sm:w-[36rem]">
<div className="flex-col justify-between rounded-[16px] text-white">
<div className="mb-5 text-2xl font-bold">Create New File</div>
Expand Down
5 changes: 4 additions & 1 deletion src/server/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ export const seniorSchema = z.object({
ChapterID: z.string(),
}) satisfies z.ZodType<Senior>;

/* 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);
date.setHours(0, 0, 0, 0);
date.setUTCHours(0, 0, 0, 0);
return date;
}),
filetype: z.string(),
Expand Down
4 changes: 4 additions & 0 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 407c7aa

Please sign in to comment.