From 5e5ac9939c8f5cbcfefd2694ab5f8da3d163c890 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 17 Apr 2022 14:58:27 -0400 Subject: [PATCH] SearchKit - Refresh fields after saving a SearchSegment --- .../crmSearchAdminSegment.component.js | 30 +++++++++++++++++-- .../crmSearchAdminSegmentListing.component.js | 14 +++++---- .../v4/SearchSegment/SearchSegmentTest.php | 1 + 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/ext/search_kit/ang/crmSearchAdmin/searchSegment/crmSearchAdminSegment.component.js b/ext/search_kit/ang/crmSearchAdmin/searchSegment/crmSearchAdminSegment.component.js index c98e16b9fc..18c63adbca 100644 --- a/ext/search_kit/ang/crmSearchAdmin/searchSegment/crmSearchAdminSegment.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/searchSegment/crmSearchAdminSegment.component.js @@ -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(); }); @@ -88,8 +92,28 @@ 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'); }); }; diff --git a/ext/search_kit/ang/crmSearchAdmin/searchSegmentListing/crmSearchAdminSegmentListing.component.js b/ext/search_kit/ang/crmSearchAdmin/searchSegmentListing/crmSearchAdminSegmentListing.component.js index 828c4b9768..16ad1dc19d 100644 --- a/ext/search_kit/ang/crmSearchAdmin/searchSegmentListing/crmSearchAdminSegmentListing.component.js +++ b/ext/search_kit/ang/crmSearchAdmin/searchSegmentListing/crmSearchAdminSegmentListing.component.js @@ -19,9 +19,12 @@ 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() { @@ -52,19 +58,17 @@ 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', diff --git a/ext/search_kit/tests/phpunit/api/v4/SearchSegment/SearchSegmentTest.php b/ext/search_kit/tests/phpunit/api/v4/SearchSegment/SearchSegmentTest.php index 01a815ac60..12cbbab8d5 100644 --- a/ext/search_kit/tests/phpunit/api/v4/SearchSegment/SearchSegmentTest.php +++ b/ext/search_kit/tests/phpunit/api/v4/SearchSegment/SearchSegmentTest.php @@ -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 = [ -- 2.25.1