refactor(web): 新增 useDictionary hook + 4 个页面下拉选项改用字典 API
- 新增 useDictionary hook 支持字典 API 获取 + fallback 降级 - DoctorList 科室/职称改用 useDictionary (health_department/health_title) - FollowUpTaskList 随访类型改用 useDictionary (health_follow_up_type) - ConsultationList 咨询类型改用 useDictionary (health_consultation_type) - FamilyMembersTab 家庭关系改用 useDictionary (health_relationship)
This commit is contained in:
31
apps/web/src/hooks/useDictionary.ts
Normal file
31
apps/web/src/hooks/useDictionary.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { useEffect, useState, useMemo } from 'react';
|
||||
import { listItemsByCode, type DictionaryItemInfo } from '../api/dictionaries';
|
||||
|
||||
export interface DictOption {
|
||||
value: string;
|
||||
label: string;
|
||||
}
|
||||
|
||||
export function useDictionary(code: string, fallback?: DictOption[]) {
|
||||
const [items, setItems] = useState<DictionaryItemInfo[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
setLoading(true);
|
||||
listItemsByCode(code)
|
||||
.then((data) => setItems(data))
|
||||
.catch(() => setItems([]))
|
||||
.finally(() => setLoading(false));
|
||||
}, [code]);
|
||||
|
||||
const options = useMemo<DictOption[]>(() => {
|
||||
if (items.length > 0) {
|
||||
return items
|
||||
.sort((a, b) => a.sort_order - b.sort_order)
|
||||
.map((item) => ({ value: item.value, label: item.label }));
|
||||
}
|
||||
return fallback ?? [];
|
||||
}, [items, fallback]);
|
||||
|
||||
return { items, options, loading };
|
||||
}
|
||||
Reference in New Issue
Block a user