forked from FoundKeyGang/FoundKey
client: refactor form suspense to composition API
This commit is contained in:
parent
9b5d740530
commit
a24e0e0648
1 changed files with 26 additions and 60 deletions
|
@ -1,79 +1,45 @@
|
||||||
<template>
|
<template>
|
||||||
<transition :name="$store.state.animation ? 'fade' : ''" mode="out-in">
|
<transition :name="defaultStore.state.animation ? 'fade' : ''" mode="out-in">
|
||||||
<div v-if="pending">
|
<div v-if="state == 'pending'">
|
||||||
<MkLoading/>
|
<MkLoading/>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="resolved">
|
<div v-else-if="state == 'resolved'">
|
||||||
<slot :result="result"></slot>
|
<slot :result="result"></slot>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<div class="wszdbhzo">
|
<MkError @retry="process"/>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
</transition>
|
</transition>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts" setup>
|
||||||
import { defineComponent, PropType, ref, watch } from 'vue';
|
import { watch } from 'vue';
|
||||||
import MkButton from '@/components/ui/button.vue';
|
import MkButton from '@/components/ui/button.vue';
|
||||||
|
import { defaultStore } from '@/store';
|
||||||
|
|
||||||
export default defineComponent({
|
const props = defineProps<{
|
||||||
components: {
|
p: () => Promise<any>;
|
||||||
MkButton
|
}>();
|
||||||
},
|
|
||||||
|
|
||||||
props: {
|
let state: 'pending' | 'resolved' | 'rejected' = $ref('pending');
|
||||||
p: {
|
let result = $ref(null);
|
||||||
type: Function as PropType<() => Promise<any>>,
|
|
||||||
required: true,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
setup(props, context) {
|
const process = () => {
|
||||||
const pending = ref(true);
|
// this might be a retry so reset the state
|
||||||
const resolved = ref(false);
|
state = 'pending';
|
||||||
const rejected = ref(false);
|
|
||||||
const result = ref(null);
|
|
||||||
|
|
||||||
const process = () => {
|
props.p?.().then((_result) => {
|
||||||
if (props.p == null) {
|
result = _result;
|
||||||
return;
|
state = 'resolved';
|
||||||
}
|
}, () => {
|
||||||
const promise = props.p();
|
state = 'rejected';
|
||||||
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, () => {
|
watch(() => props.p, () => {
|
||||||
process();
|
process();
|
||||||
}, {
|
}, {
|
||||||
immediate: true
|
immediate: true
|
||||||
});
|
|
||||||
|
|
||||||
const retry = () => {
|
|
||||||
process();
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
pending,
|
|
||||||
resolved,
|
|
||||||
rejected,
|
|
||||||
result,
|
|
||||||
retry,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue