|
| 1 | +<script setup> |
| 2 | +import { storeToRefs } from 'pinia'; |
| 3 | +import { computed, ref, onMounted, watch } from 'vue'; |
| 4 | +import { useI18n } from 'vue-i18n'; |
| 5 | +import { useRouter } from 'vue-router'; |
| 6 | +
|
| 7 | +import { useFormStore } from '~/store/form'; |
| 8 | +import { formService } from '~/services'; |
| 9 | +import { useNotificationStore } from '~/store/notification'; |
| 10 | +
|
| 11 | +const { t, locale } = useI18n({ useScope: 'global' }); |
| 12 | +
|
| 13 | +const properties = defineProps({ |
| 14 | + submissionId: { |
| 15 | + type: String, |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + class: { |
| 19 | + type: String, |
| 20 | + default: '', |
| 21 | + }, |
| 22 | +}); |
| 23 | +
|
| 24 | +const { isRTL } = storeToRefs(useFormStore()); |
| 25 | +
|
| 26 | +// Computed class binding that combines RTL and passed-in class |
| 27 | +const spanClass = computed(() => ({ |
| 28 | + 'dir-rtl': isRTL, |
| 29 | + [properties.class]: properties.class, |
| 30 | +})); |
| 31 | +
|
| 32 | +// Reactive ref to store the result of the async check |
| 33 | +const canReviseSubmission = ref(false); |
| 34 | +
|
| 35 | +// Loading state to prevent multiple clicks |
| 36 | +const isEnablingRevision = ref(false); |
| 37 | +
|
| 38 | +// Async function to check if submission can be revised |
| 39 | +async function checkCanReviseSubmission() { |
| 40 | + try { |
| 41 | + const result = await formService.checkSubmitterRevision( |
| 42 | + properties.submissionId |
| 43 | + ); |
| 44 | + canReviseSubmission.value = result.data; |
| 45 | + } catch { |
| 46 | + canReviseSubmission.value = false; |
| 47 | + } |
| 48 | +} |
| 49 | +
|
| 50 | +const router = useRouter(); |
| 51 | +const notificationStore = useNotificationStore(); |
| 52 | +
|
| 53 | +async function handleRevision() { |
| 54 | + // Prevent multiple clicks while request is in progress |
| 55 | + if (isEnablingRevision.value) { |
| 56 | + return; |
| 57 | + } |
| 58 | +
|
| 59 | + isEnablingRevision.value = true; |
| 60 | +
|
| 61 | + try { |
| 62 | + const response = await formService.performSubmitterRevision( |
| 63 | + properties.submissionId |
| 64 | + ); |
| 65 | +
|
| 66 | + // If response is 200, navigate to Edit Revision |
| 67 | + if (response.status === 200) { |
| 68 | + router.push({ |
| 69 | + name: 'UserFormDraftEdit', |
| 70 | + query: { |
| 71 | + s: properties.submissionId, |
| 72 | + }, |
| 73 | + }); |
| 74 | + } else if (response.status === 400) { |
| 75 | + // If response is 400, failed the check, so hide the button |
| 76 | + canReviseSubmission.value = false; |
| 77 | + } |
| 78 | + // Any other error will be caught by the catch block |
| 79 | + } catch (error) { |
| 80 | + notificationStore.addNotification({ |
| 81 | + text: t('trans.submitterRevision.recallErrMsg'), |
| 82 | + consoleError: t('trans.submitterRevision.recallConsErrMsg', { |
| 83 | + submissionId: properties.submissionId, |
| 84 | + error: error, |
| 85 | + }), |
| 86 | + }); |
| 87 | + } finally { |
| 88 | + // Always reset loading state when request completes |
| 89 | + isEnablingRevision.value = false; |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +// Watch for changes in submissionId or formId and re-check |
| 94 | +watch( |
| 95 | + () => [properties.submissionId], |
| 96 | + () => { |
| 97 | + if (properties.submissionId) { |
| 98 | + checkCanReviseSubmission(); |
| 99 | + } |
| 100 | + }, |
| 101 | + { immediate: true } |
| 102 | +); |
| 103 | +
|
| 104 | +// Also check when component mounts |
| 105 | +onMounted(() => { |
| 106 | + if (properties.submissionId) { |
| 107 | + checkCanReviseSubmission(); |
| 108 | + } |
| 109 | +}); |
| 110 | +
|
| 111 | +defineExpose({ |
| 112 | + handleRevision, |
| 113 | + canReviseSubmission, |
| 114 | + isEnablingRevision, |
| 115 | +}); |
| 116 | +</script> |
| 117 | + |
| 118 | +<template> |
| 119 | + <span v-if="canReviseSubmission" :class="spanClass"> |
| 120 | + <v-btn |
| 121 | + color="primary" |
| 122 | + variant="outlined" |
| 123 | + :title="$t('trans.submitterRevision.recall')" |
| 124 | + :loading="isEnablingRevision" |
| 125 | + :disabled="isEnablingRevision" |
| 126 | + @click="handleRevision" |
| 127 | + ><span :lang="locale">{{ |
| 128 | + $t('trans.submitterRevision.recall') |
| 129 | + }}</span></v-btn |
| 130 | + > |
| 131 | + </span> |
| 132 | +</template> |
0 commit comments