feat: schema builder's create flow
This commit is contained in:
1 parent
f514146fed
commit
083cc9e23e
71 files changed
+7223
-61
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
effectiveDefault,
|
||||
isUnique,
|
||||
supportsLimits,
|
||||
trimmedOptions,
|
||||
type BuilderField,
|
||||
type BuilderForm,
|
||||
} from '@/lib/schemaBuilderModel'
|
||||
|
||||
export interface PayloadField {
|
||||
key: string
|
||||
label: string
|
||||
type: string
|
||||
required: boolean
|
||||
unique: boolean
|
||||
default: string | number | boolean | null
|
||||
min: number | string | null
|
||||
max: number | string | null
|
||||
options: string[]
|
||||
}
|
||||
|
||||
export interface ContentTypePayload {
|
||||
key: string
|
||||
name: string
|
||||
fields: PayloadField[]
|
||||
}
|
||||
|
||||
function limit(field: BuilderField, text: string) {
|
||||
if (!supportsLimits(field.type) || text.trim() === '') {
|
||||
return null
|
||||
}
|
||||
return field.type === 'date' ? text : Number(text)
|
||||
}
|
||||
|
||||
function defaultFor(field: BuilderField) {
|
||||
if (field.type === 'boolean') {
|
||||
return field.defaultOn ? true : null
|
||||
}
|
||||
const given = effectiveDefault(field)
|
||||
if (given === '') {
|
||||
return null
|
||||
}
|
||||
return field.type === 'number' ? Number(given) : given
|
||||
}
|
||||
|
||||
export function payloadField(field: BuilderField): PayloadField {
|
||||
return {
|
||||
key: field.key,
|
||||
label: field.label.trim(),
|
||||
type: field.type,
|
||||
required: field.required,
|
||||
unique: isUnique(field),
|
||||
default: defaultFor(field),
|
||||
min: limit(field, field.min),
|
||||
max: limit(field, field.max),
|
||||
options: field.type === 'select' ? trimmedOptions(field) : [],
|
||||
}
|
||||
}
|
||||
|
||||
export function buildPayload(form: BuilderForm): ContentTypePayload {
|
||||
return {
|
||||
key: form.key,
|
||||
name: form.name.trim(),
|
||||
fields: form.fields.map(payloadField),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user