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

Fix Date Display in Add Event #673

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ const EventDetailsSchedulingTab = ({
// tabIndex={1}
value={new Date(formik.values.scheduleStartDate)}
onChange={(value: Date | null) =>
changeStartDate(
value && changeStartDate(
value,
formik.values,
formik.setFieldValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ const NewSourcePage = <T extends RequiredFormProps>({
className="source-toggle"
onClick={() =>
changeStartDate(
formik.values.scheduleStartDate,
new Date(formik.values.scheduleStartDate),
formik.values,
formik.setFieldValue
)
Expand Down Expand Up @@ -438,7 +438,7 @@ const Schedule = <T extends {
formik.setFieldValue
);
} else {
changeStartDate(
value && changeStartDate(
value,
formik.values,
formik.setFieldValue
Expand Down
8 changes: 5 additions & 3 deletions src/components/events/partials/wizards/NewEventSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ const NewEventSummary = <T extends RequiredFormProps>({
(workflow) => workflow.id === formik.values.processingWorkflow
);

const endsOnSameDay = formik.values.scheduleStartDate === formik.values.scheduleEndDate;

return (
<>
<div className="modal-content">
Expand Down Expand Up @@ -181,8 +183,8 @@ const NewEventSummary = <T extends RequiredFormProps>({
{t("EVENTS.EVENTS.NEW.SOURCE.DATE_TIME.START_DATE")}
</td>
<td>
{t("dateFormats.date.short", {
date: renderValidDate(formik.values.startDate),
{t("dateFormats.dateTime.short", {
dateTime: renderValidDate(formik.values.startDate),
})}
</td>
</tr>
Expand Down Expand Up @@ -214,7 +216,7 @@ const NewEventSummary = <T extends RequiredFormProps>({
{formik.values.scheduleStartMinute}
</td>
</tr>
{formik.values.sourceMode === "SCHEDULE_MULTIPLE" && (
{(!endsOnSameDay || formik.values.sourceMode === "SCHEDULE_MULTIPLE") && (
<tr>
<td>
{t("EVENTS.EVENTS.NEW.SOURCE.DATE_TIME.END_DATE")}
Expand Down
66 changes: 19 additions & 47 deletions src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { makeTwoDigits } from "./utils";
* This File contains methods concerning dates
*/

// Get the ISO date string based on local time
const getISODateString = (date: Date) => {
const offsetHours = Math.trunc(date.getTimezoneOffset() / 60)
const offsetMinutes = date.getTimezoneOffset() % 60
return new Date(date.setHours(-offsetHours, -offsetMinutes, 0)).toISOString().substring(0, 10);
}
lkiesow marked this conversation as resolved.
Show resolved Hide resolved

// check if date can be parsed
export const renderValidDate = (date: string) => {
return !isNaN(Date.parse(date)) ? new Date(date) : ""
return !isNaN(Date.parse(date)) ? new Date(date) : ""
}

// transform relative date to an absolute date
Expand Down Expand Up @@ -107,14 +114,8 @@ const changeStart = (
}

setDuration(startDate, endDate, setFieldValue);
setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue(
"scheduleStartDate",
new Date(startDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));
setFieldValue("scheduleStartDate", getISODateString(startDate));

if (!!checkConflicts) {
checkConflicts(
Expand All @@ -127,8 +128,7 @@ const changeStart = (
};

export const changeStartDate = (
// @ts-expect-error TS(7006): Parameter 'value' implicitly has an 'any' type.
value,
value: Date,
// @ts-expect-error TS(7006): Parameter 'formikValues' implicitly has an 'any' t... Remove this comment to see the full error message
formikValues,
// @ts-expect-error TS(7006): Parameter 'setFieldValue' implicitly has an 'any' ... Remove this comment to see the full error message
Expand Down Expand Up @@ -227,10 +227,7 @@ const changeEnd = (
}

setDuration(startDate, endDate, setFieldValue);
setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));

if (!!checkConflicts) {
checkConflicts(
Expand Down Expand Up @@ -314,10 +311,7 @@ const changeDuration = (

setFieldValue("scheduleEndHour", makeTwoDigits(endDate.getHours()));
setFieldValue("scheduleEndMinute", makeTwoDigits(endDate.getMinutes()));
setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));

if (!!checkConflicts) {
checkConflicts(
Expand Down Expand Up @@ -412,18 +406,8 @@ const changeStartMultiple = (
endDate.setDate(startDate.getDate() + 1);
}

setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue(
"scheduleStartDate",
new Date(startDate.setHours(0, 0, 0)).toISOString()
);

if (isEndBeforeStart(startDate, endDate)) {
endDate.setDate(startDate.getDate() + 1);
}
setFieldValue("scheduleEndDate", getISODateString(endDate));
setFieldValue("scheduleStartDate", getISODateString(startDate));

if (!!checkConflicts) {
checkConflicts(
Expand Down Expand Up @@ -537,14 +521,8 @@ export const changeEndDateMultiple = async (
}
}

setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue(
"scheduleStartDate",
new Date(startDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));
setFieldValue("scheduleStartDate", getISODateString(startDate));

if (!!checkConflicts) {
checkConflicts(
Expand Down Expand Up @@ -585,10 +563,7 @@ const changeEndMultiple = (

if (isEndBeforeStart(startDate, endDate)) {
endDate.setDate(startDate.getDate() + 1);
setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));
}

if (!!checkConflicts) {
Expand Down Expand Up @@ -677,10 +652,7 @@ const changeDurationMultiple = (

setFieldValue("scheduleEndHour", makeTwoDigits(endDate.getHours()));
setFieldValue("scheduleEndMinute", makeTwoDigits(endDate.getMinutes()));
setFieldValue(
"scheduleEndDate",
new Date(endDate.setHours(0, 0, 0)).toISOString()
);
setFieldValue("scheduleEndDate", getISODateString(endDate));

if (!!checkConflicts) {
checkConflicts(
Expand Down
Loading