mizzkey/src/client/components/form/suspense.vue

93 lines
1.6 KiB
Vue
Raw Normal View History

<template>
2021-04-16 16:04:25 +02:00
<transition name="fade" mode="out-in">
<div class="_formItem" v-if="pending">
<div class="_formPanel">
<MkLoading/>
</div>
</div>
2021-04-17 07:06:32 +02:00
<FormGroup v-else-if="resolved" class="_formItem">
2021-04-16 16:04:25 +02:00
<slot :result="result"></slot>
2021-04-17 07:06:32 +02:00
</FormGroup>
2021-04-16 16:04:25 +02:00
<div class="_formItem" v-else>
<div class="_formPanel">
error!
<button @click="retry">retry</button>
</div>
</div>
</transition>
</template>
<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue';
import './form.scss';
2021-04-17 07:06:32 +02:00
import FormGroup from './group.vue';
export default defineComponent({
2021-04-17 07:06:32 +02:00
components: {
FormGroup,
},
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 16:04:25 +02:00
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.125s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>