| 1 | <template> |
| 2 | <n-select |
| 3 | to="body" |
| 4 | :value="selectedStatus" |
| 5 | :options="statusOptions" |
| 6 | :loading |
| 7 | size="small" |
| 8 | class="min-w-36" |
| 9 | :consistent-menu-width="false" |
| 10 | @update:value="handleStatusChange" |
| 11 | /> |
| 12 | </template> |
| 13 | |
| 14 | <script setup lang="ts"> |
| 15 | import type { CaseStatus } from "@/types/cases" |
| 16 | import type { ApiError } from "@/types/common" |
| 17 | import { NSelect, useMessage } from "naive-ui" |
| 18 | import { ref, watch } from "vue" |
| 19 | import Api from "@/api" |
| 20 | import { getApiErrorMessage } from "@/utils" |
| 21 | |
| 22 | export interface CaseStatusUpdateSuccessPayload { |
| 23 | caseId: number |
| 24 | status: CaseStatus |
| 25 | } |
| 26 | |
| 27 | export interface CaseStatusUpdateErrorPayload { |
| 28 | caseId: number |
| 29 | status: CaseStatus |
| 30 | message: string |
| 31 | } |
| 32 | |
| 33 | const props = defineProps<{ |
| 34 | caseId: number |
| 35 | status: CaseStatus |
| 36 | }>() |
| 37 | |
| 38 | const emit = defineEmits<{ |
| 39 | success: [payload: CaseStatusUpdateSuccessPayload] |
| 40 | error: [payload: CaseStatusUpdateErrorPayload] |
| 41 | }>() |
| 42 | |
| 43 | const message = useMessage() |
| 44 | |
| 45 | const statusOptions = [ |
| 46 | { label: "Open", value: "OPEN" }, |
| 47 | { label: "In Progress", value: "IN_PROGRESS" }, |
| 48 | { label: "Closed", value: "CLOSED" } |
| 49 | ] |
| 50 | |
| 51 | const selectedStatus = ref<CaseStatus>(props.status) |
| 52 | const loading = ref(false) |
| 53 | |
| 54 | watch( |
| 55 | () => props.status, |
| 56 | newStatus => { |
| 57 | selectedStatus.value = newStatus |
| 58 | } |
| 59 | ) |
| 60 | |
| 61 | async function handleStatusChange(value: string) { |
| 62 | if (loading.value || value === selectedStatus.value) return |
| 63 | |
| 64 | const previousStatus = selectedStatus.value |
| 65 | selectedStatus.value = value as CaseStatus |
| 66 | loading.value = true |
| 67 | |
| 68 | try { |
| 69 | await Api.cases.updateCaseStatus(props.caseId, selectedStatus.value) |
| 70 | message.success(`Case status updated to ${selectedStatus.value}`) |
| 71 | emit("success", { |
| 72 | caseId: props.caseId, |
| 73 | status: selectedStatus.value |
| 74 | }) |
| 75 | } catch (err) { |
| 76 | selectedStatus.value = previousStatus |
| 77 | message.error(getApiErrorMessage(err as ApiError)) |
| 78 | emit("error", { |
| 79 | caseId: props.caseId, |
| 80 | status: previousStatus, |
| 81 | message: getApiErrorMessage(err as ApiError) |
| 82 | }) |
| 83 | } finally { |
| 84 | loading.value = false |
| 85 | } |
| 86 | } |
| 87 | </script> |