| 1 | <template> |
| 2 | <n-switch |
| 3 | v-model:value="entity.enabled" |
| 4 | :rail-style |
| 5 | :loading="updatingStatus" |
| 6 | @update:value="toggleExclusionRuleStatus()" |
| 7 | > |
| 8 | <template #checked>Enabled</template> |
| 9 | <template #unchecked>Disabled</template> |
| 10 | </n-switch> |
| 11 | </template> |
| 12 | |
| 13 | <script setup lang="ts"> |
| 14 | import type { CSSProperties } from "vue" |
| 15 | import type { ExclusionRule } from "@/types/incidentManagement/exclusionRules.d" |
| 16 | import { NSwitch, useMessage } from "naive-ui" |
| 17 | import { computed, ref, toRefs, watch } from "vue" |
| 18 | import Api from "@/api" |
| 19 | import { useThemeStore } from "@/stores/theme" |
| 20 | |
| 21 | const props = defineProps<{ |
| 22 | entity: ExclusionRule |
| 23 | }>() |
| 24 | |
| 25 | const emit = defineEmits<{ |
| 26 | (e: "loading", value: boolean): void |
| 27 | (e: "updated", value: ExclusionRule): void |
| 28 | }>() |
| 29 | |
| 30 | const { entity } = toRefs(props) |
| 31 | |
| 32 | const message = useMessage() |
| 33 | const themeStore = useThemeStore() |
| 34 | const checkedColor = computed(() => themeStore.style["success-color-rgb"]) |
| 35 | const uncheckedColor = computed(() => themeStore.style["border-color-rgb"]) |
| 36 | const updatingStatus = ref(false) |
| 37 | |
| 38 | function railStyle({ focused, checked }: { focused: boolean; checked: boolean }) { |
| 39 | const style: CSSProperties = {} |
| 40 | if (checked) { |
| 41 | style.background = `rgb(${checkedColor.value} / 40%)` |
| 42 | if (focused) { |
| 43 | style.boxShadow = `0 0 0 2px rgb(${checkedColor.value} / 30%)` |
| 44 | } |
| 45 | } else { |
| 46 | style.background = `rgb(${uncheckedColor.value})` |
| 47 | if (focused) { |
| 48 | style.boxShadow = `0 0 0 2px rgb(${uncheckedColor.value} / 10%)` |
| 49 | } |
| 50 | } |
| 51 | return style |
| 52 | } |
| 53 | |
| 54 | function toggleExclusionRuleStatus() { |
| 55 | updatingStatus.value = true |
| 56 | |
| 57 | Api.incidentManagement.exclusionRules |
| 58 | .toggleExclusionRuleStatus(entity.value.id) |
| 59 | .then(res => { |
| 60 | if (res.data.success) { |
| 61 | entity.value.enabled = res.data.exclusion_response.enabled |
| 62 | emit("updated", res.data.exclusion_response) |
| 63 | } |
| 64 | }) |
| 65 | .catch(err => { |
| 66 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 67 | }) |
| 68 | .finally(() => { |
| 69 | updatingStatus.value = false |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | watch(updatingStatus, val => { |
| 74 | emit("loading", val) |
| 75 | }) |
| 76 | </script> |