2026-02-12 15:42:22 +01:00
|
|
|
// * Other
|
2025-12-01 15:55:32 +01:00
|
|
|
import { Model } from "@/back/types/global";
|
|
|
|
|
|
2026-01-27 16:48:50 +01:00
|
|
|
/**
|
2026-02-12 15:42:22 +01:00
|
|
|
* Extract ids from model relation pivot and format options
|
|
|
|
|
* for a multi select component.
|
2026-01-27 16:48:50 +01:00
|
|
|
*/
|
2026-02-12 15:42:22 +01:00
|
|
|
export function mapPivotOptions<T extends Model, D extends Model>(
|
|
|
|
|
model: T[],
|
2025-12-01 15:55:32 +01:00
|
|
|
data?: D[],
|
2026-02-12 15:42:22 +01:00
|
|
|
valueKey?: keyof T,
|
2026-02-10 15:52:48 +01:00
|
|
|
locale?: string | null,
|
2025-12-01 15:55:32 +01:00
|
|
|
) {
|
2026-02-12 15:42:22 +01:00
|
|
|
const options = model?.length
|
|
|
|
|
? model.map((opt) => {
|
|
|
|
|
const raw = opt[valueKey as keyof T] as unknown;
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
: [];
|
2025-12-17 15:16:21 +01:00
|
|
|
|
2025-12-01 15:55:32 +01:00
|
|
|
return { options, defaultValue };
|
|
|
|
|
}
|
2026-01-27 16:48:50 +01:00
|
|
|
|
2026-02-12 15:42:22 +01:00
|
|
|
export function extractIdsFromDatabasePivotRelation<D extends Model>(
|
|
|
|
|
data?: D[],
|
|
|
|
|
): number[] {
|
|
|
|
|
return data ? data.map((item) => item.id) : [];
|
2026-01-27 16:48:50 +01:00
|
|
|
}
|