Files
Ferme/frontend/utils/datatable-formatters.ts

66 lines
2.2 KiB
TypeScript

export const formatBovinShipments = (value: unknown): string => {
if (!Array.isArray(value) || value.length === 0) return '-'
return value.map((item: any) => {
const label = item?.shipmentType?.label ?? item?.shipmentType?.code ??
'Type inconnu'
const qty = item?.nbBovinSend ?? '-'
return `${label} : ${qty}`
}).join(', ')
}
export const formatWeights = (value: unknown): string => {
if (!Array.isArray(value) || value.length === 0) return '-'
return value
.map((item: any) => {
const type = item?.type === 'tare' ? 'Poids à vide': item?.type === 'gross' ? 'Poids à plein': (item?.type ?? 'Poids')
const weight = item?.weight ?? '-'
return `${type}: ${weight}`
})
.join('\n ')
}
export const formatRoleLabels = (
value: unknown,
roleLabelByValue: Map<string, string>,
): string => {
if (!Array.isArray(value) || value.length === 0) {
return ' - '
}
return value
.map((role) => {
const key = String(role)
return roleLabelByValue.get(key) ?? key
})
.join(', ')
}
export const formatAddresses = (value: unknown): string => {
if (!Array.isArray(value) || value.length === 0) {
return " - "
}
if (typeof value[0] === 'string') {
return 'Adresses non chargées'
}
return value
.map((item) => {
if (!item || typeof item !== 'object') return '-'
const address = item as Record<string, unknown>
const street = String(address.street ?? '').trim()
const street2 = String(address.street2 ?? '').trim()
const postalCode = String(address.postalCode ?? '').trim()
const city = String(address.city ?? '').trim()
const countryCode = String(address.countryCode ?? '').trim().toUpperCase()
const firstLine = [street, street2].filter(Boolean).join(', ')
const secondLine = [postalCode, city].filter(Boolean).join(' ')
const finalLine = [firstLine, secondLine, countryCode].filter(Boolean).join(', ')
return finalLine || '-'
})
.join('\n')
}