fix(lancar-gto): auto-seleção robusta de paciente vindo da agenda

- Adiciona normalize() para remover acentos antes de comparar nomes,
  resolvendo falhas quando o agendamento usa "Joao" e o cadastro "João"
- Troca correspondência exata por filtro com includes() em ambas as direções
- Seleciona diretamente se há exatamente 1 match (sem abrir modal)
- Se múltiplos matches: abre modal com busca pré-preenchida
- Se nenhum match: abre modal vazio para busca manual
- Aplica normalize() também no filteredPatients para consistência

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
VPS 4 Builder
2026-05-14 22:46:44 +02:00
parent 36c4435787
commit 6dcfb27ca8
+17 -9
View File
@@ -87,6 +87,9 @@ const DENTES_CRIANCA = {
q3: [71, 72, 73, null, 74, 75, null, null] q3: [71, 72, 73, null, 74, 75, null, null]
}; };
const normalize = (s: string) =>
s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
// --- COMPONENT --- // --- COMPONENT ---
export const LancarGTO: React.FC = () => { export const LancarGTO: React.FC = () => {
const store = useGTOStore(); const store = useGTOStore();
@@ -128,24 +131,29 @@ export const LancarGTO: React.FC = () => {
const filteredPatients = useMemo(() => { const filteredPatients = useMemo(() => {
if (!patientSearchTerm) return pacientes.slice(0, 30); if (!patientSearchTerm) return pacientes.slice(0, 30);
const term = patientSearchTerm.toLowerCase(); const term = normalize(patientSearchTerm);
return pacientes.filter(p => return pacientes.filter(p =>
p.nome.toLowerCase().includes(term) || normalize(p.nome).includes(term) ||
(p.cpf && p.cpf.includes(term)) (p.cpf && p.cpf.includes(patientSearchTerm.replace(/\D/g, '')))
).slice(0, 30); ).slice(0, 30);
}, [pacientes, patientSearchTerm]); }, [pacientes, patientSearchTerm]); // eslint-disable-line react-hooks/exhaustive-deps
// Auto-seleciona paciente quando vem da agenda (pendingSearch) // Auto-seleciona paciente quando vem da agenda (pendingSearch)
useEffect(() => { useEffect(() => {
const { pendingSearch } = store; const { pendingSearch } = store;
if (!pendingSearch || pacientes.length === 0) return; if (!pendingSearch || pacientes.length === 0) return;
const term = pendingSearch.toLowerCase(); const term = normalize(pendingSearch);
const match = pacientes.find(p => p.nome.toLowerCase() === term); const matches = pacientes.filter(p => {
if (match) { const n = normalize(p.nome);
store.setPaciente(match); return n === term || n.includes(term) || term.includes(n);
} else { });
if (matches.length === 1) {
store.setPaciente(matches[0]);
} else if (matches.length > 1) {
setPatientSearchTerm(pendingSearch); setPatientSearchTerm(pendingSearch);
setPatientSearchOpen(true); setPatientSearchOpen(true);
} else {
setPatientSearchOpen(true);
} }
store.setPendingSearch(''); store.setPendingSearch('');
}, [pacientes, store.pendingSearch]); // eslint-disable-line react-hooks/exhaustive-deps }, [pacientes, store.pendingSearch]); // eslint-disable-line react-hooks/exhaustive-deps