# All files DotshaOffersList.ts

74.14% Statements 109/147
87.5% Branches 7/8
66.66% Functions 4/6
74.14% Lines 109/147

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
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    1481x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
     import { html, css, LitElement } from 'lit';
    import { customElement, property, state } from 'lit/decorators.js';
    import { deserialise } from 'kitsu-core';
    import './dotshaOffer.js';

    type OfferItem = {
      id: String;
    };

    type Offer = {
      id: String;
      name: String;
      offerItems: OfferItem[];
    };

    @customElement('dotsha-offers-list')
    export class DotshaOffersList extends LitElement {
      static styles = css`
        :host {
          display: block;
          padding: 25px;
          color: var(--dotsha-offers-list-text-color, #000);
        }
      `;

      @state()
      isLoading = true;

      @state()
      error = null;

      @state()
      offers: Offer[] = [];

      @state()
      offer: Offer | null = null;

      @property({ type: String }) title = 'Welcome';

      @property({ type: String }) apiBasePath = '/api';

      @property({ type: String }) apiToken = '';

      _getOffers() {
        this.isLoading = true;
        return fetch(`${this.apiBasePath}/offers`, {
          method: 'GET',
          credentials: 'include',
          headers: new Headers({
            Accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: `Bearer ${this.apiToken}`,
          }),
        })
          .then(data => data.json())
          .then(deserialise)
          .then(json => {
            this.offers = json.data;
          })
          .catch(error => {
            this.error = error;
          })
          .finally(() => {
            this.isLoading = false;
          });
      }

      _goToOffersList() {
        this.offer = null;
      }

      // eslint-disable-next-line class-methods-use-this
      _selectOffer(event: any) {
        this.isLoading = true;
        const offerId = event.target.getAttribute('data-offer-id');
        return fetch(`${this.apiBasePath}/offers/${offerId}?include=offerItems`, {
          method: 'GET',
          credentials: 'include',
          headers: new Headers({
            Accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: `Bearer ${this.apiToken}`,
          }),
        })
          .then(data => data.json())
          .then(deserialise)
          .then(json => {
            this.offer = json.data;
          })
          .catch(error => {
            this.error = error;
          })
          .finally(() => {
            this.isLoading = false;
          });
      }

      constructor() {
        super();
        this.addEventListener('goto-offers-list', this._goToOffersList);
      }

      override connectedCallback() {
        // eslint-disable-next-line wc/guard-super-call
        super.connectedCallback();
        this._getOffers();
      }

      override render() {
        if (this.isLoading) {
          return html`
            <h2>${this.title}</h2>
            <div
              style="width: '100%'; height: '100%'; textAlign: 'center'; paddingTop: '20%';"
            >
              <img
                src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/0.16.1/images/loader-large.gif"
                alt="Loading in progress..."
              />
            </div>
          `;
        }

        if (this.offer) {
          return html`<dotsha-offer .offer=${this.offer}></dotsha-offer>`;
        }

        return html`
          <h2>${this.title}</h2>
          <ul>
            ${this.offers.map(
              (offer: Offer) => html`
                <li>
                  <button
                    @click=${this._selectOffer}
                    data-offer-id=${offer.id}
                    part="button"
                  >
                    ${offer.name}
                  </button>
                </li>
              `
            )}
          </ul>
        `;
      }
    }