Sharkey/packages/frontend/src/components/form/suspense.vue

101 lines
1.8 KiB
Vue
Raw Normal View History

2021-11-28 13:07:37 +02:00
<template>
2023-04-01 07:42:40 +03:00
<Transition :name="defaultStore.state.animation ? 'fade' : ''" mode="out-in">
2021-11-28 13:07:37 +02:00
<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> {{ $ts.somethingHappened }}</div>
2022-12-20 01:35:49 +02:00
<MkButton inline class="retry" @click="retry"><i class="ti ti-reload"></i> {{ $ts.retry }}</MkButton>
2021-11-28 13:07:37 +02:00
</div>
</div>
2022-12-30 06:37:14 +02:00
</Transition>
2021-11-28 13:07:37 +02:00
</template>
<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue';
import MkButton from '@/components/MkButton.vue';
2023-04-01 07:42:40 +03:00
import { defaultStore } from '@/store';
2021-11-28 13:07:37 +02:00
export default defineComponent({
components: {
2022-12-20 01:35:49 +02:00
MkButton,
2021-11-28 13:07:37 +02:00
},
props: {
p: {
type: Function as PropType<() => Promise<any>>,
required: true,
2022-12-20 01:35:49 +02:00
},
2021-11-28 13:07:37 +02:00
},
setup(props, context) {
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();
}, {
2022-12-20 01:35:49 +02:00
immediate: true,
2021-11-28 13:07:37 +02:00
});
const retry = () => {
process();
};
return {
pending,
resolved,
rejected,
result,
retry,
2023-04-01 07:42:40 +03:00
defaultStore,
2021-11-28 13:07:37 +02:00
};
2022-12-20 01:35:49 +02:00
},
2021-11-28 13:07:37 +02:00
});
</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>