Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SearchFeed Optimisation and Code readability #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/components/SearchFeed.jsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import { useState, useEffect } from "react";
import React, { useState, useEffect } from "react";
import { Typography, Box } from "@mui/material";
import { useParams } from "react-router-dom";

import { fetchFromAPI } from "../utils/fetchFromAPI";
import { Videos } from "./";

const SearchFeed = () => {
// State to hold the videos data
const [videos, setVideos] = useState(null);

// Get the search term from URL params
const { searchTerm } = useParams();

// Fetch videos data from API when searchTerm changes
useEffect(() => {
fetchFromAPI(`search?part=snippet&q=${searchTerm}`)
.then((data) => setVideos(data.items))
const fetchVideos = async () => {
try {
const data = await fetchFromAPI(`search?part=snippet&q=${searchTerm}`);
setVideos(data.items);
} catch (error) {
console.error("Error fetching videos:", error);
setVideos([]); // Set videos to empty array in case of error
}
};
fetchVideos();
}, [searchTerm]);

return (
<Box p={2} minHeight="95vh">
<Typography variant="h4" fontWeight={900} color="white" mb={3} ml={{ sm: "100px"}}>
{/* Display search term and results count */}
<Typography variant="h4" fontWeight={900} color="white" mb={3}>
Search Results for <span style={{ color: "#FC1503" }}>{searchTerm}</span> videos
</Typography>
<Box display="flex">
<Box sx={{ mr: { sm: '100px' } }}/>
{<Videos videos={videos} />}
{/* Spacer to align content */}
<Box mr={{ sm: '100px' }} />
{/* Render videos component with fetched data */}
<Videos videos={videos} />
</Box>
</Box>
);
Expand Down