fix(dashboard): Clean Edit Variant form payload of empty strings (#7512)
* fix(dashboard): Clean Edit Variant form paylod of empty strings * fix(dashboard,medusa): Allow passing null to update variant to unset fields * fix product edit form * cleanup * cleanup * pass prop
This commit is contained in:
committed by
GitHub
parent
4483b7980d
commit
e5e5eb6e18
@@ -0,0 +1,48 @@
|
||||
import { castNumber } from "./cast-number"
|
||||
|
||||
export function parseOptionalFormValue<T>(
|
||||
value: T,
|
||||
nullify = true
|
||||
): T | undefined | null {
|
||||
if (typeof value === "string" && value.trim() === "") {
|
||||
return nullify ? null : undefined
|
||||
}
|
||||
|
||||
if (Array.isArray(value) && value.length === 0) {
|
||||
return nullify ? null : undefined
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
type Nullable<T> = { [K in keyof T]: T[K] | null }
|
||||
|
||||
export function parseOptionalFormData<T extends Record<string, unknown>>(
|
||||
data: T,
|
||||
nullify = true
|
||||
): Nullable<T> {
|
||||
return Object.entries(data).reduce((acc, [key, value]) => {
|
||||
return {
|
||||
...acc,
|
||||
[key]: parseOptionalFormValue(value, nullify),
|
||||
}
|
||||
}, {} as Nullable<T>)
|
||||
}
|
||||
|
||||
export function parseOptionalFormNumber(
|
||||
value?: string | number,
|
||||
nullify = true
|
||||
) {
|
||||
if (
|
||||
typeof value === "undefined" ||
|
||||
(typeof value === "string" && value.trim() === "")
|
||||
) {
|
||||
return nullify ? null : undefined
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
return castNumber(value)
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
Reference in New Issue
Block a user