Skip to content

Commit

Permalink
attempt to fix #69 #70 by using utc date strings everywhere, and only…
Browse files Browse the repository at this point in the history
… parsing into local for display
  • Loading branch information
kapil1garg committed May 21, 2024
1 parent 33fef1b commit eca9444
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 69 deletions.
12 changes: 10 additions & 2 deletions components/CurrWeekIssueCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export default function CurrWeekIssueCard({
// onAddIssue is a function that adds a new issue to the current issues
const onAddIssue = (newIssueTitle) => {
let newIssueForWeek = serializeDates(
createNewIssueObject(newIssueTitle, project, sig, date, [], true)
createNewIssueObject(
newIssueTitle,
project,
sig,
new Date(date).toISOString(),
[],
true
)
);
setCurrentIssuesData((prevData) => {
return [...prevData, newIssueForWeek];
Expand All @@ -50,7 +57,7 @@ export default function CurrWeekIssueCard({
setCurrentIssuesData((prevData) => {
let issuesToUpdate = [...prevData];
let issueIndex = issuesToUpdate.findIndex((i) => i.id === issueId);
issuesToUpdate[issueIndex].lastUpdated = longDate(new Date());
issuesToUpdate[issueIndex].lastUpdated = new Date().toISOString();
issuesToUpdate[issueIndex].wasDeleted = true;
return issuesToUpdate;
});
Expand All @@ -67,6 +74,7 @@ export default function CurrWeekIssueCard({
let issuesToUpdate = [...prevData];
let issueIndex = issuesToUpdate.findIndex((i) => i.id === issueId);
issuesToUpdate[issueIndex].title = value;
issuesToUpdate[issueIndex].lastUpdated = new Date().toISOString();
return issuesToUpdate;
});
};
Expand Down
16 changes: 8 additions & 8 deletions components/CurrWeekIssuePane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export default function CurrWeekIssuePane({
title: noteBlock.value
.trim()
.replace(/<\/?[^>]+(>|$)/g, ''),
date: longDate(new Date()),
lastUpdated: longDate(new Date()),
date: new Date().toISOString(),
lastUpdated: new Date().toISOString(),
context: editsToIssue['context'],
assessment: editsToIssue['assessment'],
plan: editsToIssue['plan'],
Expand Down Expand Up @@ -333,8 +333,8 @@ export default function CurrWeekIssuePane({
// if the current instance doesn't exist, intialize it with the additions from the notetaking space
issueInstance = {
id: new mongoose.Types.ObjectId().toString(),
date: longDate(new Date(new Date())),
lastUpdated: longDate(new Date()),
date: new Date().toISOString(),
lastUpdated: new Date().toISOString(),
context: editsToIssue['context'],
assessment: editsToIssue['summary'],
plan: editsToIssue['plan'],
Expand Down Expand Up @@ -387,15 +387,15 @@ export default function CurrWeekIssuePane({
}

// update the last updated date
issueInstance.date = longDate(new Date());
issueInstance.date = new Date().toISOString();
}

setCAPData((prevCAPData) => {
let newCAPData = { ...prevCAPData };
newcurrentIssuesData[issueIndex] =
issueInstance;
newcurrentIssuesData[issueIndex].lastUpdated =
longDate(new Date());
new Date().toISOString();

return newCAPData;
});
Expand Down Expand Up @@ -677,7 +677,7 @@ export default function CurrWeekIssuePane({
key={`issue-card-${practiceGap.id}`}
project={project}
sig={sig}
date={date}
date={new Date(date).toISOString()}
practiceGapId={practiceGap.id}
practiceGap={practiceGap}
practiceGapsData={practiceGapData}
Expand All @@ -695,7 +695,7 @@ export default function CurrWeekIssuePane({
key="issue-card-add-practice"
project={project}
sig={sig}
date={date}
date={new Date(date).toISOString()}
practiceGapId="add-practice"
practiceGap={null}
practiceGapsData={practiceGapData}
Expand Down
2 changes: 1 addition & 1 deletion components/LastWeekIssueCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function LastWeekIssueCard({
sourcePastIssue.title,
sourcePastIssue.project,
sourcePastIssue.sig,
date,
new Date().toISOString(),
[sourcePastIssue.id]
);

Expand Down
2 changes: 1 addition & 1 deletion components/LastWeekIssuePane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default function LastWeekIssuePane({
key={`issue-card-${practiceGap.id}`}
project={noteInfo.project}
sig={noteInfo.sigName}
date={noteInfo.sigDate}
date={new Date(noteInfo.sigDate).toISOString()}
practiceGapId={practiceGap.id}
practiceGap={practiceGap}
practiceGapsData={practiceGapData}
Expand Down
9 changes: 5 additions & 4 deletions components/PracticeGapCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function PracticeGapCard({
project,
sig,
'',
date,
new Date(date).toISOString(),
[],
true
);
Expand Down Expand Up @@ -63,7 +63,7 @@ export default function PracticeGapCard({
(i) => i.id === practiceGapId
);
practiceToUpdate[practiceIndex].practiceInactive = true;
practiceToUpdate[practiceIndex].lastUpdated = longDate(new Date());
practiceToUpdate[practiceIndex].lastUpdated = new Date().toISOString();
return practiceToUpdate;
});
};
Expand All @@ -75,7 +75,7 @@ export default function PracticeGapCard({
(i) => i.id === practiceGapId
);
practiceToUpdate[practiceIndex][field] = edits;
practiceToUpdate[practiceIndex].lastUpdated = longDate(new Date());
practiceToUpdate[practiceIndex].lastUpdated = new Date().toISOString();
return practiceToUpdate;
});
};
Expand Down Expand Up @@ -107,7 +107,8 @@ export default function PracticeGapCard({
`[practice gap] ${sourcePractice.title}`
)
);
newCurrentIssuesData[targetIssueIndex].lastUpdated = longDate(new Date());
newCurrentIssuesData[targetIssueIndex].lastUpdated =
new Date().toISOString();

// add issue to practice gap
setPracticeGapsData((prevData) => {
Expand Down
14 changes: 1 addition & 13 deletions controllers/followUpObjects/createFollowUpStrategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,11 @@ export const createPostSigMessage = (
practiceAgents,
orgObjs
) => {
console.log('noteDate', noteDate);
let currDate = new Date(noteDate);
currDate.setHours(currDate.getHours() + 5); // HACK FOR CHICAGO / UTC ISSUES: add 5 hours to avoid timezone issues
let weekFromCurrDate = new Date(currDate.getTime());
weekFromCurrDate.setDate(weekFromCurrDate.getDate() + 7);
let timezone = 'America/Chicago';

// console.log(currDate.toUTCString(), weekFromCurrDate.toUTCString());
// console.log('orignal dates', currDate, weekFromCurrDate);
// console.log(
// 'new timezone dates',
// convertTZ(currDate, timezone),
// convertTZ(weekFromCurrDate, timezone)
// );

let newActiveIssue = {
scriptId: crypto
.createHash('md5')
Expand Down Expand Up @@ -94,11 +84,9 @@ export const createPostSigMessage = (
});
}.toString();

console.log(strategyFunction);

strategyFunction = strategyFunction.replace(
'currDate',
currDate.toUTCString()
currDate.toISOString()
);
strategyFunction = strategyFunction.replace('timezone', timezone);
strategyFunction = strategyFunction.replace(
Expand Down
4 changes: 2 additions & 2 deletions controllers/issueObjects/createIssueObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export const createNewIssueObject = (
title: string,
project: string,
sig: string,
currentDate: Date = new Date(),
currentDate: string = new Date().toISOString(),
priorInstances: string[] = [],
includeId: boolean = false
): IssueObjectStruct => {
// TODO: create new CAP placeholder fields
let newIssueObject: IssueObjectStruct = {
title: title,
date: currentDate,
date: new Date(currentDate),
project: project,
sig: sig,
lastUpdated: new Date(),
Expand Down
6 changes: 3 additions & 3 deletions controllers/practiceGapObjects/createPracticeGapObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ export const createNewPracticeGapObject = (
project: string,
sig: string,
description: string,
currentDate: Date = new Date(),
currentDate: string = new Date().toISOString(),
priorIssues: Types.ObjectId[] = [],
includeId: boolean = false
): PracticeGapObjectStruct => {
let newPracticeGapObject: PracticeGapObjectStruct = {
title: title,
date: currentDate,
date: new Date(currentDate),
project: project,
sig: sig,
description: description,
lastUpdated: currentDate,
lastUpdated: new Date(currentDate),
practiceInactive: false,
practiceArchived: false,
prevIssues: priorIssues
Expand Down
5 changes: 5 additions & 0 deletions controllers/practiceGapObjects/updatePracticeGapObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import {

export const updatePracticeGapObject = async (practiceGapObject: object) => {
await dbConnect();

// get the original practice gap object
let practiceGapObjectId = practiceGapObject['id'];
let originalPracticeGapObject =
await PracticeGapObjectModel.findById(practiceGapObjectId);

// update the practice gap object
// upsert: create a document if it doesn't exist
let updatedPracticeGapObject = await PracticeGapObjectModel.findByIdAndUpdate(
practiceGapObjectId,
practiceGapObject,
Expand Down
11 changes: 5 additions & 6 deletions lib/helperFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export const longDate = (
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: includeSeconds ? 'numeric' : undefined,
timeZone: timezone
second: includeSeconds ? 'numeric' : undefined
});
};

Expand All @@ -32,8 +31,7 @@ export const shortDate = (date: Date, timezone: string = 'America/Chicago') => {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
timeZone: timezone
day: 'numeric'
});
};

Expand All @@ -45,11 +43,12 @@ export const shortDate = (date: Date, timezone: string = 'America/Chicago') => {
export const serializeDates = (object) => {
return {
...object,
date: typeof object.date === 'string' ? object.date : longDate(object.date),
date:
typeof object.date === 'string' ? object.date : object.date.toISOString(),
lastUpdated:
typeof object.lastUpdated === 'string'
? object.lastUpdated
: longDate(object.lastUpdated)
: object.lastUpdated.toISOString()
};
};

Expand Down
2 changes: 1 addition & 1 deletion pages/api/issues/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export default async function handler(
let postSigScript = createPostSigMessage(
noteInfo.id,
noteInfo.project,
new Date(noteInfo.sigDate),
new Date(noteInfo.sigDate).toISOString(),
practiceAgents,
orgObjs
);
Expand Down
29 changes: 22 additions & 7 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ import Link from 'next/link';
import { fetchAllCAPNotes } from '../controllers/capNotes/fetchCAPNotes';
import Head from 'next/head';
import { longDate, shortDate } from '../lib/helperFns';
import { useState } from 'react';
import { useEffect, useState } from 'react';

export default function Home({ sigs }): JSX.Element {
// store state for each SIG on whether to show latest or all CAP notes
const [showAllNotes, setShowAllNotes] = useState(false);

// use a state variable for sigs so dates can be updated to locale time
const [sigsState, setSigsState] = useState(sigs);
useEffect(() => {
setSigsState(
sigs.map((sig) => ({
...sig,
capNotes: sig.capNotes.map((capNote) => ({
...capNote,
date: shortDate(new Date(capNote.date)),
lastUpdated: longDate(new Date(capNote.lastUpdated))
}))
}))
);
}, []);

return (
<>
<Head>
Expand All @@ -33,7 +48,7 @@ export default function Home({ sigs }): JSX.Element {

<div className="w-full col-span-2">
{/* List of SIGs */}
{sigs.map((sig, i) => (
{sigsState.map((sig, i) => (
<div className="w-full mb-10" key={sig.abbreviation}>
<div className="col-span-2">
<h2 className="font-bold text-xl border-b border-black mb-3">
Expand All @@ -55,7 +70,7 @@ export default function Home({ sigs }): JSX.Element {
<Link
href={`/soap-notes/${sig.abbreviation.toLowerCase()}_${encodeURIComponent(
capNote.project
)}_${capNote.date}`}
)}_${new Date(capNote.date).toISOString().split('T')[0]}`}
>
<h3 className="text-md underline text-blue-600 hover:text-blue-800 visited:text-purple-600">
{capNote.project}
Expand Down Expand Up @@ -107,8 +122,8 @@ export const getServerSideProps = async () => {
capNotes: [
{
project: capNote.project,
date: shortDate(capNote.date),
lastUpdated: longDate(capNote.lastUpdated),
date: capNote.date.toISOString(),
lastUpdated: capNote.lastUpdated.toISOString(),
isLatest
}
]
Expand All @@ -117,8 +132,8 @@ export const getServerSideProps = async () => {
// add the CAP note to the SIG's list of CAP notes
acc[sigIndex].capNotes.push({
project: capNote.project,
date: shortDate(capNote.date),
lastUpdated: longDate(capNote.lastUpdated),
date: capNote.date.toISOString(),
lastUpdated: capNote.lastUpdated.toISOString(),
isLatest
});
}
Expand Down
Loading

0 comments on commit eca9444

Please sign in to comment.