Add supplier and imporve inventory frontend
This commit is contained in:
109
frontend/src/utils/foodSafetyEnumHelpers.ts
Normal file
109
frontend/src/utils/foodSafetyEnumHelpers.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
// frontend/src/utils/foodSafetyEnumHelpers.ts
|
||||
/**
|
||||
* Utilities for working with food safety enums and translations
|
||||
*/
|
||||
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import type { SelectOption } from '../components/ui/Select';
|
||||
import {
|
||||
FoodSafetyStandard,
|
||||
ComplianceStatus,
|
||||
FoodSafetyAlertType,
|
||||
type EnumOption
|
||||
} from '../api/types/foodSafety';
|
||||
|
||||
/**
|
||||
* Generic function to convert enum to select options with i18n translations
|
||||
*/
|
||||
export function enumToSelectOptions<T extends Record<string, string | number>>(
|
||||
enumObject: T,
|
||||
translationKey: string,
|
||||
t: (key: string) => string,
|
||||
options?: {
|
||||
includeDescription?: boolean;
|
||||
descriptionKey?: string;
|
||||
sortAlphabetically?: boolean;
|
||||
}
|
||||
): SelectOption[] {
|
||||
const selectOptions = Object.entries(enumObject).map(([, value]) => ({
|
||||
value,
|
||||
label: t(`${translationKey}.${value}`),
|
||||
...(options?.includeDescription && options?.descriptionKey && {
|
||||
description: t(`${options.descriptionKey}.${value}`)
|
||||
})
|
||||
}));
|
||||
|
||||
if (options?.sortAlphabetically) {
|
||||
selectOptions.sort((a, b) => a.label.localeCompare(b.label));
|
||||
}
|
||||
|
||||
return selectOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook for food safety enum utilities
|
||||
*/
|
||||
export function useFoodSafetyEnums() {
|
||||
const { t } = useTranslation('foodSafety');
|
||||
|
||||
return {
|
||||
// Food Safety Standard
|
||||
getFoodSafetyStandardOptions: (): SelectOption[] =>
|
||||
enumToSelectOptions(FoodSafetyStandard, 'enums.food_safety_standard', t, {
|
||||
includeDescription: true,
|
||||
descriptionKey: 'descriptions',
|
||||
sortAlphabetically: true
|
||||
}),
|
||||
|
||||
getFoodSafetyStandardLabel: (standard: FoodSafetyStandard): string =>
|
||||
t(`enums.food_safety_standard.${standard}`),
|
||||
|
||||
// Compliance Status
|
||||
getComplianceStatusOptions: (): SelectOption[] =>
|
||||
enumToSelectOptions(ComplianceStatus, 'enums.compliance_status', t, {
|
||||
includeDescription: true,
|
||||
descriptionKey: 'descriptions'
|
||||
}),
|
||||
|
||||
getComplianceStatusLabel: (status: ComplianceStatus): string =>
|
||||
t(`enums.compliance_status.${status}`),
|
||||
|
||||
// Food Safety Alert Type
|
||||
getFoodSafetyAlertTypeOptions: (): SelectOption[] =>
|
||||
enumToSelectOptions(FoodSafetyAlertType, 'enums.food_safety_alert_type', t, {
|
||||
includeDescription: true,
|
||||
descriptionKey: 'descriptions',
|
||||
sortAlphabetically: true
|
||||
}),
|
||||
|
||||
getFoodSafetyAlertTypeLabel: (type: FoodSafetyAlertType): string =>
|
||||
t(`enums.food_safety_alert_type.${type}`),
|
||||
|
||||
// Field Labels
|
||||
getFieldLabel: (field: string): string =>
|
||||
t(`labels.${field}`),
|
||||
|
||||
getFieldDescription: (field: string): string =>
|
||||
t(`descriptions.${field}`)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to get enum value from select option value
|
||||
*/
|
||||
export function getEnumFromValue<T>(
|
||||
enumObject: Record<string, T>,
|
||||
value: string | number
|
||||
): T | undefined {
|
||||
return Object.values(enumObject).find(enumValue => enumValue === value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility to validate enum value
|
||||
*/
|
||||
export function isValidEnumValue<T>(
|
||||
enumObject: Record<string, T>,
|
||||
value: unknown
|
||||
): value is T {
|
||||
return Object.values(enumObject).includes(value as T);
|
||||
}
|
||||
Reference in New Issue
Block a user