All files / src/store EndpointMonitorElement.ts

92.06% Statements 58/63
100% Branches 10/10
85.71% Functions 6/7
92.06% Lines 58/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 64126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 135x 135x 135x 135x 135x 126x 126x 126x 126x 126x 135x 135x 135x 126x 126x 18x 18x 126x 126x           126x 126x 119x 119x 119x 119x 119x 119x 119x 119x 119x 126x 126x 594x 594x 2x 2x 594x 126x 126x 594x 594x 594x 594x 104x 104x 594x 126x  
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 });
    }
  }
}