paricafe/src/client/components/debobigego/suspense.vue

102 lines
1.9 KiB
Vue
Raw Normal View History

<template>
2021-04-16 09:04:25 -05:00
<transition name="fade" mode="out-in">
2021-09-29 10:50:45 -05:00
<div class="_debobigegoItem" v-if="pending">
<div class="_debobigegoPanel">
2021-04-16 09:04:25 -05:00
<MkLoading/>
</div>
</div>
2021-09-29 10:50:45 -05:00
<div v-else-if="resolved" class="_debobigegoItem">
2021-04-16 09:04:25 -05:00
<slot :result="result"></slot>
</div>
2021-09-29 10:50:45 -05:00
<div class="_debobigegoItem" v-else>
<div class="_debobigegoPanel eiurkvay">
2021-04-22 22:00:07 -05:00
<div><i class="fas fa-exclamation-triangle"></i> {{ $ts.somethingHappened }}</div>
<MkButton inline @click="retry" class="retry"><i class="fas fa-redo-alt"></i> {{ $ts.retry }}</MkButton>
2021-04-16 09:04:25 -05:00
</div>
</div>
</transition>
</template>
<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue';
2021-09-29 10:50:45 -05:00
import './debobigego.scss';
2021-04-22 22:00:07 -05:00
import MkButton from '@client/components/ui/button.vue';
export default defineComponent({
2021-04-22 22:00:07 -05:00
components: {
MkButton
},
props: {
p: {
type: Function as PropType<() => Promise<any>>,
required: true,
}
},
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();
}, {
immediate: true
});
const retry = () => {
process();
};
return {
pending,
resolved,
rejected,
result,
retry,
};
}
});
</script>
<style lang="scss" scoped>
2021-04-16 09:04:25 -05:00
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.125s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
2021-04-22 22:00:07 -05:00
.eiurkvay {
padding: 16px;
text-align: center;
> .retry {
margin-top: 16px;
}
}
</style>