client: refactor form suspense to composition API

This commit is contained in:
Johann150 2022-08-02 11:50:46 +02:00
parent 9b5d740530
commit a24e0e0648
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -1,58 +1,38 @@
<template>
<transition :name="$store.state.animation ? 'fade' : ''" mode="out-in">
<div v-if="pending">
<transition :name="defaultStore.state.animation ? 'fade' : ''" mode="out-in">
<div v-if="state == 'pending'">
<MkLoading/>
</div>
<div v-else-if="resolved">
<div v-else-if="state == 'resolved'">
<slot :result="result"></slot>
</div>
<div v-else>
<div class="wszdbhzo">
<div><i class="fas fa-exclamation-triangle"></i> {{ $ts.somethingHappened }}</div>
<MkButton inline class="retry" @click="retry"><i class="fas fa-redo-alt"></i> {{ $ts.retry }}</MkButton>
</div>
<MkError @retry="process"/>
</div>
</transition>
</template>
<script lang="ts">
import { defineComponent, PropType, ref, watch } from 'vue';
<script lang="ts" setup>
import { watch } from 'vue';
import MkButton from '@/components/ui/button.vue';
import { defaultStore } from '@/store';
export default defineComponent({
components: {
MkButton
},
const props = defineProps<{
p: () => Promise<any>;
}>();
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);
let state: 'pending' | 'resolved' | 'rejected' = $ref('pending');
let 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;
// this might be a retry so reset the state
state = 'pending';
props.p?.().then((_result) => {
result = _result;
state = 'resolved';
}, () => {
state = 'rejected';
});
};
@ -61,20 +41,6 @@ export default defineComponent({
}, {
immediate: true
});
const retry = () => {
process();
};
return {
pending,
resolved,
rejected,
result,
retry,
};
}
});
</script>
<style lang="scss" scoped>