inogen/app/admin/components/RelatedEntitySelect.tsx

55 lines
2.7 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useEffect, useMemo, useState } from "react";
import { api } from "../lib/api";
import { useDebouncedValue } from "./ListControls";
import { Input, Select } from "./Ui";
type Option = { id: string; title: string; projectNumber?: string };
type Page = { items: Option[]; totalCount: number };
export function RelatedEntitySelect({ name, path, value, onChange, placeholder, required = true }: {
name: string; path: "/api/v1/ideas" | "/api/v1/projects";
value: string; onChange: (id: string) => void; placeholder: string; required?: boolean;
}) {
const [search, setSearch] = useState("");
const query = useDebouncedValue(search);
const [items, setItems] = useState<Option[]>([]);
const [selected, setSelected] = useState<Option | null>(null);
const [total, setTotal] = useState(0);
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
useEffect(() => {
let active = true;
setLoading(true); setError("");
const params = new URLSearchParams({ page: "1", pageSize: "100" });
if (query.trim()) params.set("search", query.trim());
void api<Page>(`${path}?${params}`).then(result => {
if (active) { setItems(result.items); setTotal(result.totalCount); }
}).catch(reason => { if (active) setError(reason instanceof Error ? reason.message : "دریافت گزینه‌ها ناموفق بود."); })
.finally(() => { if (active) setLoading(false); });
return () => { active = false; };
}, [path, query]);
useEffect(() => {
if (!value) { setSelected(null); return; }
let active = true;
void api<Option>(`${path}/${value}`).then(item => { if (active) setSelected(item); })
.catch(() => { if (active) setSelected(null); });
return () => { active = false; };
}, [path, value]);
const options = useMemo(() => selected && !items.some(item => item.id === selected.id)
? [selected, ...items] : items, [selected, items]);
const label = (item: Option) => item.projectNumber ? `${item.projectNumber} — ${item.title}` : item.title;
return <div className="related-record-select">
<Input value={search} onChange={event => setSearch(event.target.value)} placeholder={placeholder}/>
<Select name={name} value={value} onChange={event => onChange(event.target.value)} required={required}>
<option value="">انتخاب کنید</option>
{options.map(item => <option key={item.id} value={item.id}>{label(item)}</option>)}
</Select>
{loading && <small>در حال دریافت گزینه‌ها…</small>}
{total > 100 && <small>از میان {total} رکورد، جست‌وجو کنید تا گزینهٔ موردنظر نمایش داده شود.</small>}
{error && <small role="alert">{error}</small>}
</div>;
}