ldc-ovoteam/resources/ts/lib/mapPivotOptions.tsx

39 lines
959 B
TypeScript
Raw Permalink Normal View History

import type { DefaultModel } from "@/back/types/global";
2026-03-30 14:17:31 +02:00
/**
* Extract ids from model relation pivot and format options
* for a multi select component.
*/
export function mapPivotOptions<T extends DefaultModel>(
2026-03-30 14:17:31 +02:00
model: T[],
data?: DefaultModel[],
2026-03-30 14:17:31 +02:00
valueKey?: keyof T,
locale?: string | null,
) {
const options = model.length
2026-03-30 14:17:31 +02:00
? model.map((opt) => {
const raw = valueKey ? (opt[valueKey] as unknown) : null;
2026-03-30 14:17:31 +02:00
return {
value: opt.id.toString(),
label:
locale && typeof raw === "object" && raw !== null
? (raw as Record<string, string>)[locale]
: (raw as string),
};
})
: [];
const defaultValue = data?.length
? data.map((item) => item.id.toString())
: [];
return { options, defaultValue };
}
export function extractIdsFromDatabasePivotRelation(
data?: DefaultModel[],
2026-03-30 14:17:31 +02:00
): number[] {
return data ? data.map((item) => item.id) : [];
}