All files / src/live ContactFields.ts

98.85% Statements 259/262
44.18% Branches 19/43
88.88% Functions 8/9
98.85% Lines 259/262

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 26392x 92x 92x 92x 92x 92x 92x 94x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 92x 2x 2x 2x 92x 9x 9x 9x 9x 9x 92x 5x 5x 3x 3x 3x 5x 92x 92x 5x 5x 3x 3x 3x 3x 3x 3x 92x 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 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 91x 97x 97x 91x 91x 97x 97x 97x 97x 97x 97x 91x 97x 97x 97x 97x 97x 97x 91x 91x 97x 97x 97x 91x 91x 97x 97x 91x 91x 97x 97x 91x 91x 97x 97x 97x 91x 91x 97x 97x 97x 97x 97x 91x 97x 97x 97x 97x 97x 97x 92x 91x 92x 100x 100x 100x 100x 92x 92x 92x 92x 92x 92x 92x 92x 92x 100x 100x 100x 92x 92x 92x 92x 92x 91x 91x 91x 91x 92x 92x 92x 92x 92x 92x 92x 92x  
import {
  css,
  html,
  PropertyValueMap,
  PropertyValues,
  TemplateResult
} from 'lit';
import { property } from 'lit/decorators.js';
import { getClasses, postJSON } from '../utils';
import { ContactFieldEditor } from './ContactFieldEditor';
import { ContactStoreElement } from './ContactStoreElement';
import { Checkbox } from '../form/Checkbox';
import { CustomEventType } from '../interfaces';
 
const MIN_FOR_FILTER = 10;
 
export class ContactFields extends ContactStoreElement {
  static get styles() {
    return css`
      .field {
        display: flex;
        margin: 1em 0.3em;
        box-shadow: 0 0 0.2em rgba(0, 0, 0, 0.15);
        border-radius: 0px;
        align-items: center;
        overflow: hidden;
      }
 
      .show-all .unset,
      .featured {
        display: block !important;
      }
 
      .unset {
        display: none;
      }
 
      .field:hover {
        box-shadow:
          1px 1px 6px 2px rgba(0, 0, 0, 0.05),
          0px 0px 0px 2px var(--color-link-primary);
        cursor: pointer;
      }
 
      .label {
        padding: 0.25em 1em;
        border-top-left-radius: 0px;
        border-bottom-left-radius: 0px;
        color: #777;
        font-size: 0.9em;
        font-weight: 400;
        box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.1) inset;
      }
 
      .value {
        --icon-color: #ddd;
        max-width: 150px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        padding: 0.25em 1em;
        border-top-right-radius: 0px;
        border-bottom-right-radius: 0px;
        font-size: 0.9em;
      }
 
      .toggle {
        display: flex;
        background: #fff;
        align-items: center;
        margin-bottom: 0.5em;
      }
 
      .disabled .toggle {
        display: none;
      }
    `;
  }
 
  @property({ type: Boolean })
  system: boolean;
 
  @property({ type: Boolean })
  dirty: boolean;
 
  @property({ type: Boolean })
  showAll: boolean;
 
  @property({ type: String })
  timezone: string;
 
  @property({ type: String })
  role: string;
 
  @property({ type: Boolean })
  disabled = false;
 
  connectedCallback(): void {
    super.connectedCallback();
    this.handleFieldChanged = this.handleFieldChanged.bind(this);
  }
 
  private isAgent(): boolean {
    return this.role === 'T';
  }
 
  public willUpdate(changed: PropertyValues): void {
    super.willUpdate(changed);
    if (changed.has('data') && this.data) {
      if (Object.keys(this.data.fields).length <= MIN_FOR_FILTER) {
        this.showAll = true;
      }
    }
  }
 
  protected updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);
    if (changes.has('data') && this.data) {
      this.fireCustomEvent(CustomEventType.DetailsChanged, {
        count: Object.values(this.data.fields).filter((value) => !!value).length
      });
    }
  }
 
  public handleFieldChanged(evt: InputEvent) {
    const field = evt.currentTarget as ContactFieldEditor;
    const value = field.value;
 
    // TODO: Use contact.postChanges instead of postJSON
    postJSON('/api/v2/contacts.json?uuid=' + this.data.uuid, {
      fields: { [field.key]: value }
    })
      .then((response: any) => {
        field.handleResponse(response);
 
        // returns a single contact with latest updates
        this.setContact(response.json);
      })
      .catch((error) => {
        field.handleResponse(error);
      });
  }
 
  public handleToggle(evt: Event) {
    const checkbox = evt.currentTarget as Checkbox;
    this.showAll = checkbox.checked;
  }
 
  public render(): TemplateResult {
    if (this.data) {
      const fieldsToShow = Object.entries(this.data.fields).sort(
        (a: [string, string], b: [string, string]) => {
          const [ak] = a;
          const [bk] = b;
          const fieldA = this.store.getContactField(ak);
          const fieldB = this.store.getContactField(bk);
 
          if (fieldA.type === 'ward') {
            return 1;
          }
 
          if (fieldB.type === 'ward') {
            return -1;
          }
 
          if (
            fieldA.type === 'district' &&
            fieldB.type !== 'ward' &&
            fieldB.type !== 'district'
          ) {
            return 1;
          }
 
          if (
            fieldB.type === 'district' &&
            fieldA.type !== 'ward' &&
            fieldA.type !== 'district'
          ) {
            return -1;
          }
 
          if (
            fieldA.type === 'state' &&
            fieldB.type !== 'ward' &&
            fieldB.type !== 'district' &&
            fieldB.type !== 'state'
          ) {
            return 1;
          }
 
          if (
            fieldB.type === 'state' &&
            fieldA.type !== 'ward' &&
            fieldA.type !== 'district' &&
            fieldA.type !== 'state'
          ) {
            return -1;
          }
 
          if (fieldA.featured && !fieldB.featured) {
            return -1;
          }
 
          if (fieldB.featured && !fieldA.featured) {
            return 1;
          }
 
          const priority = fieldB.priority - fieldA.priority;
          if (priority !== 0) {
            return priority;
          }
 
          return ak.localeCompare(bk);
        }
      );
 
      if (fieldsToShow.length == 0) {
        return html`<slot name="empty"></slot>`;
      }
 
      const fields = fieldsToShow.map((entry: [string, string]) => {
        const [k, v] = entry;
        const field = this.store.getContactField(k);
        return html`<temba-contact-field
          class=${getClasses({ set: !!v, unset: !v, featured: field.featured })}
          key=${field.key}
          name=${field.label}
          value=${v}
          type=${field.value_type}
          @change=${this.handleFieldChanged}
          timezone=${this.timezone}
          ?disabled=${(this.isAgent() && field.agent_access === 'view') ||
          this.disabled
            ? true
            : false}
        ></temba-contact-field>`;
      });
 
      return html`
        <div class=${getClasses({ disabled: this.disabled })}>
          <div class="fields ${this.showAll ? 'show-all' : ''}">${fields}</div>
          ${Object.keys(this.data.fields).length >= MIN_FOR_FILTER
            ? html`<div class="toggle">
                <div style="flex-grow: 1"></div>
                <div>
                  <temba-checkbox
                    ?checked=${this.showAll}
                    @change=${this.handleToggle}
                    label="Show All"
                  ></temba-checkbox>
                </div>
              </div>`
            : null}
        </div>
      `;
    }
 
    return super.render();
  }
}