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]
};
const normalize = (s: string) =>
s.normalize('NFD').replace(/[̀-ͯ]/g, '').toLowerCase().trim();
// --- COMPONENT ---
export const LancarGTO: React.FC = () => {
const store = useGTOStore();
@@ -128,24 +131,29 @@ export const LancarGTO: React.FC = () => {
const filteredPatients = useMemo(() => {
if (!patientSearchTerm) return pacientes.slice(0, 30);
const term = patientSearchTerm.toLowerCase();
const term = normalize(patientSearchTerm);
return pacientes.filter(p =>
p.nome.toLowerCase().includes(term) ||
(p.cpf && p.cpf.includes(term))
normalize(p.nome).includes(term) ||
(p.cpf && p.cpf.includes(patientSearchTerm.replace(/\D/g, '')))
).slice(0, 30);
}, [pacientes, patientSearchTerm]);
}, [pacientes, patientSearchTerm]); // eslint-disable-line react-hooks/exhaustive-deps
// Auto-seleciona paciente quando vem da agenda (pendingSearch)
useEffect(() => {
const { pendingSearch } = store;
if (!pendingSearch || pacientes.length === 0) return;
const term = pendingSearch.toLowerCase();
const match = pacientes.find(p => p.nome.toLowerCase() === term);
if (match) {
store.setPaciente(match);
} else {
const term = normalize(pendingSearch);
const matches = pacientes.filter(p => {
const n = normalize(p.nome);
return n === term || n.includes(term) || term.includes(n);
});
if (matches.length === 1) {
store.setPaciente(matches[0]);
} else if (matches.length > 1) {
setPatientSearchTerm(pendingSearch);
setPatientSearchOpen(true);
} else {
setPatientSearchOpen(true);
}
store.setPendingSearch('');
}, [pacientes, store.pendingSearch]); // eslint-disable-line react-hooks/exhaustive-deps