Skip to content

Commit

Permalink
Merge pull request #518 from powerhouse-inc/document-model-libs/chang…
Browse files Browse the repository at this point in the history
…e-rwa-date-picker

feat(design-system): use intl format for datetime inputs
  • Loading branch information
ryanwolhuter authored Nov 13, 2024
2 parents 30eed31 + d51f809 commit fee9af5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 104 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion packages/design-system/src/connect/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./animated-loader";
export * from "./breadcrumbs";
export * from "./combobox";
export * from "./cookie-banner";
export * from "./date-time-input";
export * from "./default-editor-loader";
export * from "./disclosure";
export * from "./divider";
Expand Down
12 changes: 8 additions & 4 deletions packages/design-system/src/rwa/hooks/useDefaultFormValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TableItemType,
TableName,
} from "../types";
import { convertToDateTimeLocalFormat } from "../utils";
import { isoDateStringToDateInput } from "../utils";

export function useDefaultFormValues(args: {
tableName: TableName;
Expand All @@ -40,7 +40,7 @@ export function useDefaultFormValues(args: {
case tableNames.TRANSACTION:
return {
type: allGroupTransactionTypes[0],
entryTime: convertToDateTimeLocalFormat(new Date()),
entryTime: isoDateStringToDateInput(new Date().toISOString(), true),
cashAmount: null,
fixedIncomeId: null,
fixedIncomeAmount: null,
Expand Down Expand Up @@ -78,7 +78,9 @@ export function useDefaultFormValues(args: {
fixedIncomeTypeId: item.fixedIncomeTypeId,
spvId: item.spvId,
name: item.name,
maturity: item.maturity,
maturity: item.maturity
? isoDateStringToDateInput(item.maturity)
: null,
ISIN: item.ISIN,
CUSIP: item.CUSIP,
coupon: item.coupon,
Expand All @@ -89,7 +91,9 @@ export function useDefaultFormValues(args: {
return {
id: item.id,
type: item.type,
entryTime: convertToDateTimeLocalFormat(item.entryTime),
entryTime: item.entryTime
? isoDateStringToDateInput(item.entryTime, true)
: null,
cashAmount: item.cashTransaction.amount ?? null,
fixedIncomeId: item.fixedIncomeTransaction?.assetId ?? null,
fixedIncomeAmount: item.fixedIncomeTransaction?.amount ?? null,
Expand Down
32 changes: 12 additions & 20 deletions packages/design-system/src/rwa/hooks/useFormInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DateTimeLocalInput } from "@/connect";
import {
Account,
allGroupTransactionTypes,
Expand Down Expand Up @@ -67,7 +66,6 @@ export function useFormInputs(props: Props): {
fixedIncomes,
} = useEditorContext();
const { showModal } = useModal();

const showCreateItemModal = useCallback(
(tableName: TableName) => () => {
showModal("createItem", { tableName });
Expand All @@ -81,9 +79,9 @@ export function useFormInputs(props: Props): {
const tableItem = _tableItem as TableItemType<"ASSET"> | null;
type Payload = FormInputsByTableName["ASSET"];
const register = _register as UseFormRegister<Payload>;
const watch = _watch as UseFormWatch<Payload>;
const formState = _formState as FormState<Payload>;
const { errors } = formState;

const control = _control as Control<Payload>;
const derivedInputsToDisplay =
operation !== "create" && !!tableItem
Expand Down Expand Up @@ -111,11 +109,6 @@ export function useFormInputs(props: Props): {
]
: [];

const maturityInputValue =
operation === "view" && tableItem
? tableItem.maturity
: watch("maturity") || tableItem?.maturity;

const inputs = [
{
label: "Asset Name",
Expand Down Expand Up @@ -189,20 +182,17 @@ export function useFormInputs(props: Props): {
{
label: "Maturity",
Input: () => (
<DateTimeLocalInput
<input
className="h-8 w-full rounded-md bg-gray-100 px-3 disabled:bg-transparent disabled:p-0"
step={1}
type="date"
{...register("maturity", {
disabled: operation === "view",
setValueAs: (value) => {
if (value === "") return null;
return value as string;
},
})}
inputType="date"
name="maturity"
/>
),
inputLabel: maturityInputValue
? formatDateForDisplay(maturityInputValue, true)
inputLabel: tableItem?.maturity
? formatDateForDisplay(tableItem.maturity, true)
: null,
},
{
Expand Down Expand Up @@ -302,12 +292,14 @@ export function useFormInputs(props: Props): {
{
label: "Entry Time",
Input: () => (
<DateTimeLocalInput
<input
className="h-8 w-full rounded-md bg-gray-100 px-3 disabled:bg-transparent disabled:p-0"
step={1}
type="datetime-local"
{...register("entryTime", {
required: true,
disabled: operation === "view",
required: "Entry time is required",
})}
name="entryTime"
/>
),
inputLabel: entryTimeInputValue
Expand Down
23 changes: 14 additions & 9 deletions packages/design-system/src/rwa/utils/date.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import { formatInTimeZone } from "date-fns-tz/formatInTimeZone";
import { format } from "date-fns/format";

/**
* The html datetime local input requires this specific format
*/
export function convertToDateTimeLocalFormat(date: Date | string = new Date()) {
return format(date, "yyyy-MM-dd'T'HH:mm");
}
export function formatDateForDisplay(date: Date | string, displayTime = true) {
const formatString = displayTime ? "yyyy/MM/dd, HH:mm:ss zzz" : "yyyy/MM/dd";

return formatInTimeZone(date, "UTC", formatString);
return formatInTimeZone(date, getTimeZone(), formatString);
}

export function isISODate(str: string) {
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) return false;
const d = new Date(str);
return d instanceof Date && !isNaN(d.getTime()) && d.toISOString() === str;
}

export function getTimeZone() {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
}

export function isoDateStringToDateInput(
isoDateString: string,
withTime = false,
) {
const format = withTime ? "yyyy-MM-dd'T'HH:mm:ss.SSS" : "yyyy-MM-dd";
const timeZone = getTimeZone();
return formatInTimeZone(isoDateString, timeZone, format);
}
2 changes: 1 addition & 1 deletion packages/document-model-libs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"@monaco-editor/react": "^4.6.0",
"@mui/material": "^5.15.5",
"@powerhousedao/codegen": "0.14.1",
"@powerhousedao/design-system": "^1.7.2",
"@powerhousedao/design-system": "workspace:*",
"@powerhousedao/scalars": "^1.9.0",
"@prettier/sync": "^0.5.2",
"@radix-ui/react-checkbox": "^1.1.2",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fee9af5

Please sign in to comment.