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

[GG-3938] Fixed date fields prefilling #536

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 10 additions & 6 deletions src/modules/new-request-form/NewRequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export function NewRequestForm({
organizations.length > 0 && organizations[0]?.id
? organizations[0]?.id?.toString()
: null;

const handleChange = useCallback(
(field: Field, value: Field["value"]) => {
setTicketFields(
Expand All @@ -141,13 +142,16 @@ export function NewRequestForm({
setOrganizationField({ ...organizationField, value });
}

function handleDueDateChange(value: string) {
if (dueDateField === null) {
return;
}
const handleDueDateChange = useCallback(
(value: string) => {
if (dueDateField === null) {
return;
}

setDueDateField({ ...dueDateField, value });
}
setDueDateField({ ...dueDateField, value });
},
[dueDateField]
);

return (
<>
Expand Down
70 changes: 49 additions & 21 deletions src/modules/new-request-form/fields/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { Span } from "@zendeskgarden/react-typography";
import type { Field } from "../data-types";
import type { ChangeEventHandler } from "react";
import { useState } from "react";
import { useCallback, useState } from "react";

interface DatePickerProps {
field: Field;
Expand All @@ -29,25 +29,48 @@
value ? new Date(value as string) : undefined
);

const formatDate = (value: Date | undefined) => {
if (value === undefined) {
return "";
}
const isoString = value.toISOString();
return valueFormat === "dateTime" ? isoString : isoString.split("T")[0];
};
/* Formats the date using the UTC time zone to prevent timezone-related issues.
* By creating a new Date object with only the date, the time defaults to 00:00:00 UTC.
* This avoids date shifts that can occur if formatted with the local time zone, as Garden does by default.
*/
const formatDateInput = useCallback(
(date: Date) => {
const dateTimeFormat = new Intl.DateTimeFormat(locale, {
month: "long",
day: "numeric",
year: "numeric",
timeZone: "UTC",
});
return dateTimeFormat.format(date);
},
[locale]
);

const handleChange = (date: Date) => {
// Set the time to 12:00:00 as this is also the expected behavior across Support and the API
const newDate = new Date(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0)
) as Date;
setDate(newDate);
const dateString = formatDate(newDate);
if (dateString !== undefined) {
onChange(dateString);
}
};
const formatDateValue = useCallback(
(value: Date | undefined) => {
if (value === undefined) {
return "";
}
const isoString = value.toISOString();
return valueFormat === "dateTime" ? isoString : isoString.split("T")[0];
},
[valueFormat]
);

const handleChange = useCallback(
(date: Date) => {
// Set the time to 12:00:00 as this is also the expected behavior across Support and the API
const newDate = new Date(
Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), 12, 0, 0)
) as Date;
setDate(newDate);
const dateString = formatDateValue(newDate);
if (dateString !== undefined) {
onChange(dateString);
}
},
[onChange, formatDateValue]
);

const handleInputChange: ChangeEventHandler<HTMLInputElement> = (e) => {
// Allow field to be cleared
Expand All @@ -61,12 +84,17 @@
<GardenField>
<Label>
{label}
{required && <Span aria-hidden="true">*</Span>}

Check warning on line 87 in src/modules/new-request-form/fields/DatePicker.tsx

View workflow job for this annotation

GitHub Actions / Lint JS files

Do not use hardcoded content as the children of the Span component
</Label>
{description && (
<Hint dangerouslySetInnerHTML={{ __html: description }} />
)}
<GardenDatepicker value={date} onChange={handleChange} locale={locale}>
<GardenDatepicker
value={date}
onChange={handleChange}
formatDate={formatDateInput}
locale={locale}
>
<Input
required={required}
lang={locale}
Expand All @@ -75,7 +103,7 @@
/>
</GardenDatepicker>
{error && <Message validation="error">{error}</Message>}
<input type="hidden" name={name} value={formatDate(date)} />
<input type="hidden" name={name} value={formatDateValue(date)} />
</GardenField>
);
}
21 changes: 21 additions & 0 deletions src/modules/new-request-form/usePrefilledTicketFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DOMPurify from "dompurify";

const MAX_URL_LENGTH = 2048;
const TICKET_FIELD_PREFIX = "tf_";
const DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;

const ALLOWED_BOOLEAN_VALUES = ["true", "false"];
const ALLOWED_HTML_TAGS = [
Expand Down Expand Up @@ -57,6 +58,20 @@ function getFieldFromId(id: string, prefilledTicketFields: Fields) {
}
}

function isValidDate(dateString: string) {
if (!DATE_REGEX.test(dateString)) {
return false;
}

const date = new Date(dateString);
const [year, month, day] = dateString.split("-").map(Number);
return (
date.getUTCFullYear() === year &&
date.getUTCMonth() + 1 === month &&
date.getUTCDate() === day
);
}

function getPrefilledTicketFields(fields: Fields): Fields {
const { href } = location;
const params = new URL(href).searchParams;
Expand Down Expand Up @@ -102,6 +117,12 @@ function getPrefilledTicketFields(fields: Fields): Fields {
: "";
}
break;
case "due_at":
case "date":
if (isValidDate(sanitizedValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is not valid we just won't prefill the field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What else can we do, a part eventually showing a message in the console?

field.value = sanitizedValue;
}
break;
default:
field.value = sanitizedValue;
}
Expand Down
Loading