import { createRef, useEffect, useRef } from "react"; import { useState } from "react"; import { FiLink } from "react-icons/fi"; import useFilter from "../../hooks/useFilter.hook.jsx"; import styles from "../../styles/pdf_gen/selectExo.module.scss"; import { isBrowser, isEmpty, parseClassName } from "../../utils/utils.js"; import TagSelect from "../exos/TagSelect.jsx"; import Modal from "../Modal.jsx"; import Tag from "../Tag.jsx"; import ParamItem from "./ParamItem.jsx"; import TagContainer from "./tagContainer.jsx"; import { AiOutlineClose } from "react-icons/ai"; import { useInfiniteQuery, useQuery } from "react-query"; import { getExos, getPublicExos, getTags, getUserExos, } from "../../requests/requests.exos.js"; import { useInView } from "react-intersection-observer"; import { useSessionContext } from "../../context/session.context.js"; export default function SelectExo({ exos, setExos, emptyError, onlyNumber = false, }) { const { ref, inView } = useInView(); const [active, setActive] = useState(false); const { data: tagsData } = useQuery( ["tags"], async () => { return await getTags(); }, { enabled: active, staleTime: 60 * 1000 } ); const [selectedOption, setSelectedOption] = useState([]); const [selectFilter, setSelectFilter] = useState( isBrowser && localStorage.getItem("token") != null ? "user" : "all" ); const [search, setSearch] = useState(""); const { isLoading, isError, data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage, } = useInfiniteQuery( ["userExo-data", { search, tags: selectedOption.map((t) => t.value) }], ({ pageParam = 0 }) => { return getUserExos("pdf", pageParam == 0 ? 1 : pageParam, { search, tags: selectedOption.map((t) => t.value), }).then((res) => { return { ...res, results: [ ...res.results.map((r) => { return { ...r, numberInExo: 10, numberOfExo: 1 }; }), ], }; }); }, { enabled: (active && selectFilter == "user") /* || exos.length != 0 */, staleTime: 60 * 1000, refetchOnWindowFocus: false, keepPreviousData: true, getPreviousPageParam: (firstPage) => firstPage.links.previous ? firstPage.page_number - 1 : undefined, getNextPageParam: (lastPage) => lastPage.links.next ? lastPage.page_number + 1 : undefined, } ); const { isLoading: publicLoading, isError: publicError, data: publicData, fetchNextPage: publicFetchNextPage, hasNextPage: publicHasNextPage, isFetching: publicFetching, isFetchingNextPage: publicIsFetchingNextPage, } = useInfiniteQuery( ["publicExo-data", { search, tags: selectedOption.map((t) => t.value) }], ({ pageParam = 0 }) => { return getPublicExos("pdf", pageParam == 0 ? 1 : pageParam, { search, tags: selectedOption.map((t) => t.value), }).then((res) => { return { ...res, results: [ ...res.results.map((r) => { return { ...r, numberInExo: 10, numberOfExo: 1 }; }), ], }; }); }, { enabled: (active && selectFilter == "all") /* || exos.length != 0 */, staleTime: 60 * 1000, refetchOnWindowFocus: false, keepPreviousData: true, getPreviousPageParam: (firstPage) => firstPage.links.previous ? firstPage.page_number - 1 : undefined, getNextPageParam: (lastPage) => lastPage.links.next ? lastPage.page_number + 1 : undefined, } ); useEffect(() => { if (inView) { selectFilter == "user" ? fetchNextPage() : publicFetchNextPage(); } }, [inView]); const {authData} = useSessionContext() return ( <>
Ajouter exercices
setActive(true)}> +
Aucun exercice selectionné
{emptyError == true && (Veuillez selectionner un exercice au minimum
)} > )} {exos.map((s, i) => { return (Aucun résultat
)} {selectFilter == "all" && publicData && publicData.pages.map((p) => p.results).flat().length == 0 && (Aucun résultat
)} {selectFilter == "user" && data && data.pages.map((page) => { return page.results.map((d, i) => { return (