95 lines
2.7 KiB
TypeScript
95 lines
2.7 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));
|
|
}
|
|
|
|
/**
|
|
* Extract province codes from groups
|
|
* @param groups - Model.GroupResponse containing groups with metadata.code
|
|
* @returns Array of province codes
|
|
*/
|
|
export function getProvinceCodesFromGroups(
|
|
groups: Model.GroupResponse | null | undefined
|
|
): string[] {
|
|
if (!groups?.groups) return [];
|
|
|
|
return groups.groups
|
|
.map((group) => group.metadata?.code)
|
|
.filter((code): code is string => !!code);
|
|
}
|
|
|
|
/**
|
|
* Filter ports by province codes extracted from groups
|
|
* @param ports - Model.PortResponse containing all ports
|
|
* @param groups - Model.GroupResponse to extract province_code from metadata.code
|
|
* @returns Filtered ports that match the province codes from groups
|
|
*/
|
|
export function filterPortsByProvinceCode(
|
|
ports: Model.PortResponse | null | undefined,
|
|
groups: Model.GroupResponse | null | undefined
|
|
): Model.Port[] {
|
|
if (!ports?.ports) return [];
|
|
if (!groups?.groups) return ports.ports; // Return all ports if no groups
|
|
|
|
// Extract province codes from groups
|
|
const provinceCodes = getProvinceCodesFromGroups(groups);
|
|
|
|
// If no province codes found, return all ports
|
|
if (provinceCodes.length === 0) return ports.ports;
|
|
|
|
// Filter ports by province codes
|
|
return ports.ports.filter(
|
|
(port) => port.province_code && provinceCodes.includes(port.province_code)
|
|
);
|
|
}
|