fix(dashboard): Properly delete metadata keys, and fix number parsing (#11466)
**What** - Fixes an issue where we would parse a string like `"52 tests"` into `52` in the Metadata form - If a row is deleted we now send off key with an empty string as its value to signify that it should be deleted. This is needed to introduce the API we had for updating metadata in V1. Adrien is implementing the BE support for this. RESOLVES SUP-895
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/dashboard": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix(dashboard): Properly delete metadata keys, and fix number parsing
|
||||||
@@ -88,7 +88,7 @@ const InnerForm = <TRes,>({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const handleSubmit = form.handleSubmit(async (data) => {
|
const handleSubmit = form.handleSubmit(async (data) => {
|
||||||
const parsedData = parseValues(data)
|
const parsedData = parseValues(data, metadata)
|
||||||
|
|
||||||
await hook(
|
await hook(
|
||||||
{
|
{
|
||||||
@@ -364,7 +364,8 @@ function getDefaultValues(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function parseValues(
|
function parseValues(
|
||||||
values: z.infer<typeof MetadataSchema>
|
values: z.infer<typeof MetadataSchema>,
|
||||||
|
original?: Record<string, any> | null
|
||||||
): Record<string, any> | null {
|
): Record<string, any> | null {
|
||||||
const metadata = values.metadata
|
const metadata = values.metadata
|
||||||
|
|
||||||
@@ -378,12 +379,22 @@ function parseValues(
|
|||||||
|
|
||||||
const update: Record<string, any> = {}
|
const update: Record<string, any> = {}
|
||||||
|
|
||||||
|
// First, handle removed keys from original
|
||||||
|
if (original) {
|
||||||
|
Object.keys(original).forEach((originalKey) => {
|
||||||
|
const exists = metadata.some((field) => field.key === originalKey)
|
||||||
|
if (!exists) {
|
||||||
|
update[originalKey] = ""
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
metadata.forEach((field) => {
|
metadata.forEach((field) => {
|
||||||
let key = field.key
|
let key = field.key
|
||||||
let value = field.value
|
let value = field.value
|
||||||
const disabled = field.disabled
|
const disabled = field.disabled
|
||||||
|
|
||||||
if (!key || !value) {
|
if (!key) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +404,7 @@ function parseValues(
|
|||||||
}
|
}
|
||||||
|
|
||||||
key = key.trim()
|
key = key.trim()
|
||||||
value = value.trim()
|
value = value?.trim() ?? ""
|
||||||
|
|
||||||
// We try to cast the value to a boolean or number if possible
|
// We try to cast the value to a boolean or number if possible
|
||||||
if (value === "true") {
|
if (value === "true") {
|
||||||
@@ -401,9 +412,9 @@ function parseValues(
|
|||||||
} else if (value === "false") {
|
} else if (value === "false") {
|
||||||
update[key] = false
|
update[key] = false
|
||||||
} else {
|
} else {
|
||||||
const parsedNumber = parseFloat(value)
|
const isNumeric = /^-?\d*\.?\d+$/.test(value)
|
||||||
if (!isNaN(parsedNumber)) {
|
if (isNumeric) {
|
||||||
update[key] = parsedNumber
|
update[key] = parseFloat(value)
|
||||||
} else {
|
} else {
|
||||||
update[key] = value
|
update[key] = value
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user