Skip to content

Commit

Permalink
Merge pull request #28 from formsible/feat/components
Browse files Browse the repository at this point in the history
redirect component update
  • Loading branch information
tewnut authored Oct 30, 2024
2 parents 5382261 + 05b6732 commit 3c18c26
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 49 deletions.
2 changes: 2 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare module 'vue' {
Html_block_list: typeof import('./src/components/html_block_list/index.vue')['default']
Html_block_media: typeof import('./src/components/html_block_media/index.vue')['default']
Image_choice: typeof import('./src/components/image_choice/index.vue')['default']
InputText: typeof import('primevue/inputtext')['default']
Likert_scale: typeof import('./src/components/likert_scale/index.vue')['default']
List: typeof import('./src/components/list/index.vue')['default']
Locale_selector: typeof import('./src/components/locale_selector/index.vue')['default']
Expand All @@ -50,6 +51,7 @@ declare module 'vue' {
Signature: typeof import('./src/components/signature/index.vue')['default']
Slider: typeof import('./src/components/slider/index.vue')['default']
Term_of_service: typeof import('./src/components/term_of_service/index.vue')['default']
Textarea: typeof import('primevue/textarea')['default']
Time_picker: typeof import('./src/components/time_picker/index.vue')['default']
Website: typeof import('./src/components/website/index.vue')['default']
}
Expand Down
62 changes: 16 additions & 46 deletions src/components/action_redirect/index.vue
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useTimeoutFn } from '@vueuse/core'
import { onMounted } from 'vue'
import type { ActionRedirectProperties } from '~/types'
// Props declaration
interface Props {
display: ActionRedirectProperties
}
const props = defineProps<Props>()
// Countdown timer and progress
const countdown = ref(5)
const progress = ref(100)
const interval = ref<NodeJS.Timeout | null>(null)
const redirecting = ref(true)
// Automatically redirect after 5 seconds
onMounted(() => {
interval.value = setInterval(() => {
countdown.value -= 1
progress.value = (countdown.value / 5) * 100
if (countdown.value === 0) {
clearInterval(interval.value!)
window.location.href = props.display.src as string
}
}, 1000)
})
// Option to cancel redirection
const cancelRedirect = () => {
redirecting.value = false
if (interval.value) {
clearInterval(interval.value)
}
// Countdown complete event
const onCountdownComplete = () => {
window.location.href = props.display.url
}
// Cleanup on component unmount
onBeforeUnmount(() => {
if (interval.value) {
clearInterval(interval.value)
}
})
</script>
// Timeout composable by vusue
const { start } = useTimeoutFn(
onCountdownComplete,
props.display.countDown || 3000,
)
// Hooks
onMounted(start)
</script>
<template>
<div class="p-4 max-w-md mx-auto">
<div v-if="redirecting">
<p class="text-lg mb-4">
You are being redirected to
<strong>{{ display.src }}</strong> in {{ countdown }} seconds.
</p>
<div class="relative w-full bg-surface-200 rounded-full h-2 mb-4">
<div :style="{ width: progress + '%' }"></div>
</div>
<Button severity="danger" @click="cancelRedirect"> Cancel </Button>
</div>
<div v-else>
<p class="text-lg text-red-500">Redirection has been canceled.</p>
</div>
<div class="text-center">
<p>You are being directed to {{ display.url }}.</p>
</div>
</template>
3 changes: 1 addition & 2 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,7 @@
"display": {
"label": "Action Redirect",
"component": "action_redirect",
"src": "https://example.com",
"content": "You are being redirected to {url} in {x} seconds"
"url": "https://example.com"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/types/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export interface EmbedProperties extends DisplayProperties {
* Properties for redirect.
*/
export interface ActionRedirectProperties extends DisplayProperties {
src: string; // Source URL of the embedded document
url: string; // Source URL of the embedded document
countDown?: number
}

// ==============================
Expand Down

0 comments on commit 3c18c26

Please sign in to comment.