2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
2024-06-11 10:08:50 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
2024-05-23 15:17:28 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-05-23 14:56:11 +00:00
|
|
|
import type { ChannelConfiguration } from 'shared/lib/types'
|
2024-06-12 15:08:48 +00:00
|
|
|
import { ChannelDetailsService } from '../services/channel-details'
|
|
|
|
import { channelConfigurationContext, channelDetailsServiceContext } from '../contexts/channel'
|
|
|
|
import { LivechatElement } from '../../lib/elements/livechat'
|
|
|
|
import { ValidationError, ValidationErrorType } from '../../lib/models/validation'
|
|
|
|
import { tplChannelConfiguration } from './templates/channel-configuration'
|
2024-05-26 03:06:28 +00:00
|
|
|
import { TemplateResult, html, nothing } from 'lit'
|
2024-05-23 00:26:38 +00:00
|
|
|
import { customElement, property, state } from 'lit/decorators.js'
|
2024-05-23 14:56:11 +00:00
|
|
|
import { ptTr } from '../../lib/directives/translation'
|
|
|
|
import { Task } from '@lit/task'
|
2024-05-23 12:41:11 +00:00
|
|
|
import { provide } from '@lit/context'
|
2024-06-21 16:18:11 +00:00
|
|
|
import { channelTermsMaxLength } from 'shared/lib/constants'
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 13:52:12 +00:00
|
|
|
@customElement('livechat-channel-configuration')
|
2024-05-23 15:09:58 +00:00
|
|
|
export class ChannelConfigurationElement extends LivechatElement {
|
2024-05-23 00:26:38 +00:00
|
|
|
@property({ attribute: false })
|
2024-05-24 12:08:48 +00:00
|
|
|
public channelId?: number
|
2024-05-23 00:26:38 +00:00
|
|
|
|
|
|
|
@provide({ context: channelConfigurationContext })
|
|
|
|
@state()
|
2024-06-12 15:08:48 +00:00
|
|
|
public channelConfiguration?: ChannelConfiguration
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 12:41:11 +00:00
|
|
|
@provide({ context: channelDetailsServiceContext })
|
2024-05-24 12:08:48 +00:00
|
|
|
private _channelDetailsService?: ChannelDetailsService
|
2024-05-23 00:26:38 +00:00
|
|
|
|
|
|
|
@state()
|
2024-06-12 15:08:48 +00:00
|
|
|
public validationError?: ValidationError
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-06-11 13:05:20 +00:00
|
|
|
@state()
|
2024-06-12 15:08:48 +00:00
|
|
|
public actionDisabled: boolean = false
|
2024-06-11 13:05:20 +00:00
|
|
|
|
|
|
|
private _asyncTaskRender: Task
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
this._asyncTaskRender = this._initTask()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected _initTask (): Task {
|
|
|
|
return new Task(this, {
|
2024-06-12 14:26:35 +00:00
|
|
|
task: async () => {
|
|
|
|
this._channelDetailsService = new ChannelDetailsService(this.ptOptions)
|
2024-06-12 15:08:48 +00:00
|
|
|
this.channelConfiguration = await this._channelDetailsService.fetchConfiguration(this.channelId ?? 0)
|
|
|
|
this.actionDisabled = false // in case of reset
|
2024-06-11 13:05:20 +00:00
|
|
|
},
|
2024-06-12 14:26:35 +00:00
|
|
|
args: () => []
|
2024-06-11 13:05:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-21 16:18:11 +00:00
|
|
|
public termsMaxLength (): number {
|
|
|
|
return channelTermsMaxLength
|
|
|
|
}
|
|
|
|
|
2024-06-12 15:08:48 +00:00
|
|
|
/**
|
|
|
|
* Resets the form by reloading data from backend.
|
|
|
|
*/
|
|
|
|
public async reset (event?: Event): Promise<void> {
|
2024-06-11 13:05:20 +00:00
|
|
|
event?.preventDefault()
|
2024-06-12 15:08:48 +00:00
|
|
|
this.actionDisabled = true
|
2024-06-11 13:05:20 +00:00
|
|
|
this._asyncTaskRender = this._initTask()
|
|
|
|
this.requestUpdate()
|
|
|
|
}
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-06-13 10:48:58 +00:00
|
|
|
/**
|
|
|
|
* Resets the validation errors.
|
|
|
|
* @param ev the vent
|
|
|
|
*/
|
|
|
|
public resetValidation (_ev?: Event): void {
|
|
|
|
if (this.validationError) {
|
|
|
|
this.validationError = undefined
|
|
|
|
this.requestUpdate('_validationError')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 15:08:48 +00:00
|
|
|
/**
|
|
|
|
* Saves the channel configuration.
|
|
|
|
* @param event event
|
|
|
|
*/
|
|
|
|
public readonly saveConfig = async (event?: Event): Promise<void> => {
|
2024-05-24 12:08:48 +00:00
|
|
|
event?.preventDefault()
|
2024-06-12 15:08:48 +00:00
|
|
|
if (this._channelDetailsService && this.channelConfiguration) {
|
|
|
|
this.actionDisabled = true
|
|
|
|
this._channelDetailsService.saveOptions(this.channelConfiguration.channel.id,
|
|
|
|
this.channelConfiguration.configuration)
|
2024-05-23 20:52:39 +00:00
|
|
|
.then(() => {
|
2024-06-12 15:08:48 +00:00
|
|
|
this.validationError = undefined
|
2024-06-12 14:26:35 +00:00
|
|
|
this.ptTranslate(LOC_SUCCESSFULLY_SAVED).then((msg) => {
|
|
|
|
this.ptNotifier.info(msg)
|
|
|
|
}, () => {})
|
2024-05-26 03:06:28 +00:00
|
|
|
this.requestUpdate('_validationError')
|
2024-05-23 13:52:12 +00:00
|
|
|
})
|
2024-06-05 16:27:57 +00:00
|
|
|
.catch(async (error: Error) => {
|
2024-06-12 15:08:48 +00:00
|
|
|
this.validationError = undefined
|
2024-06-05 16:27:57 +00:00
|
|
|
if (error instanceof ValidationError) {
|
2024-06-12 15:08:48 +00:00
|
|
|
this.validationError = error
|
2024-06-05 16:27:57 +00:00
|
|
|
}
|
2024-06-14 13:17:14 +00:00
|
|
|
this.logger.warn(`A validation error occurred in saving configuration. ${error.name}: ${error.message}`)
|
2024-06-12 14:26:35 +00:00
|
|
|
this.ptNotifier.error(
|
2024-06-05 16:27:57 +00:00
|
|
|
error.message
|
|
|
|
? error.message
|
2024-06-12 14:26:35 +00:00
|
|
|
: await this.ptTranslate(LOC_ERROR)
|
2024-06-05 16:27:57 +00:00
|
|
|
)
|
2024-05-26 03:06:28 +00:00
|
|
|
this.requestUpdate('_validationError')
|
2024-05-23 20:52:39 +00:00
|
|
|
})
|
2024-06-11 13:05:20 +00:00
|
|
|
.finally(() => {
|
2024-06-12 15:08:48 +00:00
|
|
|
this.actionDisabled = false
|
2024-06-11 13:05:20 +00:00
|
|
|
})
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-12 15:08:48 +00:00
|
|
|
public readonly getInputValidationClass = (propertyName: string): { [key: string]: boolean } => {
|
2024-05-26 03:06:28 +00:00
|
|
|
const validationErrorTypes: ValidationErrorType[] | undefined =
|
2024-06-12 15:08:48 +00:00
|
|
|
this.validationError?.properties[`${propertyName}`]
|
2024-05-26 03:06:28 +00:00
|
|
|
return validationErrorTypes ? (validationErrorTypes.length ? { 'is-invalid': true } : { 'is-valid': true }) : {}
|
|
|
|
}
|
|
|
|
|
2024-06-12 15:08:48 +00:00
|
|
|
public readonly renderFeedback = (feedbackId: string,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyName: string): TemplateResult | typeof nothing => {
|
|
|
|
const errorMessages: TemplateResult[] = []
|
|
|
|
const validationErrorTypes: ValidationErrorType[] | undefined =
|
2024-06-12 15:08:48 +00:00
|
|
|
this.validationError?.properties[`${propertyName}`] ?? undefined
|
2024-05-26 03:06:28 +00:00
|
|
|
|
2024-06-21 16:18:11 +00:00
|
|
|
// FIXME: this code is duplicated in dymamic table form
|
2024-05-26 03:06:28 +00:00
|
|
|
if (validationErrorTypes && validationErrorTypes.length !== 0) {
|
2024-06-21 16:18:11 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.Missing)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_MISSING)}`)
|
|
|
|
}
|
2024-05-26 03:06:28 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.WrongType)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_WRONG_TYPE)}`)
|
|
|
|
}
|
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.WrongFormat)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_WRONG_FORMAT)}`)
|
|
|
|
}
|
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.NotInRange)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_NOT_IN_RANGE)}`)
|
|
|
|
}
|
2024-06-21 16:18:11 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.TooLong)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_TOO_LONG)}`)
|
|
|
|
}
|
2024-05-26 03:06:28 +00:00
|
|
|
|
|
|
|
return html`<div id=${feedbackId} class="invalid-feedback">${errorMessages}</div>`
|
|
|
|
} else {
|
|
|
|
return nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
protected override render = (): unknown => {
|
2024-05-13 14:40:36 +00:00
|
|
|
return this._asyncTaskRender.render({
|
2024-06-11 14:25:05 +00:00
|
|
|
pending: () => html`<livechat-spinner></livechat-spinner>`,
|
|
|
|
error: () => html`<livechat-error></livechat-error>`,
|
2024-06-12 15:08:48 +00:00
|
|
|
complete: () => tplChannelConfiguration(this)
|
2024-05-13 14:40:36 +00:00
|
|
|
})
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
}
|