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,58 +1,38 @@
|
||||||
<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 pending = ref(true);
|
|
||||||
const resolved = ref(false);
|
|
||||||
const rejected = ref(false);
|
|
||||||
const result = ref(null);
|
|
||||||
|
|
||||||
const process = () => {
|
const process = () => {
|
||||||
if (props.p == null) {
|
// this might be a retry so reset the state
|
||||||
return;
|
state = 'pending';
|
||||||
}
|
|
||||||
const promise = props.p();
|
props.p?.().then((_result) => {
|
||||||
pending.value = true;
|
result = _result;
|
||||||
resolved.value = false;
|
state = 'resolved';
|
||||||
rejected.value = false;
|
}, () => {
|
||||||
promise.then((_result) => {
|
state = 'rejected';
|
||||||
pending.value = false;
|
|
||||||
resolved.value = true;
|
|
||||||
result.value = _result;
|
|
||||||
});
|
|
||||||
promise.catch(() => {
|
|
||||||
pending.value = false;
|
|
||||||
rejected.value = true;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,20 +41,6 @@ export default defineComponent({
|
||||||
}, {
|
}, {
|
||||||
immediate: true
|
immediate: true
|
||||||
});
|
});
|
||||||
|
|
||||||
const retry = () => {
|
|
||||||
process();
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
pending,
|
|
||||||
resolved,
|
|
||||||
rejected,
|
|
||||||
result,
|
|
||||||
retry,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
|
Loading…
Reference in a new issue