diff --git a/src/App.vue b/src/App.vue index c4a4fd37d..fc71ab4c5 100644 --- a/src/App.vue +++ b/src/App.vue @@ -104,31 +104,7 @@
-
- - -
- +
-const onSlideConfirmed = (): void => { - vehicleStore.showSlideToConfirm = false - vehicleStore.confirmed = true - console.log('Slide confirmed!') - // Additional logic here -} - -const cancelAction = (): void => { - vehicleStore.showSlideToConfirm = false - vehicleStore.confirmed = false - console.log('Slide canceled!') - // Additional logic here -} - const hideMouse = (): void => { document.body.classList.add('hide-cursor') } diff --git a/src/components/SlideToConfirm.vue b/src/components/SlideToConfirm.vue new file mode 100644 index 000000000..232cdbbe6 --- /dev/null +++ b/src/components/SlideToConfirm.vue @@ -0,0 +1,41 @@ + + + diff --git a/src/libs/slide-to-confirm.ts b/src/libs/slide-to-confirm.ts new file mode 100644 index 000000000..9a425789b --- /dev/null +++ b/src/libs/slide-to-confirm.ts @@ -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} A Promise that resolves if the action is successfully executed or rejects in case of cancellation or errors. + */ +export function slideToConfirm(actionFunc: () => void): Promise { + 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')) + } + }) + }) +} diff --git a/src/stores/mainVehicle.ts b/src/stores/mainVehicle.ts index 6e203e69b..76d9d0031 100644 --- a/src/stores/mainVehicle.ts +++ b/src/stores/mainVehicle.ts @@ -14,6 +14,7 @@ import { registerActionCallback, } from '@/libs/joystick/protocols/cockpit-actions' import { MavlinkManualControlManager } from '@/libs/joystick/protocols/mavlink-manual-control' +import { slideToConfirm } from '@/libs/slide-to-confirm' import type { ArduPilot } from '@/libs/vehicle/ardupilot/ardupilot' import type { ArduPilotParameterSetData } from '@/libs/vehicle/ardupilot/types' import * as Protocol from '@/libs/vehicle/protocol/protocol' @@ -110,48 +111,11 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { const statusText: StatusText = reactive({} as StatusText) const statusGPS: StatusGPS = reactive({} as StatusGPS) const genericVariables: Record = reactive({}) - const showSlideToConfirm = ref(false) - const confirmed = ref(false) const mode = ref(undefined) // eslint-disable-next-line @typescript-eslint/no-explicit-any const modes = ref>() - /** - * 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 { void } A Promise that resolves if the action is successfully executed or rejects in case of cancellation or errors. - */ - function slideToConfirm(actionFunc: () => void): Promise { - return new Promise((resolve, reject) => { - // Show slide to confirm component - showSlideToConfirm.value = true - - // Watch for changes on confirmed variable - /* eslint-disable @typescript-eslint/no-unused-vars */ - const stopWatching = watch(confirmed, (newValue, oldValue) => { - 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')) - } - }) - }) - } - /** * Check if vehicle is online (no more than 5 seconds passed since last heartbeat) * @returns { boolean } True if vehicle is online @@ -502,7 +466,5 @@ export const useMainVehicleStore = defineStore('main-vehicle', () => { configurationPages, rtcConfiguration, genericVariables, - showSlideToConfirm, - confirmed, } })