-
Notifications
You must be signed in to change notification settings - Fork 821
/
controller.js
230 lines (202 loc) · 5.99 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import backend from 'silverstripe-backend';
import * as actions from 'state/campaign/actions';
import SilverStripeComponent from 'silverstripe-component';
import FormAction from 'components/form-action/index';
import i18n from 'i18n';
import NorthHeader from 'components/north-header/index';
import FormBuilder from 'components/form-builder/index';
import CampaignListContainer from './list';
class CampaignAdminContainer extends SilverStripeComponent {
constructor(props) {
super(props);
this.addCampaign = this.addCampaign.bind(this);
this.createFn = this.createFn.bind(this);
this.publishApi = backend.createEndpointFetcher({
url: this.props.sectionConfig.publishEndpoint.url,
method: this.props.sectionConfig.publishEndpoint.method,
defaultData: { SecurityID: this.props.config.SecurityID },
payloadSchema: {
id: { urlReplacement: ':id', remove: true },
},
});
}
componentDidMount() {
super.componentDidMount();
// While a component is mounted it will intercept all routes and handle internally
let captureRoute = true;
const route = window.ss.router.resolveURLToBase(this.props.sectionConfig.campaignViewRoute);
// Capture routing within this section
window.ss.router(route, (ctx, next) => {
if (captureRoute) {
// If this component is mounted, then handle all page changes via
// state / redux
this.props.actions.showCampaignView(ctx.params.id, ctx.params.view);
} else {
// If component is not mounted, we need to allow root routes to load
// this section in via ajax
next();
}
});
// When leaving this section to go to another top level section then
// disable route capturing.
window.ss.router.exit(route, (ctx, next) => {
const applies = window.ss.router.routeAppliesToCurrentLocation(route);
if (!applies) {
captureRoute = false;
}
next();
});
}
render() {
let view = null;
switch (this.props.view) {
case 'show':
view = this.renderItemListView();
break;
case 'edit':
view = this.renderDetailEditView();
break;
default:
view = this.renderIndexView();
}
return view;
}
/**
* Renders the default view which displays a list of Campaigns.
*
* @return object
*/
renderIndexView() {
const schemaUrl = this.props.sectionConfig.forms.editForm.schemaUrl;
return (
<div className="cms-middle no-preview">
<div className="cms-campaigns collapse in" aria-expanded="true">
<NorthHeader>
<h2 className="text-truncate north-header__heading">Campaigns</h2>
</NorthHeader>
<div className="cms-middle__scrollable">
<div className="content-toolbar">
<div className="btn-toolbar">
<FormAction
label={i18n._t('Campaigns.ADDCAMPAIGN')}
icon={'plus'}
handleClick={this.addCampaign}
/>
</div>
</div>
<FormBuilder schemaUrl={schemaUrl} createFn={this.createFn} />
</div>
</div>
</div>
);
}
/**
* Renders a list of items in a Campaign.
*
* @return object
*/
renderItemListView() {
const props = {
campaignId: this.props.campaignId,
itemListViewEndpoint: this.props.sectionConfig.itemListViewEndpoint,
publishApi: this.publishApi,
breadcrumbs: this.getBreadcrumbs(),
};
return (
<CampaignListContainer {...props} />
);
}
/**
* @todo
*/
renderDetailEditView() {
return <p>Edit</p>;
}
/**
* Hook to allow customisation of components being constructed by FormBuilder.
*
* @param object Component - Component constructor.
* @param object props - Props passed from FormBuilder.
*
* @return object - Instanciated React component
*/
createFn(Component, props) {
const campaignViewRoute = this.props.sectionConfig.campaignViewRoute;
if (props.component === 'GridField') {
const extendedProps = Object.assign({}, props, {
data: Object.assign({}, props.data, {
handleDrillDown: (event, record) => {
// Set url and set list
const path = campaignViewRoute
.replace(/:type\?/, 'set')
.replace(/:id\?/, record.ID)
.replace(/:view\?/, 'show');
window.ss.router.show(path);
},
}),
});
return <Component key={extendedProps.name} {...extendedProps} />;
}
return <Component key={props.name} {...props} />;
}
/**
* @todo Use dynamic breadcrumbs
*/
getBreadcrumbs() {
return [
{
text: 'Campaigns',
href: 'admin/campaigns',
},
{
text: 'March release',
href: 'admin/campaigns/show/1',
},
];
}
/**
* Gets preview URL for itemid
* @param int id
* @returns string
*/
previewURLForItem(id) {
if (!id) {
return '';
}
// hard code in baseurl for any itemid preview url
return document.getElementsByTagName('base')[0].href;
}
addCampaign() {
// Add campaign
}
}
CampaignAdminContainer.propTypes = {
sectionConfig: React.PropTypes.shape({
forms: React.PropTypes.shape({
editForm: React.PropTypes.shape({
schemaUrl: React.PropTypes.string,
}),
}),
}),
config: React.PropTypes.shape({
SecurityID: React.PropTypes.string,
}),
sectionConfigKey: React.PropTypes.string.isRequired,
};
function mapStateToProps(state, ownProps) {
return {
config: state.config,
sectionConfig: state.config.sections[ownProps.sectionConfigKey],
campaignId: state.campaign.campaignId,
view: state.campaign.view,
};
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(actions, dispatch),
};
}
export default connect(mapStateToProps, mapDispatchToProps)(CampaignAdminContainer);