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 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 | 125x 148x 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 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 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 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 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 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 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 222x 222x 143x 143x 222x 125x 80x 80x 125x 75x 125x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 207x 207x 207x 125x 32x 32x 32x 32x 32x 32x 32x 32x 32x 125x 12x 12x 1x 11x 125x 13x 13x 13x 13x 13x 13x 1x 1x 13x 12x 12x 12x 12x 12x 12x 17x 17x 12x 12x 12x 12x 13x 13x 12x 13x 11x 13x 13x 11x 11x 11x 16x 16x 16x 11x 11x 11x 11x 11x 11x 11x 13x 12x 12x 12x 12x 13x 13x 125x 125x 125x 125x 125x 125x 125x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 96x 75x 96x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 15x 125x 125x 3x 125x 7x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 125x 125x 2x 2x 125x 3x 3x 3x 3x 3x 125x 2x 2x 2x 2x 2x 2x 1x 1x 1x 6x 1x 6x 1x 1x 1x 1x 1x 1x 1x 1x 125x 1x 1x 1x 1x 1x 1x 1x 1x 1x 125x 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 125x 1x 1x 1x 7x 125x 125x 125x 1x 1x 1x 125x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 200x 200x 138x 138x 138x 138x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 138x 138x 138x 138x 1x 1x 1x 1x 1x 1x 552x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 75x 75x 1x 75x 75x 75x 75x 75x 1x 1x 1x 74x 74x 74x 74x 74x 74x 74x 74x 74x 74x 1x 1x 1x 1x 1x 1x 1x 1x 74x 2x 2x 2x 45x 45x 45x 45x 1x 1x 1x 1x 75x 75x 54x 54x 54x 54x 69x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 69x 69x 69x 69x 69x 75x 75x 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 75x 75x 75x 75x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 74x 74x 75x 75x 75x 75x 75x 75x 75x 69x 69x 69x 69x 69x 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 42x 42x 42x 42x 21x 42x 1x 1x 1x 1x 42x 21x 21x 21x 21x 21x 75x 75x 75x 74x 74x 74x 75x 75x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 1x 69x 75x 2x 75x 1x 2x 2x 71x 75x 75x 75x 1x 1x 1x 1x 1x 1x 1x 66x 66x 75x 1x 1x 1x 1x 1x 1x 1x 1x 1x 75x 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 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 184x 184x 184x 184x 184x 184x 125x 125x 125x 125x 125x 125x 125x | import { css, html, PropertyValues, TemplateResult } from 'lit';
import { property, state } from 'lit/decorators.js';
import { Icon } from '../Icons';
import { Contact, CustomEventType, Group, URN } from '../interfaces';
import { getLanguageName } from '../languages';
import { capitalize, WebResponse } from '../utils';
import { Select } from '../form/select/Select';
import { TextInput } from '../form/TextInput';
import { ContactFieldEditor } from './ContactFieldEditor';
import { ContactStoreElement, getDestinationURN } from './ContactStoreElement';
import { Events } from '../events/eventRenderers';
interface Option {
name: string;
value: string;
}
const STATUS_OPTIONS: Option[] = [
{ value: 'active', name: 'Active' },
{ value: 'blocked', name: 'Blocked' },
{ value: 'stopped', name: 'Stopped' },
{ value: 'archived', name: 'Archived' }
];
export class ContactDetails extends ContactStoreElement {
// the contact state we render - live changes to any of these reach us
// through the central watcher
protected watchTypes = [
Events.CONTACT_NAME_CHANGED,
Events.CONTACT_URNS_CHANGED,
Events.CONTACT_LANGUAGE_CHANGED,
Events.CONTACT_STATUS_CHANGED,
Events.CONTACT_GROUPS_CHANGED
];
@property({ type: Boolean })
editable = false;
@property({ type: Boolean })
anon = false;
@property({ type: Array })
schemes: Option[] = [];
@state()
private newScheme = '';
@state()
private newUrn = '';
@state()
private urnDialogOpen = false;
@state()
private draftUrns: URN[] = [];
@state()
private saving = new Set<string>();
@state()
private failed = new Set<string>();
private saveGeneration = 0;
private saveGenerations = new Map<string, number>();
private fieldSaveGenerations = new Map<string, number>();
private groupsSave: Promise<WebResponse> | null = null;
static get styles() {
return css`
.wrapper {
padding-top: 0;
}
.row {
padding-bottom: 0.6em;
border-bottom: 1px solid #ececec;
margin-bottom: 1em;
}
.row .label {
color: var(--text-2);
font-size: 11px;
font-weight: var(--w-medium);
margin: 0 0 4px 2px;
}
.row .value {
margin-left: 0.25em;
margin-top: 0.35em;
min-height: 1.75em;
display: flex;
flex-wrap: wrap;
gap: 0.4em;
}
.editable-row {
position: relative;
margin-bottom: 1em;
}
.editable-row > label {
display: block;
color: var(--text-2);
font-size: 12px;
font-weight: var(--w-medium);
margin: 0 0 4px 2px;
}
.editable-row temba-select {
display: block;
--color-widget-bg: white;
}
.editable-row.saving {
pointer-events: none;
opacity: 0.7;
}
.editable-row.failed {
--color-widget-border: rgba(var(--error-rgb), 0.5);
}
.error {
position: absolute;
right: 8px;
top: 29px;
z-index: 1;
--icon-color: rgb(var(--error-rgb));
}
.dynamic-groups {
display: flex;
flex-wrap: wrap;
gap: 0.4em;
margin: 0.5em 0 0 0.25em;
}
.urn-display {
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 8px;
align-items: start;
margin-bottom: 14px;
}
.urn-display.editable {
grid-template-columns: minmax(0, 1fr) auto 24px;
}
.urn-display temba-contact-field {
--contact-field-separator: none;
--contact-field-padding-bottom: 0;
--contact-field-wrapper-margin-bottom: 0;
margin-bottom: 0;
}
.urn-display.only-unsendable {
border-left: 3px solid rgba(var(--error-rgb), 0.65);
background: rgba(var(--error-rgb), 0.05);
padding: 6px 8px 6px 10px;
}
.urn-display.only-unsendable temba-contact-field {
--contact-field-value-icon-color: rgb(var(--error-rgb));
}
.urn-more-count {
display: flex;
align-items: center;
align-self: center;
height: 24px;
margin-top: 22px;
color: var(--text-2);
font-size: 12px;
white-space: nowrap;
}
.urn-edit-icon {
display: flex;
width: 24px;
height: 24px;
margin-top: 22px;
justify-self: center;
cursor: pointer;
--icon-color: var(--text-2);
}
.urn-edit-icon:hover,
.urn-edit-icon:focus-visible {
--icon-color: var(--text-1);
}
.urn-sendability {
display: flex;
align-items: center;
justify-content: center;
--icon-color: var(--text-3);
}
temba-textinput .urn-sendability {
margin: 0 8px 0 10px;
}
.urn-delete-action {
display: flex;
align-items: center;
margin: 0 10px 0 4px;
}
.urn-delete-icon {
display: flex;
cursor: pointer;
--icon-color: var(--text-2);
}
.urn-delete-icon:hover,
.urn-delete-icon:focus-visible {
--icon-color: rgb(var(--error-rgb));
}
.urn-dialog {
display: grid;
gap: 20px;
padding: 24px;
width: 100%;
box-sizing: border-box;
}
.urn-list {
display: block;
width: 100%;
}
.urn-row {
display: grid;
grid-template-columns: 18px minmax(0, 1fr);
gap: 10px;
align-items: center;
width: 100%;
min-width: 0;
box-sizing: border-box;
}
.urn-row temba-textinput {
display: block;
width: 100%;
min-width: 0;
}
.drag-slot {
align-self: end;
display: flex;
align-items: center;
justify-content: center;
height: calc(var(--input-h, 34px) + 2px);
box-sizing: border-box;
}
.drag-handle {
cursor: grab;
--icon-color: var(--text-2);
}
.drag-handle:active {
cursor: grabbing;
}
.add-urn-fields {
display: grid;
grid-template-columns: minmax(120px, 0.8fr) minmax(160px, 1.2fr) 72px;
gap: 8px;
align-items: end;
margin-left: 28px;
padding-top: 16px;
border-top: 1px solid var(--border-light, #ececec);
}
.add-urn-fields label {
display: block;
color: var(--text-2);
font-size: 12px;
font-weight: var(--w-medium);
margin: 0 0 4px 2px;
}
.add-urn-fields temba-select,
.add-urn-fields temba-textinput {
display: block;
width: 100%;
min-width: 0;
}
.add-urn-button {
min-height: var(--input-h, 34px);
display: flex;
align-items: center;
justify-content: flex-end;
}
.add-urn-button temba-button {
--button-height: calc(var(--input-h, 34px) + 2px);
}
.add-urn-error {
grid-column: 1 / -1;
color: rgb(var(--error-rgb));
font-size: 12px;
}
@media (max-width: 480px) {
.add-urn-fields {
grid-template-columns: 1fr;
margin-left: 28px;
}
}
temba-contact-field.last-field {
--contact-field-separator: none;
margin-bottom: 0;
}
`;
}
private getSchemeName(scheme: string): string {
return (
this.schemes.find((option) => option.value === scheme)?.name ||
capitalize(scheme as any)
);
}
private getManualGroups(): Group[] {
return this.data.groups.filter((group: Group) => !group.is_dynamic);
}
private getDynamicGroups(): Group[] {
return this.data.groups.filter((group: Group) => group.is_dynamic);
}
private getLanguageOptions(): Option[] {
const codes = [...(this.store.getWorkspace()?.languages || [])];
if (this.data.language && !codes.includes(this.data.language)) {
codes.unshift(this.data.language);
}
return [
{ value: 'none', name: 'No Preference' },
...codes.map((code) => ({ value: code, name: getLanguageName(code) }))
];
}
private setSaveState(key: string, status: 'saving' | 'failed' | 'ready') {
const saving = new Set(this.saving);
const failed = new Set(this.failed);
saving.delete(key);
failed.delete(key);
if (status === 'saving') saving.add(key);
if (status === 'failed') failed.add(key);
this.saving = saving;
this.failed = failed;
}
private finishSave(
contactId: string,
key: string,
generation: number,
status: 'failed' | 'ready'
) {
if (
this.contact !== contactId ||
this.saveGenerations.get(key) !== generation
) {
return;
}
this.saveGenerations.delete(key);
this.setSaveState(key, status);
}
private async save(key: string, payload: any): Promise<WebResponse> {
const contactId = this.contact;
if (
!contactId ||
!this.data ||
this.data.uuid !== contactId ||
this.url !== `${this.endpoint}${contactId}`
) {
return { status: 409, json: {}, headers: new Headers() };
}
const contactUuid = this.data.uuid;
const generation = ++this.saveGeneration;
const fields = Object.keys(payload);
this.saveGenerations.set(key, generation);
fields.forEach((field) => this.fieldSaveGenerations.set(field, generation));
this.setSaveState(key, 'saving');
try {
const response = await this.store.postJSON(
`${this.writeEndpoint}${contactUuid}`,
payload
);
if (response.status !== 200) {
fields.forEach((field) => {
if (this.fieldSaveGenerations.get(field) === generation) {
this.fieldSaveGenerations.delete(field);
}
});
this.finishSave(contactId, key, generation, 'failed');
return response;
}
if (
this.contact === contactId &&
this.data?.uuid === contactId &&
this.url === `${this.endpoint}${contactId}`
) {
const updated = { ...this.data };
let hasUpdates = false;
fields.forEach((field) => {
if (this.fieldSaveGenerations.get(field) === generation) {
if (response.json[field] !== undefined) {
updated[field] = response.json[field];
hasUpdates = true;
}
this.fieldSaveGenerations.delete(field);
}
});
if (hasUpdates) {
this.setContact(updated, contactId);
}
}
this.finishSave(contactId, key, generation, 'ready');
return response;
} catch (error) {
fields.forEach((field) => {
if (this.fieldSaveGenerations.get(field) === generation) {
this.fieldSaveGenerations.delete(field);
}
});
this.finishSave(contactId, key, generation, 'failed');
return { status: 500, json: {}, headers: new Headers() };
}
}
/**
* A watcher delivery carries server state from before the writes we still
* have in flight, so keep our own value for anything mid-save - the save
* itself applies the server's version when it lands.
*/
protected mergeWatchedContact(contact: Contact, previous: Contact): Contact {
if (!previous || this.fieldSaveGenerations.size === 0) {
return contact;
}
const merged = { ...contact };
this.fieldSaveGenerations.forEach((generation, field) => {
if (field in previous) {
const value = previous[field];
merged[field] = Array.isArray(value) ? [...value] : value;
}
});
return merged;
}
public willUpdate(changed: PropertyValues): void {
if (changed.has('contact') || changed.has('endpoint')) {
const url = this.contact ? `${this.endpoint}${this.contact}` : null;
if (url !== this.url) {
this.data = null;
}
this.saveGeneration++;
this.saveGenerations.clear();
this.fieldSaveGenerations.clear();
this.groupsSave = null;
this.saving.clear();
this.failed.clear();
this.urnDialogOpen = false;
this.draftUrns = [];
this.newScheme = '';
this.newUrn = '';
}
super.willUpdate(changed);
}
public async handleTextChanged(event: Event) {
const field = event.currentTarget as ContactFieldEditor;
const value = field.value;
const response = await this.save(field.key, {
[field.key]: field.key === 'name' && !value ? null : value
});
field.handleResponse(
response,
response.status === 200 ? response.json[field.key] || '' : value
);
}
public handleDraftUrnChanged(event: Event, index: number) {
this.draftUrns[index].path = (event.currentTarget as TextInput).value;
}
public handleNewSchemeChanged(event: Event) {
const select = event.currentTarget as Select<Option>;
this.newScheme = select.values[0]?.value || '';
}
public handleNewUrnChanged(event: Event) {
this.newUrn = (event.currentTarget as TextInput).value;
}
public showUrnDialog() {
this.newScheme = this.newScheme || this.schemes[0]?.value || '';
this.newUrn = '';
this.draftUrns = this.data.urns.map((urn) => ({ ...urn }));
this.setSaveState('urn-dialog', 'ready');
this.urnDialogOpen = true;
}
public hideUrnDialog() {
this.urnDialogOpen = false;
this.newUrn = '';
this.draftUrns = [];
this.setSaveState('urn-dialog', 'ready');
}
public handleAddUrn() {
const scheme = this.newScheme || this.schemes[0]?.value;
const path = this.newUrn.trim();
if (!scheme || !path) return;
this.draftUrns = [...this.draftUrns, { scheme, path }];
this.newUrn = '';
}
public async handleUrnDialogButton(event: CustomEvent) {
event.stopImmediatePropagation();
if (event.detail?.button?.name === 'Save') {
const response = await this.save('urn-dialog', {
urns: this.draftUrns
.filter((urn) => urn.path.trim())
.map((urn) => `${urn.scheme}:${urn.path.trim()}`)
});
if (response.status === 200) {
this.hideUrnDialog();
}
} else {
this.hideUrnDialog();
}
}
public handleUrnOrderChanged(event: CustomEvent) {
event.stopPropagation();
const [fromIndex, toIndex] = event.detail.swap;
const urns = [...this.draftUrns];
const moved = urns.splice(fromIndex, 1)[0];
urns.splice(toIndex, 0, moved);
this.draftUrns = urns;
}
public handleUrnOrderKeyDown(event: KeyboardEvent, index: number) {
if (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') return;
const toIndex = index + (event.key === 'ArrowUp' ? -1 : 1);
if (toIndex < 0 || toIndex >= this.draftUrns.length) return;
event.preventDefault();
event.stopPropagation();
const urns = [...this.draftUrns];
const moved = urns.splice(index, 1)[0];
urns.splice(toIndex, 0, moved);
this.draftUrns = urns;
void this.updateComplete.then(() => {
(
this.shadowRoot.querySelector(
`#urn-${toIndex} .drag-handle`
) as HTMLElement
)?.focus();
});
}
public handleDeleteUrn(event: Event, index: number) {
event.preventDefault();
event.stopPropagation();
this.draftUrns = this.draftUrns.filter((_, urnIndex) => urnIndex !== index);
}
public handleDeleteUrnKeyDown(event: KeyboardEvent, index: number) {
if (event.key === 'Enter' || event.key === ' ') {
this.handleDeleteUrn(event, index);
}
}
public handleUrnEditKeyDown(event: KeyboardEvent) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
this.showUrnDialog();
}
}
private prepareUrnGhost = (ghost: HTMLElement) => {
ghost.style.setProperty('display', 'grid', 'important');
ghost.style.setProperty(
'grid-template-columns',
'18px minmax(0, 1fr)',
'important'
);
ghost.style.setProperty('column-gap', '10px', 'important');
ghost.style.setProperty('align-items', 'center', 'important');
const dragSlot = ghost.querySelector('.drag-slot') as HTMLElement;
if (dragSlot) {
dragSlot.style.setProperty('display', 'flex', 'important');
dragSlot.style.setProperty('align-items', 'center', 'important');
dragSlot.style.setProperty('justify-content', 'center', 'important');
dragSlot.style.setProperty('align-self', 'end', 'important');
dragSlot.style.setProperty(
'height',
'calc(var(--input-h, 34px) + 2px)',
'important'
);
dragSlot.style.setProperty('box-sizing', 'border-box', 'important');
}
};
public handleSearch(event: CustomEvent) {
event.stopPropagation();
this.fireCustomEvent(CustomEventType.ButtonClicked, event.detail);
}
public async handleGroupsChanged(event: Event) {
const select = event.currentTarget as Select<any>;
const groupsSave = this.save('groups', {
groups: select.values.map((group) => group.uuid)
});
this.groupsSave = groupsSave;
try {
await groupsSave;
} finally {
if (this.groupsSave === groupsSave) {
this.groupsSave = null;
}
}
}
public async handleLanguageChanged(event: Event) {
const select = event.currentTarget as Select<Option>;
await this.save('language', {
language:
!select.values[0]?.value || select.values[0].value === 'none'
? null
: select.values[0].value
});
}
public async handleStatusChanged(event: Event) {
const select = event.currentTarget as Select<Option>;
const status = select.values[0]?.value;
const contactId = this.contact;
if (this.groupsSave) {
await this.groupsSave;
}
if (this.contact !== contactId || this.data?.uuid !== contactId) return;
if (!status || status === this.data.status) return;
const payload: any = { status };
if (this.data.status === 'active') {
// Leaving active normally clears group membership. Include the current
// manual groups so an inline status edit preserves that membership.
payload.groups = this.getManualGroups().map((group) => group.uuid);
}
await this.save('status', payload);
}
private renderFailure(key: string): TemplateResult {
return this.failed.has(key)
? html`<temba-tip class="error" text="Failed to save changes.">
<temba-icon name=${Icon.alert_warning}></temba-icon>
</temba-tip>`
: null;
}
private renderEditableSelect(
key: string,
label: string,
options: Option[],
value: string,
handler: (event: Event) => void
): TemplateResult {
return html`<div
class="editable-row ${this.saving.has(key)
? 'saving'
: ''} ${this.failed.has(key) ? 'failed' : ''}"
>
<label>${label}</label>
${this.renderFailure(key)}
<temba-select
valueKey="value"
.values=${options.filter((option) => option.value === (value || ''))}
@change=${handler}
>
${options.map(
(option) =>
html`<temba-option
name=${option.name}
value=${option.value}
></temba-option>`
)}
</temba-select>
</div>`;
}
private renderDynamicGroups(editable: boolean): TemplateResult {
const groups = this.getDynamicGroups();
if (!groups.length) return null;
return html`<div class="smart-groups ${editable ? 'editable-row' : 'row'}">
${editable
? html`<label>Smart Groups</label>`
: html`<div class="label">Smart Groups</div>`}
<div class=${editable ? 'dynamic-groups' : 'value'}>
${groups.map(
(group) =>
html`<temba-label
onclick="goto(event)"
href="/contact/group/${group.uuid}/"
icon=${Icon.group_smart}
type="group"
clickable
>
${group.name}
</temba-label>`
)}
</div>
</div>`;
}
private renderDisplayedUrn(
urn: URN | undefined,
className = ''
): TemplateResult {
const unsendable = urn?.channel === null;
return html`<temba-contact-field
class=${className}
name=${urn ? this.getSchemeName(urn.scheme) : 'URN'}
value=${urn ? urn.display || urn.path : 'No URNs'}
valueIcon=${unsendable ? Icon.contact_stopped : ''}
valueIconLabel=${unsendable ? 'Not sendable: no channel available' : ''}
disabled
></temba-contact-field>`;
}
private renderUrnSendability(urn: URN): TemplateResult {
if (urn.channel !== null) return null;
const label = 'Not sendable: no channel available';
return html`<temba-tip
class="urn-sendability unsendable"
text=${label}
position="top"
slot="prefix"
>
<temba-icon name=${Icon.contact_stopped} aria-label=${label}></temba-icon>
</temba-tip>`;
}
private renderUrnDialog(): TemplateResult {
const selectedScheme = this.newScheme || this.schemes[0]?.value || '';
return html`<temba-dialog
header="Edit URNs"
size="medium"
primaryButtonName="Save"
cancelButtonName="Cancel"
.open=${this.urnDialogOpen}
?disabled=${this.saving.has('urn-dialog')}
?submitting=${this.saving.has('urn-dialog')}
@temba-button-clicked=${this.handleUrnDialogButton}
>
<div class="urn-dialog">
<temba-sortable-list
class="urn-list"
dragHandle="drag-handle"
gap="12px"
.prepareGhost=${this.prepareUrnGhost}
@temba-order-changed=${this.handleUrnOrderChanged}
>
${this.draftUrns.map((urn, index) => {
return html`
<div
id="urn-${index}"
class="urn-row sortable ${this.draftUrns.length > 1
? ''
: 'single'}"
>
<div class="drag-slot">
${this.draftUrns.length > 1
? html`<temba-icon
class="drag-handle"
name=${Icon.drag}
title="Drag to reorder"
aria-label="Reorder ${this.getSchemeName(
urn.scheme
)} URN. Use the up and down arrow keys to move it."
role="button"
tabindex="0"
@keydown=${(event: KeyboardEvent) =>
this.handleUrnOrderKeyDown(event, index)}
></temba-icon>`
: null}
</div>
<temba-textinput
class="urn-input"
name="urn-${index}"
label=${this.getSchemeName(urn.scheme)}
.value=${urn.path}
@input=${(event: Event) =>
this.handleDraftUrnChanged(event, index)}
@change=${(event: Event) =>
this.handleDraftUrnChanged(event, index)}
>
${this.renderUrnSendability(urn)}
<temba-tip
class="urn-delete-action"
text="Delete URN"
position="top"
>
<temba-icon
class="urn-delete-icon"
name=${Icon.delete}
aria-label="Delete URN"
role="button"
tabindex="0"
@click=${(event: Event) =>
this.handleDeleteUrn(event, index)}
@keydown=${(event: KeyboardEvent) =>
this.handleDeleteUrnKeyDown(event, index)}
></temba-icon>
</temba-tip>
</temba-textinput>
</div>
`;
})}
</temba-sortable-list>
${this.failed.has('urn-dialog')
? html`<div class="add-urn-error">
Failed to save URNs. Check the addresses and try again.
</div>`
: null}
${this.schemes.length
? html`<div class="add-urn-fields">
<div>
<label>URN Type</label>
<temba-select
valueKey="value"
.values=${this.schemes.filter(
(option) => option.value === selectedScheme
)}
@change=${this.handleNewSchemeChanged}
>
${this.schemes.map(
(option) =>
html`<temba-option
name=${option.name}
value=${option.value}
></temba-option>`
)}
</temba-select>
</div>
<temba-textinput
name="urn"
label="Address"
.value=${this.newUrn}
@input=${this.handleNewUrnChanged}
></temba-textinput>
<div class="add-urn-button">
<temba-button
name="Add"
icon=${Icon.add}
light
?disabled=${!this.newUrn.trim()}
@click=${this.handleAddUrn}
></temba-button>
</div>
</div>`
: null}
</div>
</temba-dialog>`;
}
private renderPrimaryUrn(editable: boolean): TemplateResult {
if (this.anon || (!editable && this.data.urns.length === 0)) return null;
const additional = Math.max(0, this.data.urns.length - 1);
const displayedUrn = getDestinationURN(this.data) || this.data.urns[0];
const noUrns = this.data.urns.length === 0;
const onlyUnsendable =
this.data.urns.length === 1 && this.data.urns[0].channel === null;
return html`<div
class="urn-display primary-urn ${editable
? 'editable'
: ''} ${onlyUnsendable ? 'only-unsendable' : ''}"
aria-label=${noUrns
? 'This contact has no URNs'
: onlyUnsendable
? 'This contact has no sendable URNs'
: 'Contact URN'}
>
${this.renderDisplayedUrn(displayedUrn, 'primary-urn')}
${editable && additional
? html`<span
class="urn-more-count"
aria-label="${additional} additional URNs"
>+${additional} more</span
>`
: null}
${editable
? html`<temba-icon
class="urn-edit-icon"
name=${Icon.edit}
title="Edit URNs"
aria-label="Edit URNs"
role="button"
tabindex="0"
@click=${this.showUrnDialog}
@keydown=${this.handleUrnEditKeyDown}
></temba-icon>`
: null}
</div>
${editable ? this.renderUrnDialog() : null}`;
}
private renderEditable(): TemplateResult {
const manualGroups = this.getManualGroups();
return html`
${this.renderPrimaryUrn(true)}
<temba-contact-field
key="name"
name="Name"
value=${this.data.name || ''}
@change=${this.handleTextChanged}
@temba-button-clicked=${this.handleSearch}
></temba-contact-field>
${this.anon && this.data.ref
? html`<temba-contact-field
name="Ref"
value=${this.data.ref}
disabled
></temba-contact-field>`
: null}
${this.data.status === 'active'
? html`<div
class="editable-row ${this.saving.has('groups')
? 'saving'
: ''} ${this.failed.has('groups') ? 'failed' : ''}"
>
<label>Groups</label>
${this.renderFailure('groups')}
<temba-select
endpoint="/api/v2/groups.json?manual_only=1"
valueKey="uuid"
.values=${manualGroups}
@change=${this.handleGroupsChanged}
searchable
clearable
multi
></temba-select>
</div>`
: null}
${this.renderDynamicGroups(true)}
${this.renderEditableSelect(
'status',
'Status',
STATUS_OPTIONS,
this.data.status,
this.handleStatusChanged
)}
${this.renderEditableSelect(
'language',
'Language',
this.getLanguageOptions(),
this.data.language || 'none',
this.handleLanguageChanged
)}
`;
}
private renderReadonly(): TemplateResult {
const lang = getLanguageName(this.data.language);
const manualGroups = this.getManualGroups();
return html`
${this.renderPrimaryUrn(false)}
${manualGroups.length
? html`<div class="row">
<div class="label">Groups</div>
<div class="value">
${manualGroups.map(
(group) =>
html`<temba-label
onclick="goto(event)"
href="/contact/group/${group.uuid}/"
icon=${group.is_dynamic ? Icon.group_smart : Icon.group}
type="group"
clickable
>
${group.name}
</temba-label>`
)}
</div>
</div>`
: null}
${this.renderDynamicGroups(false)}
${this.data.ref
? html`<temba-contact-field
name="Ref"
value=${this.data.ref}
disabled
></temba-contact-field>`
: null}
<temba-contact-field
name="Status"
value=${STATUS_OPTIONS.find(
(option) => option.value === this.data.status
)?.name || capitalize(this.data.status)}
disabled
></temba-contact-field>
${lang
? html`<temba-contact-field
name="Language"
value=${lang}
disabled
></temba-contact-field>`
: null}
`;
}
public render(): TemplateResult {
if (!this.data) return super.render();
return html`<div class="wrapper">
${this.editable ? this.renderEditable() : this.renderReadonly()}
<temba-contact-field
name="Created"
value=${this.data.created_on}
type="datetime"
disabled
></temba-contact-field>
<temba-contact-field
class="last-field"
name="Last Seen"
value=${this.data.last_seen_on}
type="datetime"
disabled
></temba-contact-field>
</div>`;
}
}
|