All files / src/list RunList.ts

100% Statements 388/388
97.53% Branches 79/81
100% Functions 15/15
100% Lines 388/388

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 38992x 106x 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 47x 47x 47x 47x 47x 92x 89x 40x 40x 40x 89x 5x 5x 4x 4x 4x 89x 89x 89x 89x 89x 89x 89x 89x 54x 8x 89x 92x 89x 89x 12x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 89x 44x 44x 44x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 2x 2x 2x 2x 92x 2x 2x 2x 2x 2x 2x 69x 69x 1x 1x 1x 16x 69x 7x 7x 1x 1x 8x 8x 8x 8x 8x 38x 38x 38x 38x 38x 38x 38x 38x 38x 10x 1x 1x 1x 1x 1x 1x 1x 38x 28x 28x 28x 1x 38x 38x 38x 38x 38x 38x 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 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 54x 92x 92x 45x 45x 45x 45x 45x 1x 1x 1x 1x 1x 1x 1x 1x 92x 1x 1x 1x 1x 92x 92x 92x 92x 92x 92x 92x 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 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 96x 99x 99x 99x 99x 99x 99x 99x 99x 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 109x 109x 109x 109x 109x 114x 93x 93x 92x 92x 92x 92x 92x 92x 115x 115x 115x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, PropertyValues, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { Checkbox } from '../form/Checkbox';
import { Select } from '../form/select/Select';
import { capitalize } from '../utils';
import { Icon } from '../Icons';
import { TembaList } from './TembaList';
 
const FLOW_COLOR = 'rgb(223, 65, 159)';
 
export class RunList extends TembaList {
  @property({ type: String })
  flow: string;
 
  @property({ type: Object, attribute: false })
  results: any[];
 
  @property({ type: Boolean })
  responses = true;
 
  @property({ type: Object })
  resultPreview: any;
 
  @property({ type: Object })
  selectedRun: any;
 
  @property({ type: Boolean })
  allowDelete = false;
 
  private resultKeys = {};
 
  static get styles() {
    return css`
      :host {
        overflow-y: auto !important;
        --contact-name-font-size: 1em;
      }
 
      @media only screen and (max-height: 768px) {
        temba-options {
          max-height: 20vh;
        }
      }
 
      temba-options {
        display: block;
        width: 100%;
        flex-grow: 1;
      }
    `;
  }
 
  public firstUpdated(changedProperties: Map<string, any>) {
    super.firstUpdated(changedProperties);
  }
 
  public willUpdate(changedProperties: PropertyValues): void {
    if (changedProperties.has('responses') || changedProperties.has('flow')) {
      if (this.flow) {
        this.endpoint = `/api/v2/runs.json?flow=${this.flow}${
          this.responses ? '&responded=1' : ''
        }`;
      }
    }
 
    if (changedProperties.has('resultPreview')) {
      this.createRenderOption();
    }
 
    // pre-sync selectedRun so handleSelected() in updated() won't trigger a warning
    if (changedProperties.has('cursorIndex')) {
      if (this.cursorIndex > -1) {
        this.selectedRun = this.items[this.cursorIndex];
      }
    }
 
    super.willUpdate(changedProperties);
  }
 
  public updated(changedProperties: Map<string, any>): void {
    super.updated(changedProperties);
 
    if (changedProperties.has('results')) {
      if (this.results) {
        const select = this.shadowRoot.querySelector(
          'temba-select'
        ) as Select<any>;
        select.setOptions(this.results);
        this.resultKeys = this.results.reduce(
          (current, result) => ({ ...current, [result.key]: result }),
          {}
        );
      }
    }
  }
 
  public renderResultPreview(run: any) {
    if (this.resultPreview) {
      const runResult = run.values[this.resultPreview.key];
      if (runResult) {
        if (this.resultPreview.categories.length > 1) {
          if (runResult.category) {
            return runResult.category;
          }
        } else {
          return runResult.value;
        }
      }
    }
    return null;
  }
 
  public removeRun(id: number) {
    this.items = this.items.filter((run) => run.id !== id);
    this.cursorIndex = Math.min(this.cursorIndex, this.items.length);
    this.requestUpdate('cursorIndex');
  }
 
  public getIcon(run: any): TemplateResult {
    let icon = null;
    if (run.exit_type == 'completed') {
      icon = html`<temba-icon
        name="check"
        style="--icon-color:#666;margin-left:0.5em"
      />`;
    } else if (run.exit_type == 'interrupted') {
      icon = html`<temba-icon
        name="x-octagon"
        style="--icon-color:${FLOW_COLOR};margin-left:0.5em"
      />`;
    } else if (run.exit_type == 'expired') {
      icon = html`<temba-icon
        name="clock"
        style="--icon-color:${FLOW_COLOR};margin-left:0.5em"
      />`;
    } else if (!run.exit_type) {
      if (run.responded) {
        icon = html`<temba-icon
          name="activity"
          style="--icon-color:var(--color-primary-dark);margin-left:0.5em"
        />`;
      } else {
        icon = html`<temba-icon
          name="hourglass"
          style="--icon-color:var(--color-primary-dark);margin-left:0.5em"
        />`;
      }
    }
    return icon;
  }
 
  public createRenderOption() {
    this.renderOption = (run: any): TemplateResult => {
      let statusStyle = '';
 
      if (!run.exited_on) {
        statusStyle = 'font-weight:400;';
      }
 
      if (!run.responded) {
        statusStyle += '';
      }
 
      return html`
        <div class="row" style="${statusStyle}display:flex;align-items:center">
          <div
            style="width:16em;white-space:nowrap;overflow: hidden; text-overflow: ellipsis;"
          >
            <temba-contact-name
              name=${run.contact?.name || run.contact?.ref || ''}
              urn=${run.contact?.urn || ''}
              icon-size="15"
            />
          </div>
 
          <div
            style="margin: 0em 1em;flex:1;white-space:nowrap; overflow:hidden; text-overflow: ellipsis;"
          >
            ${this.renderResultPreview(run)}
          </div>
 
          <div style="flex-shrink:1">
            <temba-date value="${run.modified_on}" display="duration" />
          </div>
          ${this.getIcon(run)}
        </div>
      `;
    };
  }
 
  public getRefreshEndpoint() {
    if (this.items.length > 0) {
      const modifiedOn = this.items[0].modified_on;
      const separator = this.endpoint.includes('?') ? '&' : '?';
      return this.endpoint + separator + 'after=' + modifiedOn;
    }
    return this.endpoint;
  }
 
  public toggleResponded() {
    this.responses = (
      this.shadowRoot.querySelector('#responded') as Checkbox
    ).checked;
  }
 
  public handleColumnChanged(event: any) {
    if (event.target.values.length > 0) {
      this.resultPreview = event.target.values[0];
    } else {
      this.resultPreview = null;
    }
  }
 
  public handleSelected(selected: any) {
    this.selectedRun = selected;
  }
 
  public getListStyle(): string {
    return '';
  }
 
  public renderHeader(): TemplateResult {
    return html`
      <div style="display:flex;width:100%;margin-bottom: 1em;">
        <div style="flex-grow:1">
          ${this.results
            ? html`
                <temba-select
                  clearable
                  placeholder="Result Preview"
                  valueKey="key"
                  @change=${this.handleColumnChanged}
                  style="z-index: 10;"
                ></temba-select>
              `
            : null}
        </div>
        <div style="margin-left:1em;">
          <temba-checkbox
            id="responded"
            label="Responses Only"
            checked="true"
            @click=${this.toggleResponded}
          />
        </div>
      </div>
      <div
        style="
        font-size:0.8em;
        color:rgba(0,0,0,.4);
        text-align:right;
        background:#f9f9f9;
        border: 1px solid var(--color-widget-border);
        margin-bottom:-0.5em; 
        padding-bottom: 0.6em;
        padding-top: 0.3em;
        padding-right: 4.5em;
        border-top-right-radius: var(--curvature);
        border-top-left-radius: var(--curvature)
    "
      >
        Last Updated
      </div>
    `;
  }
 
  public renderFooter(): TemplateResult {
    if (!this.selectedRun || !this.resultKeys) {
      return null;
    }
 
    const resultKeys = Object.keys(this.selectedRun.values || {});
 
    return html` <div
      style="margin-top: 1.5em; margin-bottom:0.5em;flex-grow:1;border-radius:var(--curvature); border: 1px solid var(--color-widget-border);"
    >
      <div style="display:flex;flex-direction:column;">
        <div
          style="font-size:1.5em;background:#f9f9f9;padding:.75em;padding-top:.35em;display:flex;align-items:center;border-top-right-radius:var(--curvature);border-top-left-radius:var(--curvature)"
        >
          <div>
            <temba-contact-name
              style="cursor:pointer"
              name=${this.selectedRun.contact?.name ||
              this.selectedRun.contact?.ref ||
              ''}
              urn=${this.selectedRun.contact?.urn || ''}
              onclick="goto(event, this)"
              href="/contact/read/${this.selectedRun.contact?.uuid || ''}/"
            ></temba-contact-name>
            <div
              style="display:flex;margin-left:-0.2em;margin-top:0.25em;font-size: 0.65em"
            >
              ${this.selectedRun.exit_type
                ? html`
                    <div style="margin-left:2em;flex-grow:1;display:flex">
                      ${this.getIcon(this.selectedRun)}
                      <div style="margin-left:0.5em">
                        ${capitalize(this.selectedRun.exit_type)}&nbsp;
                      </div>
                      <temba-date
                        value="${this.selectedRun.exited_on}"
                        compare="${this.selectedRun.created_on}"
                        display="duration"
                      />
                    </div>
                  `
                : html`${this.getIcon(this.selectedRun)}
                    <div style="margin-left:1.5em;flex-grow:1;display:flex">
                      <div>Started&nbsp;</div>
                      <temba-date
                        value="${this.selectedRun.created_on}"
                        display="duration"
                      ></temba-date>
                    </div>`}
            </div>
          </div>
          <div style="flex-grow:1"></div>
          <div style="display:flex;flex-direction: column">
            <div style="font-size:0.75em">
              ${this.selectedRun.created_on
                ? new Date(this.selectedRun.created_on).toLocaleString()
                : ''}
            </div>
            <div
              style="font-size:0.6em;align-self:flex-end;color:#888;line-height:0.75em"
            >
              Started
            </div>
          </div>
          ${this.allowDelete
            ? html` <temba-icon
                clickable
                style="margin-left:0.75em;"
                name=${Icon.delete}
                onclick="deleteRun(${this.selectedRun.id});"
              ></temba-icon>`
            : null}
        </div>
 
        ${resultKeys.length > 0
          ? html`
              <div style="padding:1em;">
                <div
                  style="display:flex;font-size:1.2em;position:relative;right:0px"
                >
                  <div style="flex-grow:1"></div>
                </div>
 
                <table width="100%">
                  <tr>
                    <th style="text-align:left" width="25%">Result</th>
                    <th style="text-align:left" width="25%">Category</th>
                    <th style="text-align:left">Value</th>
                  </tr>
 
                  ${Object.keys(this.selectedRun.values).map((key: string) => {
                    const result = this.selectedRun.values[key];
                    const meta = this.resultKeys[key];
 
                    // if our result is no longer represented in the flow, skip it
                    if (meta) {
                      return html`<tr>
                        <td>${result.name}</td>
                        <td>
                          ${meta.categories.length > 1 ? result.category : '--'}
                        </td>
                        <td>${result.value}</td>
                      </tr>`;
                    }
                    return null;
                  })}
                </table>
              </div>
            `
          : null}
      </div>
    </div>`;
  }
 
  constructor() {
    super();
    this.reverseRefresh = false;
    this.valueKey = 'uuid';
    this.hideShadow = true;
    this.createRenderOption();
  }
}