chore: better short code fetching

This commit is contained in:
liyasthomas
2021-11-22 08:26:22 +05:30
parent 12a9dd1058
commit 222f0800d2

View File

@@ -333,8 +333,12 @@ const copyLinkIcon = hasNavigatorShare ? ref("share-2") : ref("copy")
const shareLink = ref("") const shareLink = ref("")
const shareLinkStatus = ref(t("request.copy_link")) const shareLinkStatus = ref(t("request.copy_link"))
const fetchingShareLink = ref(false) const fetchingShareLink = ref(false)
const shareLinkAvailable = ref(false)
const copyRequest = async () => { const copyRequest = async () => {
if (shareLinkAvailable.value) {
copyShareLink(shareLink.value)
} else {
shareLink.value = "" shareLink.value = ""
fetchingShareLink.value = true fetchingShareLink.value = true
shareLinkStatus.value = t("state.loading") shareLinkStatus.value = t("state.loading")
@@ -343,8 +347,18 @@ const copyRequest = async () => {
if (E.isLeft(shortcodeResult)) { if (E.isLeft(shortcodeResult)) {
$toast.error(`${shortcodeResult.left.error}`) $toast.error(`${shortcodeResult.left.error}`)
shareLink.value = `${t("error.something_went_wrong")}` shareLink.value = `${t("error.something_went_wrong")}`
shareLinkAvailable.value = false
} else if (E.isRight(shortcodeResult)) { } else if (E.isRight(shortcodeResult)) {
shareLink.value = shortcodeResult.right.createShortcode.id shareLink.value = `/${shortcodeResult.right.createShortcode.id}`
shareLinkAvailable.value = true
copyShareLink(shareLink.value)
}
fetchingShareLink.value = false
shareLinkStatus.value = shareLink.value
}
}
const copyShareLink = (shareLink: string) => {
if (navigator.share) { if (navigator.share) {
const time = new Date().toLocaleTimeString() const time = new Date().toLocaleTimeString()
const date = new Date().toLocaleDateString() const date = new Date().toLocaleDateString()
@@ -352,21 +366,18 @@ const copyRequest = async () => {
.share({ .share({
title: "Hoppscotch", title: "Hoppscotch",
text: `Hoppscotch • Open source API development ecosystem at ${time} on ${date}`, text: `Hoppscotch • Open source API development ecosystem at ${time} on ${date}`,
url: `https://hopp.sh/r/${shareLink.value}`, url: `https://hopp.sh/r${shareLink}`,
}) })
.then(() => {}) .then(() => {})
.catch(() => {}) .catch(() => {})
} else { } else {
copyLinkIcon.value = "check" copyLinkIcon.value = "check"
copyToClipboard(`https://hopp.sh/r/${shareLink.value}`) copyToClipboard(`https://hopp.sh/r${shareLink}`)
$toast.success(`${t("state.copied_to_clipboard")}`, { $toast.success(`${t("state.copied_to_clipboard")}`, {
icon: "content_paste", icon: "content_paste",
}) })
setTimeout(() => (copyLinkIcon.value = "copy"), 2000) setTimeout(() => (copyLinkIcon.value = "copy"), 2000)
} }
}
fetchingShareLink.value = false
shareLinkStatus.value = `/${shareLink.value}`
} }
const cycleUpMethod = () => { const cycleUpMethod = () => {