Files
sgw-owner-app/utils/tripDataConverters.ts

55 lines
1.4 KiB
TypeScript

import { FishingGear, TripCost } from "@/components/diary/TripFormModal";
/**
* Convert Model.FishingGear[] to component format with id
*/
export function convertFishingGears(
gears: Model.FishingGear[] | undefined,
prefix: string = "gear"
): FishingGear[] {
if (!gears || !Array.isArray(gears)) return [];
return gears.map((gear, index) => ({
id: `${prefix}-${Date.now()}-${index}`,
name: gear.name || "",
number: gear.number?.toString() || "",
}));
}
/**
* Convert Model.TripCost[] to component format with id
*/
export function convertTripCosts(
costs: Model.TripCost[] | undefined,
prefix: string = "cost"
): TripCost[] {
if (!costs || !Array.isArray(costs)) return [];
return costs.map((cost, index) => ({
id: `${prefix}-${Date.now()}-${index}`,
type: cost.type || "",
amount: cost.amount || 0,
unit: cost.unit || "",
cost_per_unit: cost.cost_per_unit || 0,
total_cost: cost.total_cost || 0,
}));
}
/**
* Convert fishing ground codes array to comma-separated string
*/
export function convertFishingGroundCodes(codes: number[] | undefined): string {
if (!codes || !Array.isArray(codes)) return "";
return codes.join(", ");
}
/**
* Parse comma-separated string to number array
*/
export function parseFishingGroundCodes(codesString: string): number[] {
return codesString
.split(",")
.map((code) => parseInt(code.trim()))
.filter((code) => !isNaN(code));
}