import { Fragment, useState, useEffect, useContext, useRef, useCallback } from 'react'; import Link from 'next/link'; import { UserContext } from '../../contexts/UserContext'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faTwitter } from '@fortawesome/free-brands-svg-icons'; import redirect from '../../utils/redirect'; import htmlParser from 'html-react-parser'; import Loader from '../../components/Loader'; import FunctionPage from '../../components/FunctionPage/FunctionPage'; import FunctionTabs from '../../components/FunctionPage/FunctionTabs'; import FunctionArticle from '../../components/FunctionPage/FunctionArticle'; import FunctionComments from '../../components/FunctionPage/FunctionComments/FunctionComments'; import api from '../../utils/api'; import copyToClipboard from '../../utils/copyToClipboard'; import 'notyf/notyf.min.css'; import '../../public/css/pages/FunctionComponent.css'; import '../../components/FunctionCard/FunctionCard.css'; const GenerateQuote = () => { const [quote, setQuote] = useState({ quote: "", author: "" }); useEffect(() => { getRandomQuote(); }, []); const getRandomQuote = async () => { const { data } = await api.post("/functions/randomQuote"); setQuote(data); } const handleCopyQuote = () => { let Notyf; if (typeof window != 'undefined') { Notyf = require('notyf'); } const notyf = new Notyf.Notyf({ duration: 5000 }); copyToClipboard(`"${quote.quote}" - ${quote.author}`); notyf.success('Citation copiée dans le presse-papier!'); } return (
); } const QuoteList = () => { const [quotesData, setQuotesData] = useState({ hasMore: true, rows: [], totalItems: 0 }); const [isLoadingQuotes, setLoadingQuotes] = useState(true); const [pageQuotes, setPageQuotes] = useState(1); // Récupère les citations si la page change useEffect(() => { getQuotesData(); }, [pageQuotes]); const getQuotesData = async () => { setLoadingQuotes(true); const { data } = await api.get(`/quotes?limit=20page=${pageQuotes}`); setQuotesData({ hasMore: data.hasMore, rows: [...quotesData.rows, ...data.rows], totalItems: data.totalItems }); setLoadingQuotes(false); } // Permet la pagination au scroll const observer = useRef(); const lastQuoteRef = useCallback((node) => { if (isLoadingQuotes) return; if (observer.current) observer.current.disconnect(); observer.current = new IntersectionObserver((entries) => { if (entries[0].isIntersecting && quotesData.hasMore) { setPageQuotes(pageQuotes + 1); } }, { threshold: 1 }); if (node) observer.current.observe(node); }, [isLoadingQuotes, quotesData.hasMore]); return (Total de {quotesData.totalItems} citations.
Citation/Proverbe | Auteur | Proposée par | {currentQuote.quote} | {currentQuote.author} | {currentQuote.user.name} |
---|---|---|
Succès: ${data.message}
`); setIsLoading(false); }) .catch((error) => { setMessage(`Erreur: ${error.response.data.message}
`); setIsLoading(false); }); } } if (!isAuth) { return (Vous devez être connecté pour proposer une citation.
); } return (Vous pouvez proposer des citations, et une fois validé elles seront rajoutés à la liste des citations.