-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<template> | ||
<IconField class="w-full"> | ||
<InputText | ||
:model-value="modelValue" | ||
class="w-full" | ||
:placeholder="placeholder" | ||
:invalid="validationState === UrlValidationState.INVALID" | ||
@update:model-value="handleInput" | ||
@blur="validateUrl" | ||
/> | ||
<InputIcon | ||
:class="{ | ||
'pi pi-spin pi-spinner text-neutral-400': | ||
validationState === UrlValidationState.LOADING, | ||
'pi pi-check text-green-500': | ||
validationState === UrlValidationState.VALID, | ||
'pi pi-times text-red-500': | ||
validationState === UrlValidationState.INVALID | ||
}" | ||
/> | ||
</IconField> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import IconField from 'primevue/iconfield' | ||
import InputIcon from 'primevue/inputicon' | ||
import InputText from 'primevue/inputtext' | ||
import { ref } from 'vue' | ||
import { isValidUrl } from '@/utils/formatUtil' | ||
import { checkUrlReachable } from '@/utils/networkUtil' | ||
const props = defineProps<{ | ||
modelValue: string | ||
placeholder?: string | ||
}>() | ||
const emit = defineEmits<{ | ||
'update:modelValue': [value: string] | ||
}>() | ||
enum UrlValidationState { | ||
IDLE = 'IDLE', | ||
LOADING = 'LOADING', | ||
VALID = 'VALID', | ||
INVALID = 'INVALID' | ||
} | ||
const validationState = ref<UrlValidationState>(UrlValidationState.IDLE) | ||
const handleInput = (value: string) => { | ||
emit('update:modelValue', value) | ||
// Reset validation state when user types | ||
validationState.value = UrlValidationState.IDLE | ||
} | ||
const validateUrl = async () => { | ||
const url = props.modelValue.trim() | ||
// Reset state | ||
validationState.value = UrlValidationState.IDLE | ||
// Skip validation if empty | ||
if (!url) return | ||
// First check if it's a valid URL format | ||
if (!isValidUrl(url)) { | ||
validationState.value = UrlValidationState.INVALID | ||
return | ||
} | ||
// Then check if URL is reachable | ||
validationState.value = UrlValidationState.LOADING | ||
try { | ||
const reachable = await checkUrlReachable(url) | ||
validationState.value = reachable | ||
? UrlValidationState.VALID | ||
: UrlValidationState.INVALID | ||
} catch { | ||
validationState.value = UrlValidationState.INVALID | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import axios from 'axios' | ||
|
||
const VALID_STATUS_CODES = [200, 201, 301, 302, 307, 308] | ||
export const checkUrlReachable = async (url: string): Promise<boolean> => { | ||
try { | ||
const response = await axios.head(url) | ||
// Additional check for successful response | ||
return VALID_STATUS_CODES.includes(response.status) | ||
} catch { | ||
return false | ||
} | ||
} |