-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved slide to confirm functions into components
- Loading branch information
1 parent
7e5aa52
commit 25456dd
Showing
4 changed files
with
84 additions
and
82 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,41 @@ | ||
<template> | ||
<div v-if="showSlideToConfirm"> | ||
<div class="flex items-center space-x-4 mb-3"> | ||
<slide-unlock | ||
ref="vueslideunlock" | ||
:auto-width="false" | ||
:circle="true" | ||
:disabled="false" | ||
:noanimate="false" | ||
:width="400" | ||
:height="50" | ||
text="slide to confirm" | ||
success-text="action confirmed" | ||
name="slideunlock" | ||
class="slide-unlock" | ||
@completed="onSlideConfirmed()" | ||
/> | ||
<button class="w-12 h-12 flex items-center justify-center rounded-full bg-white text-gray" @click="cancelAction"> | ||
X | ||
</button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import SlideUnlock from 'vue-slide-unlock' | ||
import { confirmed, showSlideToConfirm } from '@/libs/slide-to-confirm' | ||
const onSlideConfirmed = (): void => { | ||
showSlideToConfirm.value = false | ||
confirmed.value = true | ||
console.log('Slide confirmed!') | ||
} | ||
const cancelAction = (): void => { | ||
showSlideToConfirm.value = false | ||
confirmed.value = false | ||
console.log('Slide canceled!') | ||
} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ref, watch } from 'vue' // Adjust this import based on your Vue version | ||
|
||
// Reactive variables (if they are not provided from outside) | ||
export const showSlideToConfirm = ref(false) | ||
export const confirmed = ref(false) | ||
|
||
/** | ||
* Calls the provided action function if the user confirms through the slide-to-confirm component. | ||
* @param {() => void} actionFunc - A function representing the action to be confirmed. | ||
* @returns {Promise<void>} A Promise that resolves if the action is successfully executed or rejects in case of cancellation or errors. | ||
*/ | ||
export function slideToConfirm(actionFunc: () => void): Promise<void> { | ||
console.log('slideToConfirm') | ||
return new Promise((resolve, reject) => { | ||
// Show slide to confirm component | ||
showSlideToConfirm.value = true | ||
|
||
// Watch for changes on confirmed variable | ||
const stopWatching = watch(confirmed, (newValue) => { | ||
if (newValue === true) { | ||
// Stop the watcher to prevent memory leaks | ||
stopWatching() | ||
|
||
confirmed.value = false | ||
|
||
// Execute the provided action function | ||
try { | ||
actionFunc() | ||
resolve() | ||
} catch (error) { | ||
reject(error) | ||
} | ||
} | ||
if (showSlideToConfirm.value === false) { | ||
stopWatching() | ||
reject(new Error('User cancelled the action')) | ||
} | ||
}) | ||
}) | ||
} |
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