| 1 | <template> |
| 2 | <div> |
| 3 | <LicenseFeatureCheck |
| 4 | v-if="!skipLicenseCheck && (customersCount || 0) < 5" |
| 5 | feature="MSSP 5" |
| 6 | @response="isMSSP5Enabled = $event" |
| 7 | /> |
| 8 | <LicenseFeatureCheck |
| 9 | v-if="!skipLicenseCheck && (customersCount || 0) < 10" |
| 10 | feature="MSSP 10" |
| 11 | @response="isMSSP10Enabled = $event" |
| 12 | /> |
| 13 | <LicenseFeatureCheck |
| 14 | v-if="!skipLicenseCheck" |
| 15 | feature="MSSP Unlimited" |
| 16 | @response="isMSSPUnlimitedEnabled = $event" |
| 17 | /> |
| 18 | |
| 19 | <LicenseFeatureCheck |
| 20 | feature="MSSP 5" |
| 21 | disabled |
| 22 | feedback="tooltip" |
| 23 | :force-show-feedback="!licenseEnabled && !licenseLoading" |
| 24 | > |
| 25 | <n-button |
| 26 | :size="size || 'small'" |
| 27 | type="primary" |
| 28 | :loading="licenseLoading && !skipLicenseCheck" |
| 29 | :disabled="!licenseEnabled || disabled" |
| 30 | @click="showAddCustomer = true" |
| 31 | > |
| 32 | <template #icon> |
| 33 | <Icon :name="AddUserIcon" :size="14" /> |
| 34 | </template> |
| 35 | <div class="flex items-center gap-2"> |
| 36 | <span>Add Customer</span> |
| 37 | <Icon v-if="!licenseEnabled && !licenseLoading" :name="LockIcon" :size="14" /> |
| 38 | </div> |
| 39 | </n-button> |
| 40 | </LicenseFeatureCheck> |
| 41 | |
| 42 | <n-drawer |
| 43 | v-model:show="showAddCustomer" |
| 44 | :width="500" |
| 45 | style="max-width: 90vw" |
| 46 | :trap-focus="false" |
| 47 | display-directive="show" |
| 48 | > |
| 49 | <n-drawer-content title="Add Customer" closable :native-scrollbar="false"> |
| 50 | <CustomerForm reset-on-submit @mounted="customerFormCTX = $event" @submitted="emit('submitted')" /> |
| 51 | </n-drawer-content> |
| 52 | </n-drawer> |
| 53 | </div> |
| 54 | </template> |
| 55 | |
| 56 | <script setup lang="ts"> |
| 57 | import type { ButtonSize } from "naive-ui" |
| 58 | import { NButton, NDrawer, NDrawerContent } from "naive-ui" |
| 59 | import { computed, ref, watch } from "vue" |
| 60 | import Icon from "@/components/common/Icon.vue" |
| 61 | import LicenseFeatureCheck from "@/components/license/LicenseFeatureCheck.vue" |
| 62 | import CustomerForm from "./CustomerForm.vue" |
| 63 | |
| 64 | const { customersCount, disabled, size } = defineProps<{ |
| 65 | customersCount?: number |
| 66 | disabled?: boolean |
| 67 | size?: ButtonSize |
| 68 | }>() |
| 69 | |
| 70 | const emit = defineEmits<{ |
| 71 | (e: "submitted"): void |
| 72 | }>() |
| 73 | |
| 74 | const openForm = defineModel<boolean | undefined>("openForm", { default: false }) |
| 75 | |
| 76 | const LockIcon = "carbon:locked" |
| 77 | const AddUserIcon = "carbon:user-follow" |
| 78 | const customerFormCTX = ref<{ reset: () => void } | null>(null) |
| 79 | const showAddCustomer = ref(false) |
| 80 | const isMSSP5Enabled = ref<null | boolean>(null) |
| 81 | const isMSSP10Enabled = ref<null | boolean>(null) |
| 82 | const isMSSPUnlimitedEnabled = ref<null | boolean>(null) |
| 83 | const licenseLoading = computed<boolean>( |
| 84 | () => isMSSP5Enabled.value === null || isMSSP10Enabled.value === null || isMSSPUnlimitedEnabled.value === null |
| 85 | ) |
| 86 | const skipLicenseCheck = computed<boolean>(() => !customersCount) |
| 87 | const licenseEnabled = computed<boolean>(() => { |
| 88 | if (skipLicenseCheck.value) { |
| 89 | return true |
| 90 | } |
| 91 | |
| 92 | if (isMSSPUnlimitedEnabled.value) { |
| 93 | return true |
| 94 | } |
| 95 | |
| 96 | if (isMSSP10Enabled.value && customersCount !== undefined && customersCount < 10) { |
| 97 | return true |
| 98 | } |
| 99 | |
| 100 | if (isMSSP5Enabled.value && customersCount !== undefined && customersCount < 5) { |
| 101 | return true |
| 102 | } |
| 103 | |
| 104 | return false |
| 105 | }) |
| 106 | |
| 107 | watch(showAddCustomer, val => { |
| 108 | if (!val) { |
| 109 | openForm.value = false |
| 110 | } |
| 111 | customerFormCTX.value?.reset() |
| 112 | }) |
| 113 | |
| 114 | watch( |
| 115 | openForm, |
| 116 | val => { |
| 117 | if (val) { |
| 118 | showAddCustomer.value = true |
| 119 | } |
| 120 | }, |
| 121 | { immediate: true } |
| 122 | ) |
| 123 | |
| 124 | watch( |
| 125 | () => customersCount, |
| 126 | () => { |
| 127 | if (customersCount === undefined) { |
| 128 | return |
| 129 | } |
| 130 | if (customersCount >= 5) { |
| 131 | isMSSP5Enabled.value = true |
| 132 | } |
| 133 | if (customersCount >= 10) { |
| 134 | isMSSP10Enabled.value = true |
| 135 | } |
| 136 | }, |
| 137 | { immediate: true } |
| 138 | ) |
| 139 | </script> |