|
1 | 1 | <script>
|
2 | 2 | export let longUrl = '';
|
3 | 3 | export let shortUrl = '';
|
4 |
| - export let customUrl = ''; // New input for custom URL |
| 4 | + export let customUrl = ''; |
5 | 5 | export let errorMessage = '';
|
6 | 6 | export let expiry = '';
|
7 | 7 | export let validationError = '';
|
8 | 8 | export let isCopied = false;
|
9 |
| - export let showAccordion = false; // Controls the accordion |
| 9 | + export let showAccordion = false; |
10 | 10 |
|
11 | 11 | // Function to validate the custom URL input
|
12 | 12 | const validateCustomUrl = (url) => {
|
13 |
| - // Ensure no spaces or illegal characters (allow only alphanumeric, underscores, and dashes) |
14 | 13 | const customUrlRegex = /^[a-zA-Z0-9_-]+$/;
|
15 | 14 | return customUrlRegex.test(url);
|
16 | 15 | };
|
17 | 16 |
|
| 17 | + // Function to ensure URL has https:// |
| 18 | + const ensureHttps = (url) => { |
| 19 | + if (!url.startsWith('http')) { |
| 20 | + return `https://${url}`; |
| 21 | + } |
| 22 | + return url; |
| 23 | + }; |
| 24 | +
|
| 25 | + const handleInputChange = (event) => { |
| 26 | + longUrl = ensureHttps(event.target.value); |
| 27 | + }; |
| 28 | +
|
18 | 29 | export let shortenUrl = async () => {
|
19 | 30 | if (!longUrl) {
|
20 | 31 | validationError = 'Please enter a valid URL.';
|
21 | 32 | return;
|
22 | 33 | }
|
| 34 | +
|
23 | 35 | if (customUrl && !validateCustomUrl(customUrl)) {
|
24 | 36 | validationError =
|
25 | 37 | 'Custom URL contains spaces or invalid characters. Only alphanumeric, dashes, and underscores are allowed.';
|
26 | 38 | return;
|
27 | 39 | }
|
28 |
| - validationError = ''; // Clear validation error |
| 40 | + validationError = ''; |
29 | 41 |
|
30 | 42 | try {
|
31 | 43 | errorMessage = '';
|
32 |
| - shortUrl = ''; // Clear previous result |
| 44 | + shortUrl = ''; |
33 | 45 |
|
34 |
| - // Mocked shortening for UI testing |
35 | 46 | shortUrl = `${window.location.origin}/${customUrl || 'generated-url'}`;
|
36 | 47 |
|
37 |
| - // Collapse the accordion |
38 | 48 | showAccordion = false;
|
39 | 49 | } catch (error) {
|
40 | 50 | console.error('Error shortening URL:', error);
|
|
65 | 75 | id="long-url"
|
66 | 76 | type="url"
|
67 | 77 | bind:value={longUrl}
|
| 78 | + on:input={handleInputChange} |
68 | 79 | placeholder="Enter your URL"
|
69 | 80 | class="w-full p-3 border border-gray-300 rounded-lg shadow-sm focus:ring focus:ring-blue-300 focus:outline-none"
|
70 | 81 | required
|
|
112 | 123 |
|
113 | 124 | <!-- Expiry Date Input -->
|
114 | 125 | <div>
|
115 |
| - <label for="expiry" class="block text-gray-700 font-medium mb-2" |
116 |
| - >Expiry Date & Time</label |
117 |
| - > |
| 126 | + <label for="expiry" class="block text-gray-700 font-medium mb-2">Expiry Date & Time</label> |
118 | 127 | <input
|
119 | 128 | id="expiry"
|
120 | 129 | type="datetime-local"
|
|
0 commit comments