Skip to content

Commit e10d6d3

Browse files
authored
Merge pull request #2119 from tf/filter-scrolled-editor-widgets
Do not sync non-react widgets to scrolled state in editor
2 parents 2258013 + 4c3520e commit e10d6d3

File tree

9 files changed

+99
-6
lines changed

9 files changed

+99
-6
lines changed

app/helpers/pageflow/widgets_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def widget_types_json_seeds(config)
3232
result[role] ||= []
3333
result[role] << {
3434
name: widget_type.name,
35-
translationKey: widget_type.translation_key
35+
translationKey: widget_type.translation_key,
36+
insertPoint: widget_type.insert_point
3637
}
3738
end
3839
end.to_json.html_safe

entry_types/scrolled/package/spec/entryState/widgets-spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ describe('useWidget', () => {
1414
setup: dispatch =>
1515
watchCollections(
1616
factories.entry(ScrolledEntry, {}, {
17+
widgetTypes: factories.widgetTypes([{
18+
role: 'navigation', name: 'customNavigation', insertPoint: 'react'
19+
}]),
1720
widgetsAttributes: [{
1821
type_name: 'customNavigation',
1922
role: 'navigation',
@@ -33,6 +36,28 @@ describe('useWidget', () => {
3336
});
3437
});
3538

39+
it('filters out non react widgets in editor', () => {
40+
const {result} = renderHookInEntry(() => useWidget({role: 'consent'}), {
41+
setup: dispatch =>
42+
watchCollections(
43+
factories.entry(ScrolledEntry, {}, {
44+
widgetTypes: factories.widgetTypes([{
45+
role: 'consent', name: 'some_consent_provider', insertPoint: 'bottom_of_entry'
46+
}]),
47+
widgetsAttributes: [{
48+
type_name: 'some_consent_provider',
49+
role: 'consent'
50+
}],
51+
entryTypeSeed: normalizeSeed()
52+
}),
53+
{dispatch}
54+
)
55+
});
56+
const widget = result.current;
57+
58+
expect(widget).toBeUndefined();
59+
});
60+
3661
it('reads data from seed', () => {
3762
const {result} = renderHookInEntry(
3863
() => useWidget({role: 'navigation'}),

entry_types/scrolled/package/src/entryState/watchCollections.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function watchCollections(entry, {dispatch}) {
3939
dispatch
4040
}));
4141

42-
teardownFns.push(watchCollection(widgets, {
42+
teardownFns.push(watchCollection(widgets.withInsertPoint('react'), {
4343
name: 'widgets',
4444
attributes: [{typeName: 'type_name'}, 'role', {permaId: 'role'}],
4545
keyAttribute: 'permaId',

package/spec/editor/api/WidgetTypes-spec.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ describe('WidgetTypes', () => {
4242
navigation: [
4343
{
4444
name: 'fancy_bar',
45-
translationKey: 'pageflow.fancy_bar.widget_type_name'
45+
translationKey: 'pageflow.fancy_bar.widget_type_name',
46+
insertPoint: 'bottom_of_entry'
4647
}
4748
]
4849
});
4950

5051
var widgetType = widgetTypes.findByName('fancy_bar');
5152

5253
expect(widgetType.name).toBe('fancy_bar');
54+
expect(widgetType.insertPoint).toBe('bottom_of_entry');
5355
});
5456
});
5557

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {WidgetsCollection} from 'editor/collections/WidgetsCollection';
2+
import {factories} from '$support';
3+
4+
describe('WidgetsCollection', () => {
5+
it('supports gettting subset collection for insert point', () => {
6+
const widgetTypes = factories.widgetTypes([
7+
{role: 'navigation', name: 'some_navigation_bar', insertPoint: 'react'},
8+
{role: 'consent', name: 'some_consent_provider', insertPoint: 'bottom_of_entry'}
9+
]);
10+
const widgets = new WidgetsCollection([
11+
{type_name: 'some_navigation_bar'},
12+
{type_name: 'some_consent_provider'},
13+
], {widgetTypes});
14+
15+
expect(
16+
widgets.withInsertPoint('react').pluck('type_name')
17+
).toEqual(['some_navigation_bar']);
18+
});
19+
20+
it('keeps insert point subset collection up to date when type name changes', () => {
21+
const widgetTypes = factories.widgetTypes([
22+
{role: 'consent', name: 'consent_bar', insertPoint: 'react'},
23+
{role: 'consent', name: 'some_consent_provider', insertPoint: 'bottom_of_entry'}
24+
]);
25+
const widgets = new WidgetsCollection([
26+
{type_name: 'some_consent_provider'},
27+
], {widgetTypes});
28+
widgets.subject = factories.entry();
29+
30+
const subsetCollection = widgets.withInsertPoint('react');
31+
widgets.first().set('type_name', 'consent_bar');
32+
33+
expect(subsetCollection.pluck('type_name')).toEqual(['consent_bar']);
34+
});
35+
});

package/src/editor/api/WidgetType.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const WidgetType = Object.extend({
66
initialize: function(serverSideConfig, clientSideConfig) {
77
this.name = serverSideConfig.name;
88
this.translationKey = serverSideConfig.translationKey;
9+
this.insertPoint = serverSideConfig.insertPoint;
910
this.configurationEditorView = clientSideConfig.configurationEditorView;
1011
this.isOptional = clientSideConfig.isOptional;
1112
},

package/src/editor/collections/WidgetsCollection.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Backbone from 'backbone';
22
import _ from 'underscore';
33

44
import {Widget} from '../models/Widget';
5+
import {SubsetCollection} from './SubsetCollection';
56

67
export const WidgetsCollection = Backbone.Collection.extend({
78
model: Widget,
@@ -32,5 +33,13 @@ export const WidgetsCollection = Backbone.Collection.extend({
3233
subject.trigger('sync:widgets', subject, response, {});
3334
}
3435
}));
36+
},
37+
38+
withInsertPoint(insertPoint) {
39+
return new SubsetCollection({
40+
parent: this,
41+
watchAttribute: 'type_name',
42+
filter: widget => widget.widgetType().insertPoint === insertPoint
43+
});
3544
}
3645
});

package/src/testHelpers/factories.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ function ensureFilesCollections(options) {
351351

352352
function ensureWidgetsCollections(options) {
353353
if (!options.widgets) {
354-
options.widgets = new WidgetsCollection(options.widgetsAttributes);
354+
options.widgets = new WidgetsCollection(options.widgetsAttributes,
355+
{widgetTypes: options.widgetTypes});
355356
}
356357
}

spec/helpers/pageflow/widgets_helper_spec.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,29 @@ module Pageflow
213213
end
214214
end
215215

216+
describe '#widget_types_json_seeds' do
217+
it 'renders name, translationKey and insertPoint by role' do
218+
widget_type = TestWidgetType.new(name: 'fancy_bar',
219+
roles: ['navigation'],
220+
insert_point: :react)
221+
pageflow_configure do |config|
222+
config.widget_types.clear
223+
config.widget_types.register(widget_type)
224+
end
225+
226+
result = JSON.parse(helper.widget_types_json_seeds(Pageflow.config))
227+
228+
expect(result['navigation'][0]['name']).to eq('fancy_bar')
229+
expect(result['navigation'][0]['translationKey'])
230+
.to eq('pageflow.fancy_bar.widget_type_name')
231+
expect(result['navigation'][0]['insertPoint']).to eq('react')
232+
end
233+
end
234+
216235
describe '#widgets_json_seeds' do
217236
it 'includes role as id, type_name, configuration' do
218237
entry = DraftEntry.new(create(:entry))
219-
widget_type = TestWidgetType.new(name: 'fancy_bar', roles: 'navigation')
238+
widget_type = TestWidgetType.new(name: 'fancy_bar', roles: ['navigation'])
220239
create(:widget,
221240
subject: entry.draft,
222241
type_name: 'fancy_bar',
@@ -237,7 +256,7 @@ module Pageflow
237256

238257
it 'includes placeholders for roles without width' do
239258
entry = DraftEntry.new(create(:entry))
240-
widget_type = TestWidgetType.new(name: 'test', roles: 'foo')
259+
widget_type = TestWidgetType.new(name: 'test', roles: ['foo'])
241260

242261
pageflow_configure do |config|
243262
config.widget_types.clear

0 commit comments

Comments
 (0)