|
1 | | -import React, { useRef } from 'react'; |
| 1 | +import React, { useRef, useState } from 'react'; |
2 | 2 | import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
3 | 3 | import { faCopy } from '@fortawesome/free-solid-svg-icons'; |
4 | 4 | import Swal from 'sweetalert2/dist/sweetalert2.js'; |
5 | 5 | import 'sweetalert2/src/sweetalert2.scss'; |
6 | | -import axios from "axios"; |
| 6 | +import axios from 'axios'; |
7 | 7 |
|
8 | 8 | const Hero = () => { |
| 9 | + const baseURL = 'http://127.0.0.1:8000/links/create/'; |
| 10 | + const [shortUrl, setShortUrl] = useState(''); |
9 | 11 | const inputRef = useRef(null); |
10 | 12 |
|
| 13 | + const GetShort = async () => { |
| 14 | + const urlToShorten = document.getElementById('url-input').value; |
| 15 | + try { |
| 16 | + const response = await axios.post( |
| 17 | + baseURL, |
| 18 | + { url: urlToShorten }, |
| 19 | + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } } |
| 20 | + ); |
| 21 | + if (response.data) { |
| 22 | + const newShortUrl = response.data["short-link"]; |
| 23 | + setShortUrl(newShortUrl); |
| 24 | + Swal.fire({ |
| 25 | + position: 'top-end', |
| 26 | + icon: 'success', |
| 27 | + title: 'URL shortened successfully!', |
| 28 | + showConfirmButton: false, |
| 29 | + timer: 1500, |
| 30 | + }); |
| 31 | + } |
| 32 | + } catch (error) { |
| 33 | + let errorMessage = 'Failed to shorten URL!'; |
| 34 | + if (error.response && error.response.data) { |
| 35 | + errorMessage = error.response.data.message || errorMessage; |
| 36 | + } |
| 37 | + |
| 38 | + Swal.fire({ |
| 39 | + position: 'top-end', |
| 40 | + icon: 'error', |
| 41 | + title: errorMessage, |
| 42 | + showConfirmButton: false, |
| 43 | + timer: 1500, |
| 44 | + }); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
11 | 48 | const copyToClipboard = () => { |
12 | 49 | if (inputRef.current) { |
13 | 50 | inputRef.current.select(); |
14 | 51 | inputRef.current.setSelectionRange(0, 99999); // For mobile devices |
15 | 52 |
|
16 | 53 | try { |
17 | | - var successful = document.execCommand('copy'); |
18 | | - var msg = successful ? 'successful' : 'unsuccessful'; |
| 54 | + document.execCommand('copy'); |
19 | 55 | Swal.fire({ |
20 | | - position: "top-end", |
21 | | - icon: "success", |
22 | | - title: "Your url succussfully copied!", |
| 56 | + position: 'top-end', |
| 57 | + icon: 'success', |
| 58 | + title: 'URL copied to clipboard!', |
23 | 59 | showConfirmButton: false, |
24 | | - timer: 1500 |
| 60 | + timer: 1500, |
25 | 61 | }); |
26 | 62 | } catch (err) { |
27 | 63 | Swal.fire({ |
28 | | - position: "top-end", |
29 | | - icon: "error", |
30 | | - title: "Something went wrong!", |
| 64 | + position: 'top-end', |
| 65 | + icon: 'error', |
| 66 | + title: 'Failed to copy URL!', |
31 | 67 | showConfirmButton: false, |
32 | | - timer: 1500 |
| 68 | + timer: 1500, |
33 | 69 | }); |
34 | 70 | } |
35 | 71 | } |
36 | 72 | }; |
37 | 73 |
|
38 | 74 | return ( |
39 | 75 | <> |
40 | | - <div className="relative isolate overflow-hidden bg-gray-900 px-4 py-16 sm:py-24 lg:py-32 rounded-xl mx-3" style={{ boxShadow: "0 0 10px 1px var(--primary)" }}> |
| 76 | + <div className="relative isolate overflow-hidden bg-gray-900 px-4 py-16 sm:py-24 lg:py-32 rounded-xl mx-3" style={{ boxShadow: '0 0 10px 1px var(--primary)' }}> |
41 | 77 | <div className="flex justify-center items-center"> |
42 | 78 | <div className="flex flex-col justify-center items-center"> |
43 | 79 | <h2 className="text-3xl font-bold tracking-tight text-white sm:text-4xl text-center">Smart and powerful short links</h2> |
44 | 80 | <p className="mt-4 text-lg leading-8 text-gray-300 text-center">Stay in control of your links with advanced features for shortening and tracking.</p> |
45 | 81 | <div className="w-full mt-5 flex flex-col items-center"> |
46 | 82 | <div className="flex w-full max-w-md space-x-2"> |
47 | | - <label htmlFor="email-address" className="sr-only">Your URL ...</label> |
48 | | - <input id="email-address" name="email" type="email" autoComplete="email" required className="flex-1 rounded-md border-0 bg-white/5 px-3.5 py-2 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" placeholder="https://YOUR URL.com" /> |
49 | | - <button type="submit" className="flex-none rounded-md bg-primary px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm bc-primary hover:bg-cyan-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500">Shorten</button> |
| 83 | + <label htmlFor="url-input" className="sr-only">Your URL ...</label> |
| 84 | + <input id="url-input" name="text" type="text" autoComplete="off" required className="flex-1 rounded-md border-0 bg-white/5 px-3.5 py-2 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" placeholder="https://YOUR URL.com" /> |
| 85 | + <button type="button" onClick={GetShort} className="flex-none rounded-md bg-primary px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm bc-primary hover:bg-cyan-800 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500">Shorten</button> |
50 | 86 | </div> |
51 | 87 | <div className="relative w-full max-w-sm mt-3"> |
52 | | - <input disabled ref={inputRef} readOnly id="ShortedUrl" type="text" autoComplete="email" required className="w-full pr-12 rounded-md border-0 bg-white/5 px-3.5 py-2 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" placeholder="shorted link placed here" /> |
| 88 | + <input ref={inputRef} readOnly id="ShortedUrl" type="text" value={shortUrl} className="w-full pr-12 rounded-md border-0 bg-white/5 px-3.5 py-2 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6" placeholder="shortened link placed here" /> |
53 | 89 | <button onClick={copyToClipboard} className="absolute inset-y-0 right-0 px-3 flex items-center text-white hover:text-indigo-500"> |
54 | 90 | <FontAwesomeIcon className="tc-primary text-xl" icon={faCopy} /> |
55 | 91 | </button> |
|
0 commit comments