From 809073c7310094ad3b8a16a4918a07e115eee93f Mon Sep 17 00:00:00 2001 From: amolsasane Date: Fri, 12 Jul 2024 00:19:55 +0530 Subject: [PATCH] feat: loader, auto scroll , bg image --- src/components/Gpt.js | 8 +------- src/components/GptSearch.js | 39 +++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/components/Gpt.js b/src/components/Gpt.js index ba4e8b6..ef30646 100644 --- a/src/components/Gpt.js +++ b/src/components/Gpt.js @@ -1,16 +1,10 @@ import React from "react"; import GptSearch from "./GptSearch"; import GptResults from "./GptResults"; -import { bgImage } from "../utils/constants"; const Gpt = () => { return ( -
+
diff --git a/src/components/GptSearch.js b/src/components/GptSearch.js index c855406..78b7724 100644 --- a/src/components/GptSearch.js +++ b/src/components/GptSearch.js @@ -1,22 +1,32 @@ -import React, { useRef } from "react"; +import React, { useRef, useState, useEffect } from "react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faChevronRight } from "@fortawesome/free-solid-svg-icons"; import langConstants from "../utils/languageConstants"; import { useDispatch, useSelector } from "react-redux"; import { addGptMovies, showError } from "../utils/gptSlice"; import { API_OPTIONS } from "../utils/constants"; +import { bgImage } from "../utils/constants"; const GptSearch = () => { const lang = useSelector((store) => store.lang.selectedLang); const errorMessage = useSelector((store) => store.gpt.error); - const showErrorMessage = useSelector((store) => store.gpt.movieResults); + const movieResults = useSelector((store) => store.gpt.movieResults); // renamed to movieResults const searchText = useRef(null); + const resultsRef = useRef(null); // Ref for results section const dispatch = useDispatch(); + const [loading, setLoading] = useState(false); + const [hasResults, setHasResults] = useState(false); // State to track if results are available const { GoogleGenerativeAI } = require("@google/generative-ai"); const genAI = new GoogleGenerativeAI(process.env.REACT_APP_GEMINI_API_KEY); const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); + useEffect(() => { + if (hasResults && resultsRef.current) { + resultsRef.current.scrollIntoView({ behavior: "smooth" }); + } + }, [hasResults]); + const handleSubmit = (e) => { e.preventDefault(); gptSearchClickHandler(); @@ -36,6 +46,8 @@ const GptSearch = () => { const gptSearchClickHandler = async () => { try { + setLoading(true); // Start loading + setHasResults(false); const promptQuery = "Act like a movie suggestion system and show results for the query: " + searchText.current.value + @@ -55,14 +67,22 @@ const GptSearch = () => { dispatch( addGptMovies({ movieNames: gptResult, movieResults: tmdbResult }) ); + setHasResults(true); } catch (error) { console.error("Error fetching GPT results", error); dispatch(showError(error.message)); + } finally { + setLoading(false); // Stop loading } }; return ( -
+

{langConstants[lang].gptHeading}

@@ -79,17 +99,28 @@ const GptSearch = () => { - {!showErrorMessage && errorMessage && ( + + {loading && ( +
+
+ Loading... +
+ )} + + {!movieResults && errorMessage && (

{langConstants[lang].gptError}

)} + +
); };