diff --git a/components/rdv/OngletAgendaConseiller.tsx b/components/rdv/OngletAgendaConseiller.tsx index 7c9bff42b..74d8569c7 100644 --- a/components/rdv/OngletAgendaConseiller.tsx +++ b/components/rdv/OngletAgendaConseiller.tsx @@ -1,5 +1,5 @@ import { DateTime } from 'luxon' -import React, { useState } from 'react' +import React, { useEffect, useRef, useState } from 'react' import EmptyState from 'components/EmptyState' import TableauEvenementsConseiller from 'components/rdv/TableauEvenementsConseiller' @@ -35,20 +35,26 @@ export default function OngletAgendaConseiller({ periodeIndex, changerPeriode, }: OngletAgendaConseillerProps) { + const tableRef = useRef(null) + const [evenements, setEvenements] = useState() + const [shouldFocus, setShouldFocus] = useState(false) + const [periode, setPeriode] = useState<{ debut: DateTime; fin: DateTime }>() const [labelPeriode, setLabelPeriode] = useState() const [failed, setFailed] = useState(false) async function chargerNouvellePeriode( - nouvellePeriodeIndex: number, - dateDebut: DateTime, - dateFin: DateTime, - label: string + nouvellePeriode: { index: number; dateDebut: DateTime; dateFin: DateTime }, + opts: { label: string; shouldFocus: boolean } ) { - await initEvenementsPeriode(dateDebut, dateFin) - setLabelPeriode(label) - changerPeriode(nouvellePeriodeIndex) + await initEvenementsPeriode( + nouvellePeriode.dateDebut, + nouvellePeriode.dateFin + ) + setLabelPeriode(opts.label) + changerPeriode(nouvellePeriode.index) + setShouldFocus(opts.shouldFocus) } async function initEvenementsPeriode(dateDebut: DateTime, dateFin: DateTime) { @@ -89,6 +95,10 @@ export default function OngletAgendaConseiller({ ) } + useEffect(() => { + if (shouldFocus) tableRef.current?.focus() + }, [evenements]) + return ( <> 0 && ( diff --git a/components/rdv/OngletAgendaEtablissement.tsx b/components/rdv/OngletAgendaEtablissement.tsx index 1945b6fc0..490dd0c70 100644 --- a/components/rdv/OngletAgendaEtablissement.tsx +++ b/components/rdv/OngletAgendaEtablissement.tsx @@ -50,6 +50,7 @@ export default function OngletAgendaEtablissement({ const [recherche, setRecherche] = useState('') const [evenementsAffiches, setEvenementsAffiches] = useState() + const [shouldFocus, setShouldFocus] = useState(false) const [periode, setPeriode] = useState<{ debut: DateTime; fin: DateTime }>() const [labelPeriode, setLabelPeriode] = useState() @@ -61,14 +62,16 @@ export default function OngletAgendaEtablissement({ } async function modifierPeriode( - nouvellePeriodeIndex: number, - dateDebut: DateTime, - dateFin: DateTime, - label: string + nouvellePeriode: { index: number; dateDebut: DateTime; dateFin: DateTime }, + opts: { label: string; shouldFocus: boolean } ) { - await chargerEvenementsPeriode(dateDebut, dateFin) - setLabelPeriode(label) - changerPeriode(nouvellePeriodeIndex) + await chargerEvenementsPeriode( + nouvellePeriode.dateDebut, + nouvellePeriode.dateFin + ) + setLabelPeriode(opts.label) + changerPeriode(nouvellePeriode.index) + setShouldFocus(opts.shouldFocus) } async function chargerEvenementsPeriode( @@ -127,11 +130,15 @@ export default function OngletAgendaEtablissement({ tableRef.current?.focus() }, [recherche]) + useEffect(() => { + if (shouldFocus) tableRef.current?.focus() + }, [evenementsAffiches]) + return ( <> -