text: import { useContext, useMemo } from "react";
  import { useMutation, useQuery } from "@apollo/client";
  import { Box } from "rebass";
  
  import { UserContext } from "~/components/UserContext";
  
  import { ENQUETE_WITH_REPONSE_STATUS } from "../queries";
  import { EnqueteActiviteMesuresForm } from "./common";
  import { UPDATE_ENQUETE_SUBROGE_TUTEUR_CREATEUR } from "./mutations";
  import { ENQUETE_SUBROGE_TUTEUR_CREATEUR } from "./queries";
  
  const PREFIX = "subroge_tuteur_createur";
  
  export function EnqueteActiviteSubrogeTuteurCreateur(props) {
    const {
      enqueteContext,
      dispatchEnqueteContextEvent,
      enqueteReponse,
      section,
      step,
      enquete: { id: enqueteId },
    } = props;
    const {
      enquete_reponse_ids: { activite_id },
    } = enqueteReponse;
    const { id: userId } = useContext(UserContext);
  
    const [updateEnquete] = useMutation(UPDATE_ENQUETE_SUBROGE_TUTEUR_CREATEUR, {
      refetchQueries: [
        {
          query: ENQUETE_WITH_REPONSE_STATUS,
          variables: { enqueteId, userId },
        },
        {
          query: ENQUETE_SUBROGE_TUTEUR_CREATEUR,
          variables: {
            id: activite_id,
          },
        },
      ],
    });
    const { data, loading } = useQuery(ENQUETE_SUBROGE_TUTEUR_CREATEUR, {
      variables: {
        id: activite_id,
      },
    });
  
    const normalizedData = useMemo(() => {
      const r = data ? data.enquete_reponses_activite_by_pk || {} : {};
  
      return {
        debutAnnee: r[`${PREFIX}_debut_annee`],
        finAnnee: r[`${PREFIX}_fin_annee`],
        mesuresNouvelles: r[`${PREFIX}_mesures_nouvelles`],
        sortieMesures: r[`${PREFIX}_sortie_mesures`],
      };
    }, [data]);
  
    return (
      <Box>
        <EnqueteActiviteMesuresForm
          loading={loading}
          data={normalizedData}
          section={section}
          step={step}
          onSubmit={async (values) => {
            await updateEnquete({
              variables: {
                id: activite_id,
                ...values,
              },
            });
          }}
          enqueteContext={enqueteContext}
          dispatchEnqueteContextEvent={dispatchEnqueteContextEvent}
          title="Subrogé tuteur ou curateur"
        />
      </Box>
    );
  }
  
  export default EnqueteActiviteSubrogeTuteurCreateur;
  