All files / src/live ContactNotepad.ts

79.69% Statements 106/133
100% Branches 2/2
33.33% Functions 2/6
79.69% Lines 106/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 13492x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x   92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x           92x             92x                               92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, PropertyValueMap, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { ContactStoreElement } from './ContactStoreElement';
import { getDisplayName } from './ContactChat';
import { ContactNote, CustomEventType } from '../interfaces';
 
export class ContactNotepad extends ContactStoreElement {
  @property({ type: Object, attribute: false })
  note: ContactNote;
 
  @property({ type: String })
  dirtyMessage =
    'You have unsaved changes to the contact notepad. Are you sure you want to contiunue?';
 
  static get styles() {
    return css`
      :host {
        height: 100%;
        display: flex;
      }
 
      .wrapper {
        flex-grow: 1;
        --color-widget-bg: transparent;
        --color-widget-bg-focused: transparent;
        outline: none;
        border-radius: var(--curvature);
        display: flex;
        flex-direction: column;
      }
 
      .notepad {
        flex-grow: 1;
        padding: 1.25rem;
        overflow-y: auto;
        background: transparent;
        border: none;
        outline: none;
        resize: none;
        font-family: monospace;
      }
 
      .notepad:focus {
        outline: none;
      }
 
      .toolbar {
        background: rgba(0, 0, 0, 0.03);
        padding: 0.25em 0.5em;
        display: flex;
        min-height: 2em;
        align-items: center;
        border-top: 1px solid rgba(0, 0, 0, 0.1);
      }
 
      .toolbar temba-button {
        margin-right: 0.5em;
      }
 
      .updated {
        font-size: 0.9em;
        flex-grow: 1;
        margin-left: 0.5em;
      }
    `;
  }
 
  private handleChange() {
    this.markDirty();
  }
 
  private submitChanges() {
    const notepad = this.shadowRoot.querySelector(
      '.notepad'
    ) as HTMLInputElement;
    const note = notepad.value;
    this.postChanges({ note }).then(() => {
      this.markClean();
    });
  }

  protected updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);

    if (changes.has('data')) {
      this.note =
        this.data?.notes.length > 0
          ? { ...this.data.notes[this.data.notes.length - 1] }
          : null;
      this.fireCustomEvent(CustomEventType.DetailsChanged, {
        count: this.note && this.note.text.length > 0 ? 1 : 0
      });
      this.markClean();
    }
  }

  public render(): TemplateResult {
    if (this.data) {
      return html`
        <div class="wrapper">
          <!-- prettier-ignore -->
          <textarea class="notepad" @input=${this.handleChange} .value=${this
            .note
            ? this.note.text
            : ''}></textarea>
          <div class="toolbar">
            <div class="updated">
              ${this.note
                ? html`Last updated by ${getDisplayName(this.note.created_by)}
                    <temba-date
                      value="${this.note.created_on}"
                      display="duration"
                    ></temba-date>`
                : null}
            </div>
            ${this.dirty
              ? html`
                  <temba-button
                    name="Save"
                    small
                    @click=${this.submitChanges}
                  ></temba-button>
                `
              : null}
          </div>
        </div>
      `;
    }
    return super.render();
  }
}