-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathConfirmsGate.vue
115 lines (94 loc) · 2.73 KB
/
ConfirmsGate.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<script setup lang="ts">
import {
type PropType,
defineAsyncComponent,
ref,
shallowRef,
useSlots,
} from "vue";
import BaseModal from "@/components/modals/BaseModal.vue";
import type { Gate } from "@m-media/vue3-gate-keeper/src/gateKeeper";
import { useGateKeeper } from "@m-media/vue3-gate-keeper";
const props = defineProps({
/** The title of the modal. It will be in the header of the opened modal. */
title: {
type: String,
required: true,
},
/** The gate or gates to check before confirming. */
gate: {
type: [Array, String] as PropType<string | Gate | (Gate | string)[]>,
required: true,
},
});
const slots = useSlots();
const formToUse = ref();
const emits = defineEmits(["confirmed"]);
const modal = ref();
const isConfirming = ref(false);
const interceptedByGate = ref("");
const runGates = useGateKeeper() as Function;
const startConfirming = async () => {
if (props.gate === undefined) {
return;
}
isConfirming.value = true;
const gateResponse = await runGates(props.gate).handle();
if (gateResponse?.data === undefined) {
return HandleConfirmed();
}
interceptedByGate.value = gateResponse.gate;
if (
gateResponse.data === false &&
!slots["confirmationElement:" + interceptedByGate.value]
) {
return HandleFailed();
}
if (typeof gateResponse.data === "string") {
formToUse.value = gateResponse.data;
setElement();
}
modal.value.openModal();
};
const HandleConfirmed = () => {
emits("confirmed");
modal.value.closeModal();
};
const HandleFailed = () => {
modal.value.closeModal();
};
const ConfirmationElement = shallowRef();
const setElement = () => {
ConfirmationElement.value = defineAsyncComponent(
() => import(`./../../forms/${formToUse.value}.vue`)
);
};
</script>
<template>
<span>
<span @click.prevent="startConfirming">
<!-- @slot This is the slot for the trigger of the confirmation. You can use this to create a button or any other element to trigger the confirmation. It will be wrapped in a click handler that will trigger the confirmation modal. -->
<slot :isConfirming="isConfirming" />
</span>
<base-modal
ref="modal"
:title="title"
:showTrigger="false"
:showFooter="false"
@closed="isConfirming = false"
>
<!-- @slot This is the slot for the confirmation element. This is the form or element that will be shown in the modal. -->
<slot
:name="'confirmationElement:' + interceptedByGate"
:success="startConfirming"
:fail="HandleFailed"
>
<component
:is="ConfirmationElement"
v-if="formToUse && modal?.isModalOpen"
@success="startConfirming"
/>
</slot>
</base-modal>
</span>
</template>