-
Notifications
You must be signed in to change notification settings - Fork 18
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
4 changed files
with
126 additions
and
88 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
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,60 @@ | ||
import { getNetwork } from '@/networks'; | ||
import { NetworkID } from '@/types'; | ||
|
||
type SpaceValidationResult = { value: string; valid: boolean }; | ||
|
||
export function useSpaceInputValidation( | ||
networkId: Ref<NetworkID>, | ||
value: Ref<string> | ||
) { | ||
const loading = ref(false); | ||
const validationResult = ref(null as SpaceValidationResult | null); | ||
|
||
const network = computed(() => getNetwork(networkId.value)); | ||
const error = computed(() => { | ||
if (validationResult.value === null) return null; | ||
if (validationResult.value.value !== value.value) return null; | ||
if (validationResult.value.valid) return null; | ||
|
||
return `Space ${validationResult.value.value} not found`; | ||
}); | ||
|
||
watchDebounced( | ||
value, | ||
async value => { | ||
loading.value = true; | ||
|
||
try { | ||
if (!value || value === validationResult.value?.value) { | ||
validationResult.value = { | ||
value, | ||
valid: true | ||
}; | ||
return; | ||
} | ||
|
||
const space = await network.value.api.loadSpace(value); | ||
validationResult.value = { | ||
value, | ||
valid: !!space | ||
}; | ||
} catch (e) { | ||
console.error(e); | ||
|
||
validationResult.value = { | ||
value, | ||
valid: false | ||
}; | ||
} finally { | ||
loading.value = false; | ||
} | ||
}, | ||
{ debounce: 500, immediate: true } | ||
); | ||
|
||
return { | ||
loading, | ||
validationResult, | ||
error | ||
}; | ||
} |