Skip to content

Commit

Permalink
loader for search
Browse files Browse the repository at this point in the history
  • Loading branch information
mihir224 committed Sep 30, 2023
1 parent 9a440e2 commit 94d6179
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
45 changes: 39 additions & 6 deletions client/src/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,60 @@ import React,{useState,useEffect} from "react";
import {useLocation} from "react-router-dom";
import axios from "axios";
import Card from './Card';
import { Oval } from 'react-loader-spinner';
import {useSelector} from "react-redux";

function Search(){
const showNav=useSelector(state=>state.navbar.open);
const[videos,setVideos] =useState([]);
const [isLoading,setLoading]=useState(false);
const [found,setFound]=useState(false);
const query=useLocation().search;
useEffect(()=>{
setLoading(true);
(async ()=>{
try{
const url=process.env.NODE_ENV==="production"?"https://youtube-clone-api224.onrender.com/api":"";
const res=await axios.get(`${url}/videos/search${query}`,{withCredentials: true});
setVideos(res.data);
if(res.data.length===0){
setFound(false);
}
else{
setVideos(res.data);
setFound(true);
}
setLoading(false);
}
catch(err){
console.log(err);
}
})();
},[]);
return(
},[query]);
return (
<div id='cards' style={{display:'flex',alignItems:'flex-start', justifyContent:'center'}}>
{videos?.map((video) => {
return <Card key={video._id} video={video} />;
})}
{
isLoading?(
<div style={{paddingTop:`${showNav?"":"170px"}`}}>
<Oval
height={80}
width={80}
color="white"
wrapperStyle={{}}
wrapperClass=""
visible={true}
ariaLabel='oval-loading'
secondaryColor="grey"
strokeWidth={2}
strokeWidthSecondary={2}
/>
</div>
):
(found?(videos?.map((video) => {
return <Card key={video._id} video={video} />
})):
(<h2 style={{textAlign:'center',color:'white',fontFamily:'inherit'}}>Sorry! Couldn't find any matching results.</h2>)
)
}
</div>
)
}
Expand Down
10 changes: 5 additions & 5 deletions client/src/components/Searchbar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState,useEffect } from "react";
import React, { useState } from "react";
import "../styles/Searchbar.css";
import { Link, useNavigate } from "react-router-dom"
import logo from "../images/logo.png";
Expand All @@ -20,9 +20,9 @@ function Searchbar(){
const currentUser=useSelector(state=>state.user.currentUser);
const uploadOpen=useSelector(state=>state.navbar.uploadOpen);
const [query,setQuery]=useState("");
useEffect(()=>{
console.log(query);
},[query])
// useEffect(()=>{
// console.log(query);
// },[query])
const customStyling={
background:`url(${currentUser?.img}) no-repeat`,
backgroundSize:"40px",
Expand All @@ -41,7 +41,7 @@ function Searchbar(){
<div id="logo-container">
<div id="hamburger" onClick={()=>dispatch(setOpen())}><button type='button' className='nav-btn'><MenuIcon id="icon"/></button></div>
<Link to="/" style={{textDecoration: "none",fontSize: "14px"}}><div id="logo">
<img src={logo} height="25" width="40"></img><h2>MS Tube</h2>
<img src={logo} height="25" width="40" alt="logo"></img><h2>MS Tube</h2>
</div></Link>
</div>
<div id="search-area">
Expand Down

0 comments on commit 94d6179

Please sign in to comment.