Files
simrsx-fe/app/lib/utils.ts
Khafid Prayoga 55239606af feat(patient): address integration to backend apis
feat(patient): add newborn status field and validation

- Add radio button component for newborn status selection
- Update patient schema with newborn status validation
- Remove deprecated alias field from person model
- Refactor disability type handling in patient schema

fix(patient): correct address comparison logic and schema

Update the patient address comparison to use boolean instead of string '1' and modify the schema to transform the string value to boolean. This ensures consistent type usage throughout the application.

feat(models): add village and district model interfaces

Add new model interfaces for Village and District with their respective generator functions. These models will be used to handle administrative division data in the application.

feat(address): implement dynamic province selection with caching

- Add province service for CRUD operations
- Create useProvinces composable with caching and loading states
- Update select-province component to use dynamic data
- Export SelectItem interface for type consistency
- Improve combobox styling and accessibility

feat(address-form): implement dynamic regency selection with caching

- Add new regency service for CRUD operations
- Create useRegencies composable with caching and loading states
- Update SelectRegency component to use dynamic data based on province selection
- Improve placeholder and disabled state handling

feat(address-form): implement dynamic district selection

- Add district service for CRUD operations
- Create useDistricts composable with caching and loading states
- Update SelectDistrict component to use dynamic data
- Remove hardcoded district options and implement regency-based filtering

feat(address-form): implement dynamic village selection with caching

- Add village service for CRUD operations
- Create useVillages composable with caching and loading states
- Update SelectVillage component to fetch villages based on district
- Remove hardcoded village options in favor of API-driven data

feat(address-form): improve address selection with debouncing and request deduplication

- Add debouncing to prevent rapid API calls when selecting addresses
- Implement request deduplication to avoid duplicate API calls
- Add delayed form reset to ensure proper composable cleanup
- Add isUserAction flag to force refresh when user changes selection
2025-10-08 15:58:22 +07:00

106 lines
2.5 KiB
TypeScript

import type { ClassValue } from 'clsx'
import { clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export interface SelectOptionType<_T = string> {
value: string
label: string
code?: string
}
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function mapToComboboxOptList(items: Record<string, string>): SelectOptionType<string>[] {
if (!items) {
return []
}
const result: SelectOptionType<string>[] = []
Object.keys(items).forEach((item) => {
result.push({
label: items[item] as string,
value: item,
code: item,
})
})
return result
}
/**
* Mengkonversi string menjadi title case (huruf pertama setiap kata kapital)
* @param str - String yang akan dikonversi
* @returns String dalam format title case
*/
export function toTitleCase(str: string): string {
return str.toLowerCase().replace(/\b\w/g, (char) => char.toUpperCase())
}
/**
* Menghitung umur berdasarkan tanggal lahir
* @param birthDate - Tanggal lahir dalam format Date atau string
* @returns String umur dalam format "X tahun Y bulan" atau "X bulan" untuk bayi
*/
export function calculateAge(birthDate: Date | string | null | undefined): string {
if (!birthDate) {
return '-'
}
let birth: Date
// Konversi ke Date object
if (typeof birthDate === 'string') {
birth = new Date(birthDate)
} else {
birth = birthDate
}
// Validasi tanggal
if (isNaN(birth.getTime())) {
return '-'
}
const today = new Date()
const birthYear = birth.getFullYear()
const birthMonth = birth.getMonth()
const birthDay = birth.getDate()
const currentYear = today.getFullYear()
const currentMonth = today.getMonth()
const currentDay = today.getDate()
// Hitung tahun
let years = currentYear - birthYear
let months = currentMonth - birthMonth
// Adjust jika bulan atau hari belum lewat
if (months < 0 || (months === 0 && currentDay < birthDay)) {
years--
months += 12
}
if (currentDay < birthDay) {
months--
}
// Pastikan months tidak negatif
if (months < 0) {
months += 12
}
// Format output
if (years === 0) {
if (months === 0) {
// Hitung hari untuk bayi baru lahir
const diffTime = today.getTime() - birth.getTime()
const days = Math.floor(diffTime / (1000 * 60 * 60 * 24))
return days <= 30 ? `${days} hari` : '1 bulan'
}
return `${months} bulan`
} else if (months === 0) {
return `${years} tahun`
} else {
return `${years} tahun ${months} bulan`
}
}