diff --git a/packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/generator_action.rb b/packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/generator_action.rb index 0836c5bc5..626bfeae5 100644 --- a/packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/generator_action.rb +++ b/packages/forest_admin_agent/lib/forest_admin_agent/utils/schema/generator_action.rb @@ -35,7 +35,7 @@ def self.build_schema(collection, name) layout = form_elements[:layout] else fields = DEFAULT_FIELDS - layout = nil + layout = [] end schema = { @@ -57,9 +57,9 @@ def self.build_schema(collection, name) } } - return schema unless layout && !layout.empty? + return schema if layout.all? { |element| element.component == 'Input' } - # schema[:layout] = build_layout(layout) + schema[:layout] = build_layout(layout) schema end diff --git a/packages/forest_admin_agent/spec/lib/forest_admin_agent/utils/schema/generator_action_spec.rb b/packages/forest_admin_agent/spec/lib/forest_admin_agent/utils/schema/generator_action_spec.rb index 50f2f488b..013abac23 100644 --- a/packages/forest_admin_agent/spec/lib/forest_admin_agent/utils/schema/generator_action_spec.rb +++ b/packages/forest_admin_agent/spec/lib/forest_admin_agent/utils/schema/generator_action_spec.rb @@ -266,10 +266,10 @@ module Schema widgetEdit: nil } ], - # layout: [ - # { component: 'input', fieldId: 'label', type: 'Layout' }, - # { component: 'separator', type: 'Layout' } - # ], + layout: [ + { component: 'input', fieldId: 'label', type: 'Layout' }, + { component: 'separator', type: 'Layout' } + ], hooks: { load: false, change: ['changeHook'] } } ) @@ -320,10 +320,10 @@ module Schema widgetEdit: nil } ], - # layout: [ - # { component: 'input', fieldId: 'label', type: 'Layout' }, - # { component: 'htmlBlock', type: 'Layout', content: '

foo

' } - # ], + layout: [ + { component: 'input', fieldId: 'label_id', type: 'Layout' }, + { component: 'htmlBlock', type: 'Layout', content: '

foo

' } + ], hooks: { load: false, change: ['changeHook'] } } ) @@ -388,9 +388,16 @@ module Schema defaultValue: nil } ], - # layout: [ - # { component: 'row', type: 'Layout', fields: ['label'] } - # ], + layout: [ + { + component: 'row', + type: 'Layout', + fields: [ + { component: 'input', fieldId: 'label_id', type: 'Layout' }, + { component: 'input', fieldId: 'amount_id', type: 'Layout' } + ] + } + ], hooks: { load: false, change: ['changeHook'] } } ) @@ -462,15 +469,160 @@ module Schema defaultValue: nil } ], - # layout: [ - # { component: 'page', type: 'Layout', elements: ['htmlBlock', 'row'] } - # ], + layout: [ + { + component: 'page', + type: 'Layout', + nextButtonLabel: 'Next', + previousButtonLabel: 'Previous', + elements: [ + { + component: 'htmlBlock', + content: '

Charge the credit card of the customer

', + type: 'Layout' + }, + { + component: 'row', + type: 'Layout', + fields: [ + { component: 'input', fieldId: 'label', type: 'Layout' }, + { component: 'input', fieldId: 'amount', type: 'Layout' } + ] + } + ] + } + ], hooks: { load: false, change: ['changeHook'] } } ) end end + context 'with dynamic element' do + before do + @collection = collection_build( + schema: { + actions: { + 'Charge credit card' => BaseAction.new( + scope: Types::ActionScope::SINGLE, + form: [ + FormLayoutElement::PageElement.new( + if_condition: proc { true }, + elements: [] + ) + ] + ) + } + } + ) + end + + it 'generate default loading schema' do + schema = described_class.build_schema(@collection, 'Charge credit card') + + expect(schema).to eq( + { + id: 'collection-0-charge-credit-card', + name: 'Charge credit card', + submitButtonLabel: nil, + description: nil, + type: 'single', + baseUrl: nil, + endpoint: '/forest/_actions/collection/0/charge-credit-card', + httpMethod: 'POST', + redirect: nil, + download: false, + fields: [ + { + defaultValue: 'Form is loading', + description: '', + enums: nil, + field: 'Loading...', + hook: nil, + isReadOnly: true, + isRequired: false, + label: 'Loading...', + reference: nil, + type: 'String', + value: nil, + widgetEdit: nil + } + ], + hooks: { load: true, change: ['changeHook'] } + } + ) + end + end + + context 'with nested dynamic element in page' do + before do + @collection = collection_build( + schema: { + actions: { + 'Charge credit card' => BaseAction.new( + scope: Types::ActionScope::SINGLE, + form: [ + FormLayoutElement::PageElement.new( + elements: [ + { + type: 'Layout', + component: 'HtmlBlock', + content: '

Charge the credit card of the customer

' + }, + { + type: 'Layout', + component: 'Row', + fields: [ + { id: 'label', label: 'label', type: 'String', if_condition: proc { true } }, + { id: 'amount', label: 'amount', type: 'String' } + ] + } + ] + ) + ] + ) + } + } + ) + end + + it 'generate default loading schema' do + schema = described_class.build_schema(@collection, 'Charge credit card') + + expect(schema).to eq( + { + id: 'collection-0-charge-credit-card', + name: 'Charge credit card', + submitButtonLabel: nil, + description: nil, + type: 'single', + baseUrl: nil, + endpoint: '/forest/_actions/collection/0/charge-credit-card', + httpMethod: 'POST', + redirect: nil, + download: false, + fields: [ + { + defaultValue: 'Form is loading', + description: '', + enums: nil, + field: 'Loading...', + hook: nil, + isReadOnly: true, + isRequired: false, + label: 'Loading...', + reference: nil, + type: 'String', + value: nil, + widgetEdit: nil + } + ], + hooks: { load: true, change: ['changeHook'] } + } + ) + end + end + describe 'extract_fields_and_layout' do before do @collection = collection_build( diff --git a/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/base_action.rb b/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/base_action.rb index 6b056aea1..1417d01d6 100644 --- a/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/base_action.rb +++ b/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/base_action.rb @@ -18,7 +18,7 @@ def build_elements end def static_form? - return form&.all?(&:static?) && form&.none? { |field| field.type == 'Layout' } if form + return form&.all?(&:static?) if form true end diff --git a/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/form_layout_element.rb b/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/form_layout_element.rb index ab5b84ece..f2409f7b1 100644 --- a/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/form_layout_element.rb +++ b/packages/forest_admin_datasource_customizer/lib/forest_admin_datasource_customizer/decorators/action/form_layout_element.rb @@ -42,6 +42,10 @@ def initialize(options) @fields = instantiate_subfields(options[:fields] || []) end + def static? + super && fields&.all?(&:static?) + end + private def validate_fields_presence!(options) @@ -77,6 +81,10 @@ def initialize(options) @elements = instantiate_elements(options[:elements] || []) end + def static? + super && elements&.all?(&:static?) + end + private def validate_elements_presence!(options)