aboutsummaryrefslogblamecommitdiffstats
path: root/src/pages/ManageAds.jsx
blob: cceca06401936c6321086d9e922e0cb8093e0bc9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                                   
                                                  
                                            



                                               

                                                                













                                       



                                         
                                         
                                            

    


                         
                                                            











                                                                     




                         
import React, { useState, useEffect } from 'react';
import AdCard from '../components/AdCard';
import UploadForm from '../components/UploadForm';
import ViewAds from '../components/ViewAds';
import { fetchAds, addAd } from '../utils/api';

const ManageAds = () => {
  const [ads, setAds] = useState([]);
  const [showUploadedAds, setShowUploadedAds] = useState(false);
  const [uploadedUrls, setUploadedUrls] = useState([]);

  useEffect(() => {
    const getAds = async () => {
      const adsData = await fetchAds();
      setAds(adsData);
    };
    getAds();
  }, []);

  const handleAddAd = async (ad) => {
    await addAd(ad);
    setAds([...ads, ad]);
  };

  const toggleViewAds = () => {
    setShowUploadedAds(!showUploadedAds);
  };

  const handleUploadComplete = (url) => {
    setUploadedUrls([...uploadedUrls, url]);
  };

  return (
    <div>
      <h1>Manage Ads</h1>
      <UploadForm onUploadComplete={handleUploadComplete} />
      <button onClick={toggleViewAds}>
        {showUploadedAds ? 'Hide Uploaded Ads' : 'View Uploaded Ads'}
      </button>
      {showUploadedAds ? (
        <ViewAds />
      ) : (
        <div>
          {ads.map((ad) => (
            <AdCard key={ad.id} ad={ad} />
          ))}
        </div>
      )}
    </div>
  );
};

export default ManageAds;