summaryrefslogtreecommitdiff
path: root/packages/frontend/src/components/form/suspense.vue
blob: 9b39858ca192e3b937899d4bb488a75383abd749 (plain)
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
<template>
<Transition :name="defaultStore.state.animation ? 'fade' : ''" mode="out-in">
	<div v-if="pending">
		<MkLoading/>
	</div>
	<div v-else-if="resolved">
		<slot :result="result"></slot>
	</div>
	<div v-else>
		<div class="wszdbhzo">
			<div><i class="ti ti-alert-triangle"></i> {{ i18n.ts.somethingHappened }}</div>
			<MkButton inline class="retry" @click="retry"><i class="ti ti-reload"></i> {{ i18n.ts.retry }}</MkButton>
		</div>
	</div>
</Transition>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue';
import MkButton from '@/components/MkButton.vue';
import { defaultStore } from '@/store';
import { i18n } from '@/i18n';

const props = defineProps<{
	p: () => Promise<any>;
}>();

const pending = ref(true);
const resolved = ref(false);
const rejected = ref(false);
const result = ref(null);

const process = () => {
	if (props.p == null) {
		return;
	}
	const promise = props.p();
	pending.value = true;
	resolved.value = false;
	rejected.value = false;
	promise.then((_result) => {
		pending.value = false;
		resolved.value = true;
		result.value = _result;
	});
	promise.catch(() => {
		pending.value = false;
		rejected.value = true;
	});
};

watch(() => props.p, () => {
	process();
}, {
	immediate: true,
});

const retry = () => {
	process();
};
</script>

<style lang="scss" scoped>
.fade-enter-active,
.fade-leave-active {
	transition: opacity 0.125s ease;
}
.fade-enter-from,
.fade-leave-to {
	opacity: 0;
}

.wszdbhzo {
	padding: 16px;
	text-align: center;

	> .retry {
		margin-top: 16px;
	}
}
</style>