Skip to content

Commit

Permalink
Merge pull request adrianhajdin#28 from billy93/main
Browse files Browse the repository at this point in the history
fix: update formatDateTime to use client timezone before sending sms
  • Loading branch information
adrianhajdin authored Jul 22, 2024
2 parents 145e118 + db29646 commit 4009165
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/actions/appointment.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export const sendSMSNotification = async (userId: string, content: string) => {
export const updateAppointment = async ({
appointmentId,
userId,
timeZone,
appointment,
type,
}: UpdateAppointmentParams) => {
Expand All @@ -134,7 +135,7 @@ export const updateAppointment = async ({

if (!updatedAppointment) throw Error;

const smsMessage = `Greetings from CarePulse. ${type === "schedule" ? `Your appointment is confirmed for ${formatDateTime(appointment.schedule!).dateTime} with Dr. ${appointment.primaryPhysician}` : `We regret to inform that your appointment for ${formatDateTime(appointment.schedule!).dateTime} is cancelled. Reason: ${appointment.cancellationReason}`}.`;
const smsMessage = `Greetings from CarePulse. ${type === "schedule" ? `Your appointment is confirmed for ${formatDateTime(appointment.schedule!, timeZone).dateTime} with Dr. ${appointment.primaryPhysician}` : `We regret to inform that your appointment for ${formatDateTime(appointment.schedule!, timeZone).dateTime} is cancelled. Reason: ${appointment.cancellationReason}`}.`;
await sendSMSNotification(userId, smsMessage);

revalidatePath("/admin");
Expand Down
8 changes: 6 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,38 @@ export const parseStringify = (value: any) => JSON.parse(JSON.stringify(value));
export const convertFileToUrl = (file: File) => URL.createObjectURL(file);

// FORMAT DATE TIME
export const formatDateTime = (dateString: Date | string) => {
export const formatDateTime = (dateString: Date | string, timeZone: string = Intl.DateTimeFormat().resolvedOptions().timeZone) => {
const dateTimeOptions: Intl.DateTimeFormatOptions = {
// weekday: "short", // abbreviated weekday name (e.g., 'Mon')
month: "short", // abbreviated month name (e.g., 'Oct')
day: "numeric", // numeric day of the month (e.g., '25')
year: "numeric", // numeric year (e.g., '2023')
hour: "numeric", // numeric hour (e.g., '8')
minute: "numeric", // numeric minute (e.g., '30')
hour12: true, // use 12-hour clock (true) or 24-hour clock (false)
hour12: true, // use 12-hour clock (true) or 24-hour clock (false),
timeZone: timeZone, // use the provided timezone
};

const dateDayOptions: Intl.DateTimeFormatOptions = {
weekday: "short", // abbreviated weekday name (e.g., 'Mon')
year: "numeric", // numeric year (e.g., '2023')
month: "2-digit", // abbreviated month name (e.g., 'Oct')
day: "2-digit", // numeric day of the month (e.g., '25')
timeZone: timeZone, // use the provided timezone
};

const dateOptions: Intl.DateTimeFormatOptions = {
month: "short", // abbreviated month name (e.g., 'Oct')
year: "numeric", // numeric year (e.g., '2023')
day: "numeric", // numeric day of the month (e.g., '25')
timeZone: timeZone, // use the provided timezone
};

const timeOptions: Intl.DateTimeFormatOptions = {
hour: "numeric", // numeric hour (e.g., '8')
minute: "numeric", // numeric minute (e.g., '30')
hour12: true, // use 12-hour clock (true) or 24-hour clock (false)
timeZone: timeZone, // use the provided timezone
};

const formattedDateTime: string = new Date(dateString).toLocaleString(
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare type CreateAppointmentParams = {
declare type UpdateAppointmentParams = {
appointmentId: string;
userId: string;
timeZone: string;
appointment: Appointment;
type: string;
};

0 comments on commit 4009165

Please sign in to comment.