All files / src/store EndpointMonitorElement.ts

88.88% Statements 56/63
90% Branches 9/10
85.71% Functions 6/7
88.88% Lines 56/63

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 6492x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 19x 19x 19x 19x 19x 92x 92x 92x 92x 92x 19x 19x 19x 92x 92x 4x 4x 92x 92x           92x 92x 20x 20x 20x 20x 20x 20x 20x 20x 20x 92x 92x 53x 53x     53x 92x 92x 53x 53x 53x 53x 19x 19x 53x 92x  
import { PropertyValueMap, PropertyValues } from 'lit';
import { property } from 'lit/decorators.js';
import { CustomEventType } from '../interfaces';
 
import { StoreMonitorElement } from './StoreMonitorElement';
 
/**
 * StoreElement is a listener for a given endpoint that re-renders
 * when the underlying store element changes
 */
export class EndpointMonitorElement extends StoreMonitorElement {
  @property({ type: String })
  url: string;
 
  @property({ type: Boolean })
  showLoading = false;
 
  @property({ type: Object, attribute: false })
  data: any;
 
  connectedCallback(): void {
    super.connectedCallback();
    this.prepareData = this.prepareData.bind(this);
  }
 
  prepareData(data: any): any {
    return data;
  }
 
  public refresh() {
    this.store.makeRequest(this.url, {
      prepareData: this.prepareData,
      force: true
    });
  }
 
  protected storeUpdated(event: CustomEvent) {
    if (event.detail.url === this.url) {
      const previous = this.data;
      this.data = event.detail.data;
      this.fireCustomEvent(CustomEventType.Refreshed, {
        data: event.detail.data,
        previous
      });
    }
  }
 
  public willUpdate(changed: PropertyValues): void {
    super.willUpdate(changed);
    if (changed.has('url') && !this.url) {
      this.data = null;
    }
  }
 
  protected updated(
    properties: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(properties);
    if (properties.has('url') && this.url) {
      this.store.makeRequest(this.url, { prepareData: this.prepareData });
    }
  }
}