generated from Nodarx/template
refactor: bunch of improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { IdentityBasic } from '@KTXM/MailManager/models/identity'
|
||||
import { ServiceObject } from '@KTXM/MailManager/models/service'
|
||||
import type { ServiceIdentity } from '@KTXM/MailManager/types/service'
|
||||
import type { ProviderAuthPanelProps, ProviderAuthPanelEmits } from '@KTXM/MailManager/types/integration'
|
||||
|
||||
const props = defineProps<ProviderAuthPanelProps>()
|
||||
const emit = defineEmits<ProviderAuthPanelEmits>()
|
||||
|
||||
const identity = ref(props.prefilledIdentity || props.emailAddress || '')
|
||||
const secret = ref(props.prefilledSecret || '')
|
||||
const identity = ref('')
|
||||
const secret = ref('')
|
||||
|
||||
const rules = {
|
||||
required: (value: unknown) => !!value || 'This field is required'
|
||||
}
|
||||
|
||||
const isValid = computed(() => !!identity.value && !!secret.value)
|
||||
|
||||
const currentIdentity = computed((): ServiceIdentity | null => {
|
||||
if (!isValid.value) {
|
||||
if (!identity.value || !secret.value) {
|
||||
return null
|
||||
}
|
||||
|
||||
@@ -28,33 +28,13 @@ const currentIdentity = computed((): ServiceIdentity | null => {
|
||||
})
|
||||
|
||||
watch(
|
||||
currentIdentity,
|
||||
value => {
|
||||
if (value) {
|
||||
emit('update:modelValue', value)
|
||||
}
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
isValid,
|
||||
value => {
|
||||
emit('valid', value)
|
||||
() => props.service,
|
||||
service => {
|
||||
syncFromService(service)
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
value => {
|
||||
if (value?.type === 'BA') {
|
||||
identity.value = value.identity || ''
|
||||
secret.value = value.secret || ''
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => props.emailAddress,
|
||||
value => {
|
||||
@@ -64,6 +44,55 @@ watch(
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
currentIdentity,
|
||||
value => {
|
||||
const existingIdentity = props.service?.identity?.toJson() ?? null
|
||||
if (sameIdentity(existingIdentity, value)) {
|
||||
return
|
||||
}
|
||||
|
||||
const nextService = createServiceObject(props.service)
|
||||
nextService.identity = value ? new IdentityBasic(value.identity, value.secret) : null
|
||||
emit('update:service', nextService)
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
|
||||
function syncFromService(service?: ServiceObject) {
|
||||
const serviceIdentity = service?.identity?.toJson() ?? null
|
||||
|
||||
if (serviceIdentity?.type === 'BA') {
|
||||
identity.value = serviceIdentity.identity || props.prefilledIdentity || props.emailAddress || ''
|
||||
secret.value = serviceIdentity.secret || props.prefilledSecret || ''
|
||||
return
|
||||
}
|
||||
|
||||
identity.value = props.prefilledIdentity || props.emailAddress || ''
|
||||
secret.value = props.prefilledSecret || ''
|
||||
}
|
||||
|
||||
function createServiceObject(service?: ServiceObject): ServiceObject {
|
||||
const nextService = new ServiceObject()
|
||||
|
||||
if (service) {
|
||||
nextService.fromJson(service.toJson())
|
||||
}
|
||||
|
||||
return nextService
|
||||
}
|
||||
|
||||
function sameIdentity(a: ServiceIdentity | null, b: ServiceIdentity | null): boolean {
|
||||
if (a === null || b === null) {
|
||||
return a === b
|
||||
}
|
||||
|
||||
return a.type === 'BA'
|
||||
&& b.type === 'BA'
|
||||
&& a.identity === b.identity
|
||||
&& a.secret === b.secret
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in New Issue
Block a user