Skip to content

Commit

Permalink
Merge pull request #1077 from Shelf-nu/1073-bug-to-check-if-bug-time-…
Browse files Browse the repository at this point in the history
…zone-issues-with-date-picker-field

fix: making sure that values of date custom fields are properly formatted …
  • Loading branch information
DonKoko authored Jun 19, 2024
2 parents 71f6a7a + 4899ad5 commit d120cd8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 6 additions & 5 deletions app/routes/_layout+/assets.$assetId.overview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { getScanByQrId } from "~/modules/scan/service.server";
import { parseScanData } from "~/modules/scan/utils.server";
import { appendToMetaTitle } from "~/utils/append-to-meta-title";
import { checkExhaustiveSwitch } from "~/utils/check-exhaustive-switch";
import { getDateTimeFormat, getLocale } from "~/utils/client-hints";
import { getClientHint, getDateTimeFormat } from "~/utils/client-hints";
import { getCustomFieldDisplayValue } from "~/utils/custom-fields";
import { sendNotification } from "~/utils/emitter/send-notification.server";
import { makeShelfError } from "~/utils/error";
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function loader({ context, request, params }: LoaderFunctionArgs) {
action: PermissionAction.read,
});

const locale = getLocale(request);
const { locale, timeZone } = getClientHint(request);

const asset = await getAsset({
id,
Expand Down Expand Up @@ -172,6 +172,7 @@ export async function loader({ context, request, params }: LoaderFunctionArgs) {
lastScan,
header,
locale,
timeZone,
qrObj,
})
);
Expand Down Expand Up @@ -229,8 +230,7 @@ export async function action({ context, request, params }: ActionFunctionArgs) {
}

export default function AssetOverview() {
const { asset, locale, qrObj } = useLoaderData<typeof loader>();

const { asset, locale, timeZone, qrObj } = useLoaderData<typeof loader>();
const booking = asset?.bookings?.length ? asset?.bookings[0] : undefined;

const customFieldsValues =
Expand Down Expand Up @@ -358,7 +358,8 @@ export default function AssetOverview() {
<ul className="item-information">
{customFieldsValues.map((field, _index) => {
const customFieldDisplayValue = getCustomFieldDisplayValue(
field.value as unknown as ShelfAssetCustomFieldValueType["value"]
field.value as unknown as ShelfAssetCustomFieldValueType["value"],
{ locale, timeZone }
);
return (
<li
Expand Down
9 changes: 7 additions & 2 deletions app/utils/custom-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { format } from "date-fns";
import type { ZodRawShape } from "zod";
import { z } from "zod";
import type { ShelfAssetCustomFieldValueType } from "~/modules/asset/types";
import type { ClientHint } from "~/modules/booking/types";
import { getDateTimeFormatFromHints } from "./client-hints";

/** Returns the schema depending on the field type.
* Also handles the required field error message.
Expand Down Expand Up @@ -164,10 +166,13 @@ export const buildCustomFieldValue = (
};

export const getCustomFieldDisplayValue = (
value: ShelfAssetCustomFieldValueType["value"]
value: ShelfAssetCustomFieldValueType["value"],
hints?: ClientHint
): string => {
if (value.valueDate) {
return format(new Date(value.valueDate), "PPP");
return hints
? getDateTimeFormatFromHints(hints).format(new Date(value.valueDate))
: format(new Date(value.valueDate), "PPP"); // Fallback to default date format
}
return String(value.raw);
};
Expand Down

0 comments on commit d120cd8

Please sign in to comment.