Advertisement
bebo231312312321

Untitled

Nov 12th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable react-hooks/exhaustive-deps */
  2. import React, { useState, useEffect } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import { QuotesIcon, QuotesFlippedIcon } from '../../../Utils/AdminIcons';
  5. import { userServiceFactory } from '../../services/userService';
  6.  
  7. const RandomQuote = () => {
  8.     const { i18n } = useTranslation();
  9.     const [quote, setQuote] = useState(null);
  10.  
  11.     const userService = userServiceFactory();
  12.  
  13.     useEffect(() => {
  14.         const fetchQuotes = async () => {
  15.             try {
  16.                 const response = await userService.getQuotes();
  17.  
  18.                 if (response && Array.isArray(response)) {
  19.                     const transformedQuotes = response.map(q => ({
  20.                         quoteBg: q.bg,
  21.                         quoteEn: q.en,
  22.                         authorBg: q.author.bg,
  23.                         authorEn: q.author.en
  24.                     }));
  25.  
  26.                     const randomIndex = Math.floor(Math.random() * transformedQuotes.length);
  27.                     setQuote(transformedQuotes[randomIndex]);
  28.                 }
  29.             } catch (error) {
  30.                 console.error('Error fetching quotes:', error);
  31.             }
  32.         };
  33.  
  34.         fetchQuotes();
  35.     }, []);
  36.  
  37.     const currentLanguage = i18n.language;
  38.  
  39.     const displayedQuote = quote ? (currentLanguage === 'bg' ? quote.quoteBg : quote.quoteEn) : '';
  40.     const displayedAuthor = quote ? (currentLanguage === 'bg' ? quote.authorBg : quote.authorEn) : '';
  41.  
  42.     return (
  43.         <section className="quote">
  44.             <div className="quote-flex">
  45.                 <div className="quote-first">
  46.                     <QuotesIcon />
  47.                 </div>
  48.                 <p className="display-quote">
  49.                     {displayedQuote}
  50.                 </p>
  51.                 <div className="quote-second">
  52.                     <QuotesFlippedIcon />
  53.                 </div>
  54.                 <p className="quote-author">{displayedAuthor}</p>
  55.             </div>
  56.         </section>
  57.     );
  58. };
  59.  
  60. export default RandomQuote;
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement