SearchKit - Refresh fields after saving a SearchSegment
authorColeman Watts <coleman@civicrm.org>
Sun, 17 Apr 2022 18:58:27 +0000 (14:58 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 18 Apr 2022 18:34:14 +0000 (14:34 -0400)
ext/search_kit/ang/crmSearchAdmin/searchSegment/crmSearchAdminSegment.component.js
ext/search_kit/ang/crmSearchAdmin/searchSegmentListing/crmSearchAdminSegmentListing.component.js
ext/search_kit/tests/phpunit/api/v4/SearchSegment/SearchSegmentTest.php

index c98e16b9fcee4e6b28cbd02df3ffffe6202f8c50..18c63adbca60897278b98b5be1cdfc9d92984d4b 100644 (file)
@@ -8,7 +8,9 @@
     templateUrl: '~/crmSearchAdmin/searchSegment/crmSearchAdminSegment.html',
     controller: function ($scope, searchMeta, dialogService, crmApi4, crmStatus, formatForSelect2) {
       var ts = $scope.ts = CRM.ts('org.civicrm.search_kit'),
-        ctrl = this;
+        ctrl = this,
+        originalEntity,
+        originalField;
 
       this.entitySelect = searchMeta.getPrimaryAndSecondaryEntitySelect();
 
@@ -37,6 +39,8 @@
             where: [['id', '=', ctrl.segmentId]]
           }, 0).then(function(segment) {
             ctrl.segment = segment;
+            originalEntity = segment.entity_name;
+            originalField = 'segment_' + segment.name;
             searchMeta.loadFieldOptions([segment.entity_name]);
             $('.ui-dialog:visible').unblock();
           });
 
       this.save = function() {
         crmStatus({}, crmApi4('SearchSegment', 'save', {
-          records: [ctrl.segment]
-        })).then(function() {
+          records: [ctrl.segment],
+          chain: {
+            fields: [ctrl.segment.entity_name, 'getFields', {
+              loadOptions: ['id', 'name', 'label', 'description', 'color', 'icon'],
+              where: [['type', '=', 'Extra'], ['name', 'LIKE', 'segment_%']]
+            }]
+          }
+        }, 0)).then(function(saved) {
+          // If entity changed, remove field from orignal entity
+          if (originalEntity) {
+            _.remove(searchMeta.getEntity(originalEntity).fields, {name: originalField});
+          }
+          // Refresh all segment fields in this entity
+          var entity = searchMeta.getEntity(ctrl.segment.entity_name);
+          _.remove(entity.fields, function(field) {
+            return field.name.indexOf('segment_') === 0;
+          });
+          _.each(saved.fields, function(field) {
+            field.fieldName = field.name;
+            entity.fields.push(field);
+          });
+          entity.fields = _.sortBy(entity.fields, 'label');
           dialogService.close('searchSegmentDialog');
         });
       };
index 828c4b97683b047f087c390990f2dd528af9b710..16ad1dc19d6c277a0150a019363b17c117bc7eeb 100644 (file)
         api_params: {
           version: 4,
           select: [
+            'id',
             'label',
             'description',
+            'entity_name',
             'entity_name:label',
+            'CONCAT("segment_", name) AS field_name',
             'items'
           ],
           join: [],
@@ -41,6 +44,9 @@
           {start: ts('Deleting...'), success: ts('Segment Deleted')},
           row
         );
+        // Delete field from metadata
+        var entity = searchMeta.getEntity(row.data.entity_name);
+        _.remove(entity.fields, {name: row.data.field_name});
       };
 
       function buildDisplaySettings() {
             actions: false,
             classes: ['table', 'table-striped'],
             sort: [['label', 'ASC']],
+            // Do not make columns editable because it would require a metadata refresh
             columns: [
               {
                 key: 'label',
                 label: ts('Label'),
-                title: ts('Edit Label'),
-                type: 'field',
-                editable: true
+                type: 'field'
               },
               {
                 key: 'description',
                 label: ts('Description'),
-                type: 'field',
-                editable: true
+                type: 'field'
               },
               {
                 key: 'entity_name:label',
index 01a815ac601f6ac19efbec762864c6ca4e217cf3..12cbbab8d56fe7ee19c7b34251d6269f7cd150be 100644 (file)
@@ -84,6 +84,7 @@ class SearchSegmentTest extends \PHPUnit\Framework\TestCase implements HeadlessI
       ->execute()->single();
 
     $this->assertEquals('Giving Tier', $getField['label']);
+    $this->assertEquals('Extra', $getField['type']);
     $this->assertEquals(['Low ball', 'Minor league', 'Major league', 'Heavy hitter'], $getField['options']);
 
     $params = [