import { Fragment, useState, useEffect, useContext, useRef, useCallback } from 'react';
import HeadTag from '../../components/HeadTag';
import Link from 'next/link';
import { UserContext } from '../../contexts/UserContext';
import FunctionComponentTop from '../../components/FunctionComponentTop';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faTwitter } from '@fortawesome/free-brands-svg-icons';
import SwipeableViews from 'react-swipeable-views';
import redirect from '../../utils/redirect';
import FunctionArticle from '../../components/FunctionArticle';
import FunctionComments from '../../components/FunctionComments/FunctionComments';
import htmlParser from 'html-react-parser';
import Loader from '../../components/Loader';
import api from '../../utils/api';
import copyToClipboard from '../../utils/copyToClipboard';
import { API_URL } from '../../utils/config';
import '../../public/css/pages/FunctionComponent.css';
import '../../components/FunctionTabs/FunctionTabs.css';
import '../../components/FunctionCard/FunctionCard.css';
import 'notyf/notyf.min.css';
const FunctionTabsTop = (props) => {
return (
);
}
const FunctionTabs = (props) => {
return (
props.setSlideIndex(index)} index={props.slideIndex} enableMouseEvents>
{props.children}
);
}
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 (
" {quote.quote} "
- {quote.author}
);
}
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 (
Liste des citations :
Total de {quotesData.totalItems} citations.
Citation/Proverbe |
Auteur |
Proposée par |
{quotesData.rows.map((currentQuote, index) => {
const quoteJSX = (
{currentQuote.quote} |
{currentQuote.author} |
{currentQuote.user.name}
|
);
// Si c'est le dernier élément
if (quotesData.rows.length === index + 1) {
return {quoteJSX}
}
return {quoteJSX}
})}
);
}
const SuggestQuote = () => {
const { isAuth, user } = useContext(UserContext);
const [inputState, setInputState] = useState({ quote: "", author: "" });
const [message, setMessage] = useState("");
const [isLoading, setIsLoading] = useState(false);
const handleChange = (event) => {
const inputStateNew = { ...inputState };
inputStateNew[event.target.name] = event.target.value;
setInputState(inputStateNew);
}
const handleSubmit = (event) => {
setIsLoading(true);
event.preventDefault();
const token = user.token;
if (isAuth && token != undefined) {
api.post('/quotes', inputState, { headers: { 'Authorization': token } })
.then(({ data }) => {
setInputState({ quote: "", author: "" });
setMessage(`Succès: ${data.message}
`);
setIsLoading(false);
})
.catch((error) => {
setMessage(`Erreur: ${error.response.data.message}
`);
setIsLoading(false);
});
}
}
return (
{
(isAuth) ?
Proposer une citation :
Vous pouvez proposer des citations, et une fois validé elles seront rajoutés à la liste des citations.
{
(isLoading) ?
:
htmlParser(message)
}
:
Vous devez être connecté pour proposer une citation.
}
);
}
const FunctionTabManager = (props) => {
return (
);
}
const randomQuote = (props) => {
const [slideIndex, setSlideIndex] = useState(0);
return (
);
}
export async function getServerSideProps(context) {
return api.get(`/functions/randomQuote`)
.then((response) => ({ props: response.data }))
.catch(() => redirect(context, '/404'));
}
export default randomQuote;