Skip to content

Commit b080f63

Browse files
committed
feat : connect backend to frontend
1 parent e11ee30 commit b080f63

File tree

1 file changed

+53
-17
lines changed
  • web/csr/UrlShorter/src/components

1 file changed

+53
-17
lines changed

web/csr/UrlShorter/src/components/Hero.jsx

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,91 @@
1-
import React, { useRef } from 'react';
1+
import React, { useRef, useState } from 'react';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
33
import { faCopy } from '@fortawesome/free-solid-svg-icons';
44
import Swal from 'sweetalert2/dist/sweetalert2.js';
55
import 'sweetalert2/src/sweetalert2.scss';
6-
import axios from "axios";
6+
import axios from 'axios';
77

88
const Hero = () => {
9+
const baseURL = 'http://127.0.0.1:8000/links/create/';
10+
const [shortUrl, setShortUrl] = useState('');
911
const inputRef = useRef(null);
1012

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+
1148
const copyToClipboard = () => {
1249
if (inputRef.current) {
1350
inputRef.current.select();
1451
inputRef.current.setSelectionRange(0, 99999); // For mobile devices
1552

1653
try {
17-
var successful = document.execCommand('copy');
18-
var msg = successful ? 'successful' : 'unsuccessful';
54+
document.execCommand('copy');
1955
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!',
2359
showConfirmButton: false,
24-
timer: 1500
60+
timer: 1500,
2561
});
2662
} catch (err) {
2763
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!',
3167
showConfirmButton: false,
32-
timer: 1500
68+
timer: 1500,
3369
});
3470
}
3571
}
3672
};
3773

3874
return (
3975
<>
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)' }}>
4177
<div className="flex justify-center items-center">
4278
<div className="flex flex-col justify-center items-center">
4379
<h2 className="text-3xl font-bold tracking-tight text-white sm:text-4xl text-center">Smart and powerful short links</h2>
4480
<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>
4581
<div className="w-full mt-5 flex flex-col items-center">
4682
<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>
5086
</div>
5187
<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" />
5389
<button onClick={copyToClipboard} className="absolute inset-y-0 right-0 px-3 flex items-center text-white hover:text-indigo-500">
5490
<FontAwesomeIcon className="tc-primary text-xl" icon={faCopy} />
5591
</button>

0 commit comments

Comments
 (0)