All files / src/live ContactFields.ts

95.34% Statements 348/365
54.79% Branches 40/73
93.33% Functions 14/15
95.34% Lines 348/365

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 366125x 125x 125x 125x 125x 125x 125x 127x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 6x 6x 6x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 124x 125x 131x 131x 131x 131x 131x 131x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 125x 7x 7x 7x 7x 125x 192x 192x 169x 169x 125x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 144x 144x 125x 48x 48x 48x 147x 147x 147x 147x 147x 147x 147x 147x 147x 48x 48x 125x 125x 125x 125x 125x 125x 125x 125x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 23x   8x 8x 8x 8x                       125x 23x 23x 23x 23x 23x 23x 16x 16x 16x 16x 16x 16x 23x 23x 23x 23x 23x 23x 23x 23x 16x 16x 16x 16x 16x 16x 125x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x           125x 125x 125x 125x 125x 125x 125x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 154x 124x 154x 154x 124x 154x 154x 154x 154x 154x 154x 154x 124x 154x 154x 154x 124x 124x 154x 154x 154x 154x 154x 124x 124x 124x 154x 154x 124x 124x 124x 154x 124x 124x 124x 154x 154x 124x 124x 154x 154x 154x 154x 154x 154x 125x 124x 124x 124x 125x 170x 170x 170x 170x 170x 125x 125x 125x 125x 170x 170x 170x 170x 170x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x  
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 { Contact, ContactField, CustomEventType } from '../interfaces';
import { Events } from '../events/eventRenderers';
 
const MIN_FOR_FILTER = 10;
 
export class ContactFields extends ContactStoreElement {
  // we render field values, so field changes are the only events we need -
  // the central watcher hands us a refetched contact when they happen
  protected watchTypes = [Events.CONTACT_FIELD_CHANGED];
 
  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;
        align-items: center;
        margin-top: 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;
 
  // tracks whether the store's contact field definitions have loaded. these
  // load independently of the contact data this element fetches, so we can't
  // render field rows until they're present (see render). this is reactive so
  // that flipping it re-renders once the definitions arrive.
  @property({ type: Boolean, attribute: false })
  fieldsReady = false;
 
  connectedCallback(): void {
    super.connectedCallback();
    this.handleFieldChanged = this.handleFieldChanged.bind(this);
 
    // field definitions are usually already loaded by the time this element
    // connects, but during a cold load they may still be in flight
    this.fieldsReady =
      !!this.store &&
      (this.store.ready ||
        (this.store.getKeyedAssets()['fields'] || []).length > 0);
  }
 
  protected storeUpdated(event: CustomEvent) {
    super.storeUpdated(event);
    // the store fires a StoreUpdated for the fields endpoint once the field
    // definitions have loaded; that's our signal that it's safe to render
    if (this.store && event.detail.url === this.store.fieldsEndpoint) {
      this.fieldsReady = true;
    }
  }
 
  private isAgent(): boolean {
    return this.role === 'T' || this.role?.toLowerCase() === 'agent';
  }
 
  private getAgentAccess(field: ContactField): 'none' | 'view' | 'edit' {
    const access = field.agent_access?.toLowerCase();
    if (access === 'n' || access === 'none') return 'none';
    if (access === 'e' || access === 'edit') return 'edit';
    return 'view';
  }
 
  private canViewField(field: ContactField): boolean {
    return !this.isAgent() || this.getAgentAccess(field) !== 'none';
  }
 
  private canEditField(field: ContactField): boolean {
    return (
      !this.disabled &&
      (!this.isAgent() || this.getAgentAccess(field) === 'edit')
    );
  }
 
  private getVisibleFieldEntries(): [string, string][] {
    if (!this.data || !this.fieldsReady) return [];
    return Object.entries(this.data.fields).filter(
      ([key]: [string, string]) => {
        const field = this.store.getContactField(key);
        return !!field && this.canViewField(field);
      }
    );
  }
 
  /**
   * A watcher delivery carries server state from before an edit the user is
   * still making, so keep the value we already have for any field whose
   * editor is dirty - rebinding it would drop what they typed.
   */
  protected mergeWatchedContact(contact: Contact, previous: Contact): Contact {
    if (!previous || !previous.fields) {
      return contact;
    }
 
    const editors = Array.from(
      this.shadowRoot?.querySelectorAll('temba-contact-field') || []
    ) as ContactFieldEditor[];
    const editing = editors.filter(
      (editor) => editor.dirty && editor.key in previous.fields
    );
 
    if (editing.length === 0) {
      return contact;
    }
 
    const fields = { ...contact.fields };
    editing.forEach((editor) => {
      fields[editor.key] = previous.fields[editor.key];
    });
    return { ...contact, fields };
  }
 
  public willUpdate(changed: PropertyValues): void {
    super.willUpdate(changed);
    if (
      (changed.has('data') ||
        changed.has('fieldsReady') ||
        changed.has('role')) &&
      this.data &&
      this.fieldsReady
    ) {
      if (this.getVisibleFieldEntries().length <= MIN_FOR_FILTER) {
        this.showAll = true;
      }
    }
  }

  protected updated(
    changes: PropertyValueMap<any> | Map<PropertyKey, unknown>
  ): void {
    super.updated(changes);
    if (
      (changes.has('data') ||
        changes.has('fieldsReady') ||
        changes.has('role')) &&
      this.data &&
      this.fieldsReady
    ) {
      this.fireCustomEvent(CustomEventType.DetailsChanged, {
        count: this.getVisibleFieldEntries().filter(([, value]) => !!value)
          .length
      });
    }
  }
 
  public handleFieldChanged(evt: InputEvent) {
    const field = evt.currentTarget as ContactFieldEditor;
    const definition = this.store.getContactField(field.key);
    if (!definition || !this.canEditField(definition)) return;
 
    const value = field.value;
 
    // TODO: Use contact.postChanges instead of postJSON
    postJSON(this.writeEndpoint + 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 {
    // we need both the contact data and the field definitions (loaded during
    // store init) before we can render. these load independently, so the
    // contact data can arrive first; rendering then would crash on missing
    // field definitions and leave the tab empty (see getContactField below).
    if (this.data && this.fieldsReady) {
      // guard against keys with no matching field definition (mid-load races
      // or fields that have since been deleted) so a single missing
      // definition can't throw and blank out the entire tab
      const fieldsToShow = this.getVisibleFieldEntries().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.canEditField(field)}
        ></temba-contact-field>`;
      });
 
      return html`
        <div class=${getClasses({ disabled: this.disabled })}>
          <div class="fields ${this.showAll ? 'show-all' : ''}">${fields}</div>
          ${fieldsToShow.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();
  }
}