Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed date field to store ISOstring and date format #179

Merged
merged 6 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/app/api/file/[fileId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moment.js does a similar conversion to local time zone error to Prisma, therefore I added an offset such that the Google Docs name matches.


const body = { name: formatted_date };

Expand Down
6 changes: 5 additions & 1 deletion src/app/api/file/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well!


const fileMetadata = {
name: [formatted_date],
Expand Down
10 changes: 8 additions & 2 deletions src/components/senior/DisplaySenior.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Copy link
Collaborator

@nickbar01234 nickbar01234 May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think it's cleaner to do

return { ...file, date: newDate }

instead of de-structuring { date, ...other }. Alternatively, since there's not too much logic here, it wouldn't be bad style to write the map function in one-line.

senior.Files.map((file) => ({ ...file, date: new Date(# Your logic)})

});

return (
<div className="flex flex-col gap-y-6">
Expand Down Expand Up @@ -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)}
Expand All @@ -88,7 +94,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
2 changes: 1 addition & 1 deletion src/server/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const seniorSchema = z.object({
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this bug was quite tricky to find, I would add the link we used here!

return date;
}),
filetype: z.string(),
Expand Down