diff --git a/components/UI/Services.jsx b/components/UI/Services.jsx index 87050f80..e31f5fc1 100644 --- a/components/UI/Services.jsx +++ b/components/UI/Services.jsx @@ -1,12 +1,35 @@ -import React from "react"; +import React, { useEffect, useState } from "react"; import { Container, Row, Col, Button } from "reactstrap"; import Slider from "react-slick"; -import Image from "next/image"; -import SectionSubtitle from "./SectionSubtitle"; import classes from "../../styles/services.module.css"; import ServicesItem from "./ServicesItem"; +import SectionSubtitle from "./SectionSubtitle"; +import { fetchYouTubeStats, fetchYouTubeVideos } from "../data/youtube.js"; +const Services = () => { + const [youtubeStats, setYoutubeStats] = useState(null); + const [youtubeVideos, setYoutubeVideos] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + const fetchData = async () => { + try { + const [stats, videos] = await Promise.all([ + fetchYouTubeStats(), + fetchYouTubeVideos(), + ]); + + setYoutubeStats(stats); + setYoutubeVideos(videos); + } catch (error) { + console.error("Error fetching YouTube data:", error); + } finally { + setLoading(false); + } + }; + + fetchData(); + }, []); -const Services = ({ youtubeStats, youtubeVideos }) => { const settings = { dots: false, autoplay: true, @@ -18,17 +41,34 @@ const Services = ({ youtubeStats, youtubeVideos }) => { slidesToScroll: 1, arrows: true, }; + + // Format subscriber count with K/M suffixes + const formatSubscribers = (count) => { + if (!count) return "0"; + + const num = Number(count); + if (num >= 1000000) return (num / 1000000).toFixed(1) + "M"; + if (num >= 1000) return (num / 1000).toFixed(1) + "K"; + return num.toString(); + }; + + if (loading) { + return ( +
+ +
Loading YouTube data...
+
+
+ ); + } + return (
- - {youtubeVideos + + {youtubeVideos?.items ?.filter((video) => video.id.videoId) ?.map((video) => (
{ diff --git a/components/data/youtube.js b/components/data/youtube.js new file mode 100644 index 00000000..4f284d68 --- /dev/null +++ b/components/data/youtube.js @@ -0,0 +1,26 @@ +const API_KEY = process.env.YOUTUBE_API_KEY; +const CHANNEL_ID = process.env.YOUTUBE_CHANNEL_ID; + +export const fetchYouTubeStats = async () => { + try { + const response = await fetch( + `https://www.googleapis.com/youtube/v3/channels?part=statistics&id=${CHANNEL_ID}&key=${API_KEY}` + ); + return await response.json(); + } catch (error) { + console.error("Error fetching YouTube stats:", error); + return null; + } +}; + +export const fetchYouTubeVideos = async () => { + try { + const response = await fetch( + `https://www.googleapis.com/youtube/v3/search?key=${API_KEY}&channelId=${CHANNEL_ID}&part=snippet,id&order=date&maxResults=3` + ); + return await response.json(); + } catch (error) { + console.error("Error fetching YouTube videos:", error); + return null; + } +}; diff --git a/next.config.js b/next.config.js index 800514bd..a1cff71f 100644 --- a/next.config.js +++ b/next.config.js @@ -18,6 +18,11 @@ const nextConfig = { images: { domains: ["i.ytimg.com", "cdn.hashnode.com", "wsrv.nl"], }, + // Add environment variables configuration + env: { + YOUTUBE_API_KEY: process.env.YOUTUBE_API_KEY, + YOUTUBE_CHANNEL_ID: process.env.YOUTUBE_CHANNEL_ID, + }, }; module.exports = nextConfig;