import { LitElementWw } from '@webwriter/lit' import { CSSResult, TemplateResult, html } from 'lit' import { customElement, query } from 'lit/decorators.js' import { consume } from '@lit/context' import { globalStyles } from '@/global_styles' import { editableContext } from '@/contexts/editable_context' import type { QAndAEntry } from '@/types/q_and_a_entry' import { qAndAContext } from '@/contexts/q_and_a_context' import SlDetails from '@shoelace-style/shoelace/dist/components/details/details.component.js' import SlInput from '@shoelace-style/shoelace/dist/components/input/input.component.js' import SlTextarea from '@shoelace-style/shoelace/dist/components/textarea/textarea.component.js' import SlButton from '@shoelace-style/shoelace/dist/components/button/button.component.js' import { CCard } from '../reusables/c-card' import { msg } from '@lit/localize' export class HelpQAndACard extends LitElementWw { static scopedElements = { 'c-card': CCard, 'sl-details': SlDetails, 'sl-input': SlInput, 'sl-textarea': SlTextarea, 'sl-button': SlButton, } @consume({ context: editableContext, subscribe: true }) accessor editable: boolean @consume({ context: qAndAContext, subscribe: true }) accessor qAndA: QAndAEntry[] @query('#newEntryTitle') accessor _newEntryTitle: SlInput // METHODS - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - addEntry(e: MouseEvent): void { e.preventDefault() const entry: QAndAEntry = { title: this._newEntryTitle.value, description: '', } this.dispatchEvent( new CustomEvent('add-new-help-entry', { detail: entry, bubbles: true, composed: true, }), ) this._newEntryTitle.value = '' } updateEntry(e: MouseEvent): void { const entry: QAndAEntry = { title: e.target.title, description: e.target.parentNode.previousElementSibling.value, } this.dispatchEvent( new CustomEvent('update-help-entry', { detail: entry, bubbles: true, composed: true, }), ) } removeEntry(e: MouseEvent): void { this.dispatchEvent( new CustomEvent('remove-help-entry', { detail: e.target.title, bubbles: true, composed: true, }), ) } // STYLES - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static styles: CSSResult = globalStyles // RENDER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - render(): TemplateResult<1> { return html`
${msg('Q & A')}
${!this.qAndA.length && this.editable ? html`${msg( 'There are currently no help entries. If you publish the widget like this, the help section will be hidden.', )}` : html``} ${this.qAndA.map( (entry) => html` ${this.editable ? html`
${msg('Update entry')}
${msg('Remove entry')}
` : html`${entry.description}`}
`, )} ${this.editable ? html`
${msg('Add entry')}
` : html``}
` } }