From c35869e8da220fef5fd18b0792d57e8296dd8d87 Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 13 Sep 2024 12:44:47 +0000 Subject: [PATCH 1/2] fix: prepare arg --- src/utils/api.tsx | 13 +++++++++++++ src/utils/form.ts | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/utils/api.tsx b/src/utils/api.tsx index dbacaec..ed18c31 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -326,3 +326,16 @@ export function handleResultState( // Have yet to call the API. return loadingNode } + +export function prepareArg(arg: unknown): void { + if (typeof arg === "object" && arg !== null) { + const _arg = arg as Record + for (const property in _arg) { + let value = _arg[property] + if (value instanceof Date) value = value.toISOString() + else if (typeof value === "object" && value !== null) prepareArg(value) + + _arg[property] = value + } + } +} diff --git a/src/utils/form.ts b/src/utils/form.ts index 954d1d7..45f8ac3 100644 --- a/src/utils/form.ts +++ b/src/utils/form.ts @@ -3,6 +3,7 @@ import type { FieldValidator, FormikHelpers } from "formik" import { ValidationError, type Schema, type ValidateOptions } from "yup" import { excludeKeyPaths } from "./general" +import { prepareArg } from "./api" export function isFormError(error: unknown): boolean { return ( @@ -81,6 +82,8 @@ export function submitForm< if (exclude) arg = excludeKeyPaths(arg, exclude) + prepareArg(arg) + trigger(arg) .unwrap() .then(result => { From b32fd6bc7cf806abc939540836616e7874df238e Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 13 Sep 2024 13:09:57 +0000 Subject: [PATCH 2/2] fix: don't unpack date --- src/utils/api.tsx | 13 ------------- src/utils/form.ts | 3 --- src/utils/general.ts | 9 +++++++-- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/utils/api.tsx b/src/utils/api.tsx index ed18c31..dbacaec 100644 --- a/src/utils/api.tsx +++ b/src/utils/api.tsx @@ -326,16 +326,3 @@ export function handleResultState( // Have yet to call the API. return loadingNode } - -export function prepareArg(arg: unknown): void { - if (typeof arg === "object" && arg !== null) { - const _arg = arg as Record - for (const property in _arg) { - let value = _arg[property] - if (value instanceof Date) value = value.toISOString() - else if (typeof value === "object" && value !== null) prepareArg(value) - - _arg[property] = value - } - } -} diff --git a/src/utils/form.ts b/src/utils/form.ts index 45f8ac3..954d1d7 100644 --- a/src/utils/form.ts +++ b/src/utils/form.ts @@ -3,7 +3,6 @@ import type { FieldValidator, FormikHelpers } from "formik" import { ValidationError, type Schema, type ValidateOptions } from "yup" import { excludeKeyPaths } from "./general" -import { prepareArg } from "./api" export function isFormError(error: unknown): boolean { return ( @@ -82,8 +81,6 @@ export function submitForm< if (exclude) arg = excludeKeyPaths(arg, exclude) - prepareArg(arg) - trigger(arg) .unwrap() .then(result => { diff --git a/src/utils/general.ts b/src/utils/general.ts index 0357e7e..c9ed3a8 100644 --- a/src/utils/general.ts +++ b/src/utils/general.ts @@ -478,10 +478,15 @@ export function excludeKeyPaths( function _excludeKeyPaths(obj: object, path: string[]) { return Object.fromEntries( Object.entries(obj) - .map(([key, value]) => { + .map(([key, value]: [string, unknown]) => { const _path = [...path, key] - if (typeof value === "object") value = _excludeKeyPaths(value, _path) + if ( + typeof value === "object" && + value !== null && + !(value instanceof Date) + ) + value = _excludeKeyPaths(value, _path) return exclude.includes(_path.join(delimiter)) ? [] : [key, value] })