From e9d14a0a516217da02e1ef8a6e1dc78e46116b18 Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Fri, 26 Jul 2024 21:35:30 +0530 Subject: fetching the content from firebase --- src/components/ViewAds.jsx | 51 +++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 16 deletions(-) (limited to 'src/components/ViewAds.jsx') diff --git a/src/components/ViewAds.jsx b/src/components/ViewAds.jsx index 719d1b2..0c5486c 100644 --- a/src/components/ViewAds.jsx +++ b/src/components/ViewAds.jsx @@ -1,30 +1,49 @@ -import React, { useEffect, useState } from 'react'; -import { ref, listAll, getDownloadURL } from 'firebase/storage'; +import React, { useState, useEffect } from 'react'; +import './ViewAds.css'; +import { getDownloadURL, listAll, ref } from 'firebase/storage'; import { storage } from '../firebase'; const ViewAds = () => { - const [ads, setAds] = useState([]); + const [urls, setUrls] = useState([]); useEffect(() => { - const fetchAds = async () => { - const adsRef = ref(storage, 'ads/'); - const adList = await listAll(adsRef); - const adUrls = await Promise.all(adList.items.map((item) => getDownloadURL(item))); - setAds(adUrls); + const fetchUrls = async () => { + const listRef = ref(storage, 'ads/'); + try { + const res = await listAll(listRef); + const urlsPromises = res.items.map(itemRef => getDownloadURL(itemRef)); + const urls = await Promise.all(urlsPromises); + setUrls(urls); + } catch (error) { + console.error('Error fetching URLs:', error); + } }; - fetchAds(); + fetchUrls(); }, []); return (
-

View Ads

-
- {ads.map((url, index) => ( -
- {`Ad -
- ))} +

Uploaded Ads

+
+ {urls.map((url, index) => { + const isImage = url.match(/\.(jpeg|jpg|gif|png)$/); + const isVideo = url.match(/\.(mp4|mov|avi|mkv)$/); + return ( +
+ {isImage ? ( + {`Ad + ) : isVideo ? ( + + ) : ( +

Unsupported file type

+ )} +
+ ); + })}
); -- cgit v1.2.3-59-g8ed1b