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:
Kasper Fabricius Kristensen
2025-02-18 11:28:24 +01:00
committed by GitHub
parent 06cc658246
commit 3b69f5a105
2 changed files with 23 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/dashboard": patch
---
fix(dashboard): Properly delete metadata keys, and fix number parsing

View File

@@ -88,7 +88,7 @@ const InnerForm = <TRes,>({
})
const handleSubmit = form.handleSubmit(async (data) => {
const parsedData = parseValues(data)
const parsedData = parseValues(data, metadata)
await hook(
{
@@ -364,7 +364,8 @@ function getDefaultValues(
}
function parseValues(
values: z.infer<typeof MetadataSchema>
values: z.infer<typeof MetadataSchema>,
original?: Record<string, any> | null
): Record<string, any> | null {
const metadata = values.metadata
@@ -378,12 +379,22 @@ function parseValues(
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) => {
let key = field.key
let value = field.value
const disabled = field.disabled
if (!key || !value) {
if (!key) {
return
}
@@ -393,7 +404,7 @@ function parseValues(
}
key = key.trim()
value = value.trim()
value = value?.trim() ?? ""
// We try to cast the value to a boolean or number if possible
if (value === "true") {
@@ -401,9 +412,9 @@ function parseValues(
} else if (value === "false") {
update[key] = false
} else {
const parsedNumber = parseFloat(value)
if (!isNaN(parsedNumber)) {
update[key] = parsedNumber
const isNumeric = /^-?\d*\.?\d+$/.test(value)
if (isNumeric) {
update[key] = parseFloat(value)
} else {
update[key] = value
}