All files / src/live ContactFieldEditor.ts

83.52% Statements 502/601
85% Branches 34/40
64.7% Functions 11/17
83.52% Lines 502/601

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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 60292x 100x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 91x 91x 91x 92x 92x 91x 91x 91x 92x 92x 92x 92x 91x 91x 91x 91x 91x 91x 91x 91x 92x 92x 92x 92x 92x 92x 92x 16x 16x 16x 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 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 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 16x 16x 16x 16x 16x 16x 16x 16x 92x                                                                   92x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                             92x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 92x 4x 4x 4x 4x 4x 92x     3x 3x 2x 2x 2x 2x 7x 7x 7x 5x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 7x 7x 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 7x 7x 7x 7x 7x 7x 7x 7x                     92x 92x 92x 92x                                                                 92x 92x 92x 92x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 91x 92x 92x 90x 91x 91x 91x 91x 91x 92x 91x 91x 91x 91x 91x 91x 91x 92x 92x 92x 92x 92x 92x 92x  
import { css, html, TemplateResult } from 'lit';
import { property } from 'lit/decorators.js';
import { CustomEventType } from '../interfaces';
import { RapidElement } from '../RapidElement';
import { InputType, TextInput } from '../form/TextInput';
import { Icon } from '../Icons';
import { getClasses, WebResponse } from '../utils';
import { Select } from '../form/select/Select';
import { FieldElement } from '../form/FieldElement';
 
enum Status {
  Success = 'success',
  Failure = 'failure',
  Saving = 'saving',
  Ready = 'ready'
}
 
export class ContactFieldEditor extends RapidElement {
  @property({ type: String })
  key: string;
 
  @property({ type: String })
  value: string;
 
  @property({ type: String })
  name: string;
 
  @property({ type: String })
  type: string;
 
  @property({ type: String })
  timezone: string;
 
  @property({ type: String })
  icon = navigator.clipboard ? Icon.copy : '';
 
  @property({ type: String })
  iconClass = '';
 
  @property({ type: String })
  status: Status = Status.Ready;
 
  @property({ type: Boolean })
  disabled = false;
 
  @property({ type: Boolean })
  dirty = false;
 
  static get styles() {
    return css`
      :host {
        --transition-speed: 0ms;
        margin-bottom: 1em;
        display: block;
      }
 
      .wrapper {
        --disabled-opacity: 1;
        position: relative;
        --color-widget-bg: transparent;
        --color-widget-bg-focused: #fff;
        --widget-box-shadow: none;
        padding-bottom: 0.6em;
        border-bottom: 1px solid #ececec;
      }
 
      .wrapper.disabled {
        --color-widget-border: transparent;
      }
 
      .wrapper.mutable:hover {
      }
 
      .wrapper.mutable {
        --color-widget-border: rgb(235, 235, 235);
        --color-widget-bg: transparent;
        --input-cursor: pointer;
 
        border-bottom: none;
        margin-bottom: 0.5em;
        padding-bottom: 0em;
      }
 
      .mutable.success {
        --color-widget-border: rgba(var(--success-rgb), 0.6);
      }
 
      .mutable.failure {
        --color-widget-border: rgba(var(--error-rgb), 0.3) !important;
      }
 
      .mutable .dirty {
        --color-widget-border: rgb(235, 235, 235);
      }
 
      .prefix {
        border-top-left-radius: var(--curvature-widget);
        border-bottom-left-radius: var(--curvature-widget);
        cursor: pointer !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        display: flex;
        position: absolute;
        margin-top: -0.6em;
        margin-left: 0.5em;
        pointer-events: none;
        background: #fff;
        border-radius: var(--curvature);
      }
 
      temba-select .prefix {
        margin-top: -1em;
      }
 
      .wrapper {
        margin-bottom: 0.5em;
      }
 
      .prefix .name,
      .label .name {
        padding: 0em 0.4em;
        color: rgba(100, 100, 100, 0.7);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        font-size: 0.8em;
      }
 
      .disabled .name {
        margin-top: 1em;
        margin-left: 0.75em;
      }
 
      .disabled .value {
        margin-left: 0.9em;
        margin-top: 0.1em;
        min-height: 1.75em;
      }
 
      .postfix {
        display: flex;
        align-items: stretch;
        margin-left: 1em;
      }
 
      .popper {
        background: rgba(0, 0, 0, 0.03);
        border-top-right-radius: var(--curvature-widget);
        border-bottom-right-radius: var(--curvature-widget);
        --icon-color: #888;
        display: flex;
        cursor: default;
        transition: all var(--transition-speed) ease-in-out;
        align-items: stretch;
        margin: -1px;
      }
 
      temba-icon[name='calendar'] {
        --icon-color: rgba(0, 0, 0, 0.2);
      }
 
      temba-icon:hover {
        --icon-color: rgba(0, 0, 0, 0.5);
      }
 
      temba-icon {
        cursor: pointer;
        --icon-color: rgba(0, 0, 0, 0.3);
      }
 
      temba-textinput:focus .popper,
      temba-textinput:hover .popper {
        display: flex;
      }
 
      .disabled temba-textinput .postfix {
        display: none;
        padding: none;
      }
 
      .unset temba-textinput .popper .copy,
      .unset temba-textinput .popper .search {
        display: none;
      }
 
      .unset temba-textinput:focus .popper .copy,
      .unset temba-textinput:hover .popper .copy,
      .unset temba-textinput:focus .popper .save,
      .unset temba-textinput:hover .popper .save {
        display: none;
      }
 
      .popper temba-icon {
        padding: 0.5em 0em;
        padding-right: 1em;
      }
 
      .popper:first-child {
        padding: 0.5em 0em;
        padding-right: 0.5em;
        padding-left: 1em;
      }
 
      .popper:last-child {
        padding-right: 0em;
      }
 
      .copy.clicked temba-icon {
        transform: scale(1.2);
      }
 
      temba-icon {
        transition: all 200ms ease-in-out;
      }
 
      temba-datepicker {
        position: relative;
      }
 
      .save-state {
        display: flex;
        align-items: center;
      }
 
      .save-button {
        padding-right: 1em;
      }
 
      .dirty .copy,
      .dirty .search {
        display: none;
      }
 
      .saving .copy,
      .saving .search {
        display: none;
      }
 
      .success .copy,
      .success .search {
        display: none;
      }
 
      .failure .copy,
      .failure .search {
        display: none;
      }
 
      .popper.success {
        background: rgb(var(--success-rgb));
      }
 
      .popper.failure {
        background: rgb(var(--error-rgb));
      }
 
      .popper.success temba-icon,
      .popper.failure temba-icon {
        --icon-color: #fff !important;
      }
 
      .popper.dirty {
        background: rgba(0, 0, 0, 0.03);
      }
 
      temba-datepicker .popper {
        border-radius: 0px;
      }
 
      temba-datepicker .popper:first-child {
        padding: 0;
      }
 
      .dirty temba-datepicker .popper:first-child {
        padding-left: 1em;
      }
 
      .success temba-datepicker .popper:first-child {
        padding-left: 1em;
      }
 
      .failure temba-datepicker .popper:first-child {
        padding-left: 1em;
      }
 
      .saving temba-datepicker .popper:first-child {
        padding-left: 1em;
      }
 
      temba-datepicker .postfix {
        margin-left: 0;
      }
 
      temba-datepicker {
        --temba-textinput-padding: 0.8em 0.8em 0.4em 0.8em;
      }
 
      .saving temba-datepicker,
      .saving temba-textinput {
        pointer-events: none !important;
        cursor: default !important;
        opacity: 0.7;
      }
 
      temba-select {
        --color-widget-bg: white;
      }
 
      temba-option {
      }
    `;
  }
 
  connectedCallback(): void {
    super.connectedCallback();
    this.handleInput = this.handleInput.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
 
  public handleIconClick(evt: MouseEvent) {
    const ele = evt.target as HTMLDivElement;
    const icon = ele.getAttribute('icon-action');
    const input = this.shadowRoot.querySelector('temba-textinput') as TextInput;
 
    if (icon === 'copy') {
      if (navigator.clipboard) {
        this.iconClass = 'clicked';
        navigator.clipboard.writeText(input.getDisplayValue()).then(() => {
          window.setTimeout(() => {
            this.iconClass = '';
          }, 300);
        });
      }
    }
 
    if (icon === 'search') {
      this.fireCustomEvent(CustomEventType.ButtonClicked, {
        key: this.key,
        value: this.value
      });
    }

    evt.preventDefault();
    evt.stopPropagation();
  }

  public handleResponse(response: WebResponse) {
    if (response.status === 200) {
      this.value = response.json.fields[this.key];
      // this.status = Status.Success;
      // on success lets go back to ready state for now
      this.status = Status.Ready;
      this.dirty = false;
    } else {
      this.status = Status.Failure;
      this.dirty = false;
    }
  }

  public handleSelectChange(evt: CustomEvent) {
    const select = evt.currentTarget as Select<any>;
    let value = '';

    evt.preventDefault();
    evt.stopPropagation();

    if (select.values.length > 0) {
      value = select.values[0].path;
    }

    if (value !== this.value) {
      this.dirty = true;
      this.status = Status.Saving;
      this.value = value;
      this.fireEvent('change');
    }
  }
 
  public handleSubmit() {
    const input = this.shadowRoot.querySelector(
      'temba-textinput, temba-datepicker'
    ) as FieldElement;

    if (input.value !== this.value) {
      this.dirty = true;
      this.status = Status.Saving;
      this.value = input.value;
      this.fireEvent('change');
    }
  }

  public handleChange(evt: Event) {
    evt.preventDefault();
    evt.stopPropagation();
  }

  public handleDateChange(evt: Event) {
    evt.preventDefault();
    evt.stopPropagation();
    this.dirty = true;
  }

  public handleInput(evt: KeyboardEvent) {
    const input = evt.currentTarget as TextInput;
    if (evt.key === 'Enter') {
      input.blur();
      this.handleSubmit();
    } else {
      if (input.value !== this.value) {
        this.dirty = true;
      }
    }
  }
 
  private getInputType(type: string): string {
    if (type === 'numeric') {
      return InputType.Number;
    }
    return InputType.Text;
  }
 
  private renderDateField(state: TemplateResult) {
    return html` <temba-datepicker
      timezone=${this.timezone}
      value="${this.value ? this.value : ''}"
      @change=${this.handleDateChange}
      ?disabled=${this.disabled}
      time
    >
      <div class="prefix" slot="prefix">
        <div class="name">${this.name}</div>
      </div>
      <div class="postfix" slot="postfix">
        <div class="popper ${this.status}  ${this.dirty ? 'dirty' : ''}">
          ${state}
        </div>
      </div>
    </temba-datepicker>`;
  }
 
  private renderTextField(state: TemplateResult) {
    return html`
      <temba-textinput
        class="${this.status} ${this.dirty ? 'dirty' : ''}"
        value="${this.value ? this.value : ''}"
        @keyup=${this.handleInput}
        @change=${this.handleChange}
        type=${this.getInputType(this.type)}
        ?disabled=${this.disabled}
      >
        <div class="prefix" slot="prefix">
          <div class="name">${this.name}</div>
        </div>
 
        <div class="postfix">
          <div
            class="popper ${this.iconClass} ${this.status}  ${this.dirty
              ? 'dirty'
              : ''}"
            @click=${this.handleIconClick}
          >
            ${state}
            <temba-icon
              class="search"
              icon-action="search"
              name="${Icon.search}"
              animateclick="pulse"
            ></temba-icon>
            <temba-icon
              class="copy"
              icon-action="copy"
              name="${this.icon}"
              animatechange="spin"
              animateclick="pulse"
            ></temba-icon>
          </div>
        </div>
      </temba-textinput>
    `;
  }
 
  private getOptions(response: WebResponse) {
    return response.json[0].children;
  }
 
  private renderSelectedLocation(option: any) {
    if (!option) {
      return null;
    }
 
    return html`
      <div
        class="selected-location"
        style="display:flex;margin-top:1em;margin-left:0.1em"
      >
        <span>${option['path']}</span>
      </div>
    `;
  }
 
  public renderLocationField(level: string = 'state') {
    return html`
      <temba-select
        endpoint="/api/internal/locations.json?level=${level}"
        nameKey="path"
        valueKey="path"
        @change=${this.handleSelectChange}
        .resnderSelectedItem=${this.renderSelectedLocation}
        placeholder="Select a ${level}"
        queryParam="query"
        searchable
        clearable
        inpsutStyle=${JSON.stringify({ 'margin-top': '1.1em !important;' })}
        values=${this.value
          ? JSON.stringify([{ path: this.value, osm_id: this.value }])
          : '[]'}
      >
        <div class="prefix" slot="prefix">
          <div class="name">${this.name}</div>
        </div>
      </temba-select>
    `;
  }

  public render(): TemplateResult {
    if (this.disabled) {
      return html`<div
        class=${this.status +
        ' ' +
        getClasses({
          wrapper: true,
          set: !!this.value,
          unset: !this.value,
          disabled: this.disabled,
          mutable: !this.disabled,
          dirty: this.dirty
        })}
      >
        <div class="label"><div class="name">${this.name}</div></div>
        <div class="value">
          ${this.type === 'datetime'
            ? this.value
              ? html`<temba-date
                  value=${this.value}
                  display="datetime"
                ></temba-date>`
              : null
            : this.value}
        </div>
      </div>`;
    }

    const state = html`<div class="save-state">
      ${this.dirty
        ? html`<temba-button
            class="save-button"
            name="Save"
            small
            @click=${this.handleSubmit}
          ></temba-button>`
        : html` ${this.status === Status.Saving
            ? html`<temba-icon
                spin
                name="${Icon.progress_spinner}"
              ></temba-icon>`
            : null}
          ${this.status === Status.Success && !this.dirty
            ? html`<temba-icon name="${Icon.success}"></temba-icon>`
            : null}
          ${this.status === Status.Failure
            ? html`<temba-tip text="Failed to save changes, try again later."
                ><temba-icon name="${Icon.alert_warning}"></temba-icon
              ></temba-tip>`
            : null}`}
    </div>`;
 
    return html`
      <div
        class=${this.status +
        ' ' +
        getClasses({
          wrapper: true,
          set: !!this.value,
          unset: !this.value,
          disabled: this.disabled,
          mutable: !this.disabled,
          dirty: this.dirty
        })}
      >
        ${this.type === 'datetime'
          ? this.renderDateField(state)
          : this.type === 'state' ||
              this.type === 'district' ||
              this.type === 'ward'
            ? this.renderLocationField(this.type)
            : this.renderTextField(state)}
      </div>
    `;
  }
}