Skip to content

Commit

Permalink
ENH Allow URLs without trailing slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Jan 13, 2023
1 parent 708e384 commit 1898a3e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions client/src/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import reactRouteRegister from 'lib/ReactRouteRegister';
import CampaignAdmin from 'containers/CampaignAdmin/CampaignAdmin';
import CampaignReducer from 'state/campaign/CampaignReducer';
import applyConditionals from 'boot/applyConditionals';
import { joinLinks } from 'lib/urls';

document.addEventListener('DOMContentLoaded', () => {
const baseURL = Config.getSection('SilverStripe\\CampaignAdmin\\CampaignAdmin').reactRoutePath;
reactRouteRegister.add({
path: '/',
routes: [
{ path: `/${baseURL}/set/:id/:view`, component: CampaignAdmin },
{ path: `/${baseURL}/:type/:id/:view`, component: CampaignAdmin },
{ path: `/${baseURL}`, component: CampaignAdmin },
{ path: joinLinks(baseURL, 'set/:id/:view'), component: CampaignAdmin },
{ path: joinLinks(baseURL, ':type/:id/:view'), component: CampaignAdmin },
{ path: baseURL, component: CampaignAdmin },
],
});

Expand Down
17 changes: 9 additions & 8 deletions client/src/containers/CampaignAdmin/CampaignAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ResizeAware from 'components/ResizeAware/ResizeAware';
import withRouter, { routerPropTypes } from 'lib/withRouter';
import * as viewModeActions from 'state/viewMode/ViewModeActions';
import CampaignAdminList from './CampaignAdminList';
import { joinLinks } from 'lib/urls';

const sectionConfigKey = 'SilverStripe\\CampaignAdmin\\CampaignAdmin';

Expand Down Expand Up @@ -79,12 +80,12 @@ class CampaignAdmin extends Component {
}

setBreadcrumbs(view, id, title) {
const { sectionConfig: { url } } = this.props;
const { sectionConfig: { reactRoutePath } } = this.props;

// Set root breadcrumb
const breadcrumbs = [{
text: i18n._t('CampaignAdmin.CAMPAIGN', 'Campaigns'),
href: url,
href: `/${reactRoutePath}`,
}];
switch (view) {
case 'show':
Expand Down Expand Up @@ -119,7 +120,8 @@ class CampaignAdmin extends Component {
* @return {string}
*/
getActionRoute(id, view) {
return `/${this.props.sectionConfig.reactRoutePath}/set/${id}/${view}`;
const { reactRoutePath } = this.props.sectionConfig;
return joinLinks(`/${reactRoutePath}`, `/set/${id}/${view}`);
}

handleBackButtonClick(event) {
Expand Down Expand Up @@ -154,10 +156,9 @@ class CampaignAdmin extends Component {
const hasErrors = this.hasErrors(response);
if (action === 'action_save' && !hasErrors) {
// open the new campaign in edit mode after save completes
const sectionUrl = this.props.sectionConfig.reactRoutePath;
const id = response.record.id;
this.props.campaignActions.setNewItem(id);
this.props.router.navigate(`/${sectionUrl}/set/${id}/show`);
this.props.router.navigate(this.getActionRoute(id, 'show'));
}

return response;
Expand Down Expand Up @@ -326,10 +327,10 @@ By removing this item all linked items will be removed unless used elsewhere.`;
data: {
...props.data,
onDrillDown: (event, record) => {
navigate(`/${reactRoutePath}/${typeUrlParam}/${record.ID}/show`);
navigate(joinLinks(`/${reactRoutePath}`, `${typeUrlParam}/${record.ID}/show`));
},
onEditRecord: (event, id) => {
navigate(`/${reactRoutePath}/${typeUrlParam}/${id}/edit`);
navigate(joinLinks(`/${reactRoutePath}`, `${typeUrlParam}/${id}/edit`));
},
},
};
Expand All @@ -355,7 +356,7 @@ By removing this item all linked items will be removed unless used elsewhere.`;
return this.renderCreateView();
}
const baseSchemaUrl = this.props.sectionConfig.form.campaignEditForm.schemaUrl;
const schemaUrl = `${baseSchemaUrl}/${this.props.router.params.id}`;
const schemaUrl = joinLinks(baseSchemaUrl, `/${this.props.router.params.id}`);

return (
<div className="fill-height">
Expand Down
7 changes: 4 additions & 3 deletions client/src/containers/CampaignAdmin/CampaignAdminList.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DropdownItem } from 'reactstrap';
import i18n from 'i18n';
import { inject } from 'lib/Injector';
import classNames from 'classnames';
import { joinLinks } from 'lib/urls';

/**
* Represents a campaign list view
Expand Down Expand Up @@ -80,7 +81,7 @@ class CampaignAdminList extends Component {
* Update breadcrumbs for this view
*/
setBreadcrumbs() {
const { breadcrumbsActions: actions, campaignId, record, sectionConfig: { url } } = this.props;
const { breadcrumbsActions: actions, campaignId, record, sectionConfig: { reactRoutePath } } = this.props;

// Setup breadcrumbs if record is loaded
if (!record) {
Expand All @@ -90,11 +91,11 @@ class CampaignAdminList extends Component {
// Push breadcrumb
const breadcrumbs = [{
text: i18n._t('CampaignAdmin.CAMPAIGN', 'Campaigns'),
href: url,
href: `/${reactRoutePath}`,
}];
breadcrumbs.push({
text: record.Name,
href: `${url}/set/${campaignId}/show`,
href: joinLinks(`/${reactRoutePath}`, `set/${campaignId}/show`),
});

actions.setBreadcrumbs(breadcrumbs);
Expand Down
8 changes: 4 additions & 4 deletions src/CampaignAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,19 @@ public function getClientConfig()
],
],
'readCampaignsEndpoint' => [
'url' => $this->Link() . 'sets',
'url' => $this->Link('sets'),
'method' => 'get'
],
'itemListViewEndpoint' => [
'url' => $this->Link() . 'set/:id/show',
'url' => $this->Link('set/:id/show'),
'method' => 'get'
],
'publishEndpoint' => [
'url' => $this->Link() . 'set/:id/publish',
'url' => $this->Link('set/:id/publish'),
'method' => 'post'
],
'removeCampaignItemEndpoint' => [
'url' => $this->Link() . 'removeCampaignItem/:id/:itemId',
'url' => $this->Link('removeCampaignItem/:id/:itemId'),
'method' => 'post'
],
'treeClass' => $this->config()->get('tree_class')
Expand Down

0 comments on commit 1898a3e

Please sign in to comment.