From be5b9645d7f8e0194de7a1702005fcd635338298 Mon Sep 17 00:00:00 2001 From: "Siddhesh.More" Date: Wed, 30 Oct 2024 18:48:00 +0530 Subject: [PATCH] update --- src/Services/api.service.js | 15 +++++- src/pages/Home.jsx | 99 ++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/src/Services/api.service.js b/src/Services/api.service.js index 1de6f33..0981246 100644 --- a/src/Services/api.service.js +++ b/src/Services/api.service.js @@ -94,6 +94,18 @@ export const rubix = createApi({ query: ({ pageNumber = 1, pageSize = 10 }) => `SubNet/GetAllSubnetworks?pageNumber=${pageNumber}&pageSize=${pageSize}`, providesTags: ["getTransAll"], }), + + + + + gnerateShortUrl: builder.mutation({ + query: (data) => ({ + url: `ShortUrl/create`, + method: "POST", + body: data, + }), + + }), }), }); @@ -110,5 +122,6 @@ export const { useGetDateWiseDataQuery, useGetSubnetCountQuery, useGetSubnetByIdQuery, - useGetSubnetAllQuery + useGetSubnetAllQuery, + useGnerateShortUrlMutation } = rubix; diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index 58d5e6c..47514f7 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -5,9 +5,12 @@ import { Container, FormControl, Heading, + HStack, + Icon, Input, InputGroup, InputLeftElement, + Text, useColorMode, VStack, } from "@chakra-ui/react"; @@ -22,14 +25,66 @@ import Pagination from "../components/Pagination"; import bannerImage from "../assets/images/bannerImg.png"; import bannerImageMobile from "../assets/images/bannerImgmobile.png"; import { BiSearchAlt } from "react-icons/bi"; +import { MdContentCopy } from "react-icons/md"; + + +import { motion, AnimatePresence } from "framer-motion"; // Import AnimatePresence and motion +import { useGnerateShortUrlMutation } from "../Services/api.service"; + +const AnimatedBox = motion(HStack); // Create an animated HStack const Home = () => { const [isSwitchOn, setIsSwitchOn] = useState(true); const { colorMode, toggleColorMode } = useColorMode(); + const [ linkVisible, setLinkVisible ] = useState(false) const [ searchTerm, setSearchTerm] = useState("") const navigate = useNavigate() + const [ shortURL, setShortURL ] = useState(null) + const [ isLoading, setIsLoading ] = useState(false) + + const[ genrateSortURL ] = useGnerateShortUrlMutation() + + // const handleGenrateShortURL = async () => { + // try { + // // Ensure searchTerm is converted to a string before passing to the API + // const res = await genrateSortURL(JSON.stringify(searchTerm)); + // console.log(res); + // } catch (error) { + // console.error("Error generating short URL:", error); + // } + // }; + + const handleGenrateShortURL = async () => { + setLinkVisible(true) + setIsLoading(true) + try { + const response = await fetch('https://rexplorerapi.azurewebsites.net/api/ShortUrl/create', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(searchTerm), // Sending searchTerm as a JSON string + }); + + if (!response.ok) { + throw new Error('Failed to generate short URL'); + } + + const result = await response.json(); + console.log(result); + setShortURL(result?.shortUrl) + setIsLoading(false) + // You can handle the result here, e.g., by updating a state with the short URL + } catch (error) { + console.error("Error generating short URL:", error); + } + }; + + + + return ( @@ -40,9 +95,9 @@ const Home = () => { backgroundSize="contain" backgroundRepeat="no-repeat"> - + - + { _focus={{ opacity:1, outline:'none', bg:colorMode === "light" ? "#4023A6" : "#565252" }} _active={{ opacity:0.9, outline:'none', bg:colorMode === "light" ? "#4023A6" : "#565252" }} color={'#fff'} + + onClick={handleGenrateShortURL} > Generate short url @@ -116,6 +173,44 @@ const Home = () => { + + + {/* Slide-down animated HStack */} + + {isLoading || linkVisible && ( + + + {shortURL} + + copyToClipboard(shortURL)} + /> + + )} +