diff --git a/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js b/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js index c7c63611d..3c33c8949 100644 --- a/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js +++ b/admin/client/src/containers/CampaignAdmin/CampaignAdmin.js @@ -26,6 +26,7 @@ class CampaignAdmin extends SilverStripeComponent { }); this.campaignListCreateFn = this.campaignListCreateFn.bind(this); this.campaignEditCreateFn = this.campaignEditCreateFn.bind(this); + this.campaignCreationCreateFn = this.campaignCreationCreateFn.bind(this); } componentDidMount() { @@ -68,6 +69,9 @@ class CampaignAdmin extends SilverStripeComponent { case 'edit': view = this.renderDetailEditView(); break; + case 'create': + view = this.renderCreateView(); + break; default: view = this.renderIndexView(); } @@ -157,6 +161,53 @@ class CampaignAdmin extends SilverStripeComponent { ); } + /** + * Render the view for creating a new Campaign. + */ + renderCreateView() { + const baseSchemaUrl = this.props.sectionConfig.forms.CreateEditForm.schemaUrl; + const formBuilderProps = { + createFn: this.campaignCreationCreateFn, + formId: 'CreateEditForm', + schemaUrl: `${baseSchemaUrl}/ChangeSet`, + }; + + return ( +
+
+ +

Campaigns

+
+
+ +
+
+
+ ); + } + + /** + * Hook to allow customisation of components being constructed + * by the Campaign creation FormBuilder. + * + * @param {Object} Component - Component constructor. + * @param {Object} props - Props passed from FormBuilder. + * @return {Object} - Instanciated React component + */ + campaignCreationCreateFn(Component, props) { + if (props.name === 'action_save') { + const extendedProps = Object.assign({}, props, { + type: 'submit', + label: props.title, + icon: 'save', + }); + + return ; + } + + return ; + } + /** * Hook to allow customisation of components being constructed * by the Campaign list FormBuilder. @@ -254,7 +305,12 @@ class CampaignAdmin extends SilverStripeComponent { } addCampaign() { - // Add campaign + const path = this.props.sectionConfig.campaignViewRoute + .replace(/:type\?/, 'set') + .replace(/:id\?/, 0) + .replace(/:view\?/, 'create'); + + window.ss.router.show(path); } } diff --git a/admin/code/CampaignAdmin.php b/admin/code/CampaignAdmin.php index 921d275f7..0e3e952c2 100644 --- a/admin/code/CampaignAdmin.php +++ b/admin/code/CampaignAdmin.php @@ -12,6 +12,7 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { 'set', 'sets', 'schema', + 'CreateEditForm', 'DetailEditForm', 'readCampaigns', 'createCampaign', @@ -30,7 +31,6 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { private static $url_handlers = [ 'GET sets' => 'readCampaigns', 'POST set/$ID/publish' => 'publishCampaign', - 'POST set/$ID' => 'createCampaign', 'GET set/$ID/$Name' => 'readCampaign', 'DELETE set/$ID' => 'deleteCampaign', ]; @@ -62,7 +62,10 @@ class CampaignAdmin extends LeftAndMain implements PermissionProvider { ], 'DetailEditForm' => [ 'schemaUrl' => $this->Link('schema/DetailEditForm') - ] + ], + 'CreateEditForm' => [ + 'schemaUrl' => $this->Link('schema/CreateEditForm') + ], ], 'campaignViewRoute' => $this->Link() . ':type?/:id?/:view?', 'itemListViewEndpoint' => $this->Link() . 'set/:id/show', @@ -188,23 +191,6 @@ JSON; } } - /** - * REST endpoint to create a campaign. - * - * @param SS_HTTPRequest $request - * - * @return SS_HTTPResponse - */ - public function createCampaign(SS_HTTPRequest $request) { - $response = new SS_HTTPResponse(); - $response->addHeader('Content-Type', 'application/json'); - $response->setBody(Convert::raw2json(['campaign' => 'create'])); - - // TODO Implement permission check and data creation - - return $response; - } - /** * REST endpoint to get a list of campaigns. * @@ -489,6 +475,22 @@ JSON; return $form; } + /** + * @todo Use GridFieldDetailForm once it can handle structured data and form schemas + * + * @return Form + */ + public function getCreateEditForm() { + return Form::create( + $this, + 'CreateEditForm', + ChangeSet::singleton()->getCMSFields(), + FieldList::create( + FormAction::create('save', 'Save') + ) + ); + } + /** * Gets user-visible url to edit a specific {@see ChangeSet} *