Move file type handling to a separate function and remove nested ternary

This commit is contained in:
Dean 2024-07-01 21:25:56 +01:00
parent c5b702ab69
commit 52893e4004
1 changed files with 27 additions and 14 deletions

View File

@ -57,6 +57,31 @@
} }
return params return params
} }
const handleMediaUpdate = e => {
const media = e.detail || []
const isSingle =
schema.type === FieldType.ATTACHMENT_SINGLE ||
schema.type === FieldType.SIGNATURE_SINGLE
const parsedMedia = media.map(({ name, value }) => ({
url: name,
filename: value,
}))
if (isSingle) {
const [singleMedia] = parsedMedia
// Return only the first entry
return singleMedia
? {
url: singleMedia.url,
filename: singleMedia.filename,
}
: null
}
// Return the entire array
return parsedMedia
}
</script> </script>
{#if schemaHasOptions(schema) && schema.type !== "array"} {#if schemaHasOptions(schema) && schema.type !== "array"}
@ -185,19 +210,7 @@
on:change={e => { on:change={e => {
onChange({ onChange({
row: { row: {
[field]: [field]: handleMediaUpdate(e),
schema.type === FieldType.ATTACHMENT_SINGLE ||
schema.type === FieldType.SIGNATURE_SINGLE
? e.detail.length > 0
? {
url: e.detail[0].name,
filename: e.detail[0].value,
}
: null
: e.detail.map(({ name, value }) => ({
url: name,
filename: value,
})),
}, },
}) })
}} }}
@ -212,7 +225,7 @@
valuePlaceholder={"Filename"} valuePlaceholder={"Filename"}
actionButtonDisabled={(schema.type === FieldType.ATTACHMENT_SINGLE || actionButtonDisabled={(schema.type === FieldType.ATTACHMENT_SINGLE ||
schema.type === FieldType.SIGNATURE_SINGLE) && schema.type === FieldType.SIGNATURE_SINGLE) &&
Object.keys(fieldData || {}).length >= 1} fieldData}
/> />
</div> </div>
{:else} {:else}