318 lines
9.1 KiB
JavaScript
Raw Normal View History

2016-04-12 10:24:16 +12:00
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as breadcrumbsActions from 'state/breadcrumbs/BreadcrumbsActions';
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
import * as recordActions from 'state/records/RecordsActions';
import * as campaignActions from 'state/campaign/CampaignActions';
import SilverStripeComponent from 'lib/SilverStripeComponent';
import Accordion from 'components/Accordion/Accordion';
import AccordionBlock from 'components/Accordion/AccordionBlock';
import ListGroupItem from 'components/ListGroup/ListGroupItem';
import Toolbar from 'components/Toolbar/Toolbar';
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
import FormAction from 'components/FormAction/FormAction';
import CampaignAdminItem from './CampaignAdminItem';
import BreadcrumbComponent from 'components/Breadcrumb/Breadcrumb';
import Preview from 'components/Preview/Preview';
2016-04-12 09:15:04 +12:00
import i18n from 'i18n';
2016-04-12 10:24:16 +12:00
2016-04-12 10:24:16 +12:00
/**
* Represents a campaign list view
*/
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
class CampaignAdminList extends SilverStripeComponent {
2016-04-12 10:24:16 +12:00
2016-04-12 09:15:04 +12:00
constructor(props) {
super(props);
this.handlePublish = this.handlePublish.bind(this);
2016-04-27 14:26:39 +12:00
this.handleItemSelected = this.handleItemSelected.bind(this);
this.setBreadcrumbs = this.setBreadcrumbs.bind(this);
2016-04-12 09:15:04 +12:00
}
2016-04-12 10:24:16 +12:00
componentDidMount() {
const fetchURL = this.props.itemListViewEndpoint.replace(/:id/, this.props.campaignId);
super.componentDidMount();
this.setBreadcrumbs();
this.props.recordActions.fetchRecord('ChangeSet', 'get', fetchURL).then(this.setBreadcrumbs);
}
/**
* Update breadcrumbs for this view
*/
setBreadcrumbs() {
// Setup breadcrumbs if record is loaded
if (!this.props.record) {
return;
}
// Check that we haven't navigated away from this page once the callback has returned
const thisLink = this.props.sectionConfig.campaignViewRoute
.replace(/:type\?/, 'set')
.replace(/:id\?/, this.props.campaignId)
.replace(/:view\?/, 'show');
const applies = window.ss.router.routeAppliesToCurrentLocation(
window.ss.router.resolveURLToBase(thisLink)
);
if (!applies) {
return;
}
// Push breadcrumb
const breadcrumbs = this.props.baseBreadcrumbs.slice(0);
breadcrumbs.push({
text: this.props.record.Name,
href: thisLink,
});
this.props.breadcrumbsActions.setBreadcrumbs(breadcrumbs);
2016-04-12 10:24:16 +12:00
}
/**
* Renders a list of items in a Campaign.
*
* @return object
*/
render() {
2016-05-10 15:07:29 +12:00
let itemId = this.props.campaign.changeSetItemId;
let itemLinks = null;
2016-04-12 10:24:16 +12:00
const campaignId = this.props.campaignId;
const campaign = this.props.record;
2016-04-12 10:24:16 +12:00
// Trigger different layout when preview is enabled
const itemGroups = this.groupItemsForSet();
// Get items in this set
let accordionBlocks = [];
2016-04-12 10:24:16 +12:00
Object.keys(itemGroups).forEach(className => {
const group = itemGroups[className];
const groupCount = group.items.length;
let listGroupItems = [];
2016-04-12 10:24:16 +12:00
let title = `${groupCount} ${groupCount === 1 ? group.singular : group.plural}`;
let groupid = `Set_${campaignId}_Group_${className}`;
// Create items for this group
group.items.forEach(item => {
2016-04-27 14:26:39 +12:00
// Auto-select first item
2016-05-10 15:07:29 +12:00
if (!itemId) {
itemId = item.ID;
2016-04-27 14:26:39 +12:00
}
2016-05-10 15:07:29 +12:00
const selected = (itemId === item.ID);
// Check links
2016-05-10 15:07:29 +12:00
if (selected && item._links) {
itemLinks = item._links;
2016-04-27 14:26:39 +12:00
}
// Add extra css class for published items
const itemClassNames = [];
if (item.ChangeType === 'none' || campaign.State === 'published') {
itemClassNames.push('list-group-item--inactive');
2016-04-27 14:26:39 +12:00
}
if (selected) {
itemClassNames.push('active');
2016-04-12 10:24:16 +12:00
}
listGroupItems.push(
<ListGroupItem
key={item.ID}
className={itemClassNames.join(' ')}
handleClick={this.handleItemSelected}
handleClickArg={item.ID}
>
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
<CampaignAdminItem item={item} campaign={this.props.record} />
</ListGroupItem>
2016-04-12 10:24:16 +12:00
);
});
// Merge into group
accordionBlocks.push(
<AccordionBlock key={groupid} groupid={groupid} title={title}>
{listGroupItems}
</AccordionBlock>
2016-04-12 10:24:16 +12:00
);
});
2016-05-10 15:07:29 +12:00
// Set body
const pagesLink = this.props.config.sections.CMSMain.route;
const body = accordionBlocks.length
? (<Accordion>{accordionBlocks}</Accordion>)
: (
<div className="alert alert-warning" role="alert">
<strong>This campaign is empty.</strong> You can add pages by selecting{' '}
<em>Add to campaign</em> from within the <em>More Options</em> popup on{' '}
the <a href={pagesLink}>edit page screen</a>.
</div>
);
2016-04-27 14:26:39 +12:00
2016-04-12 10:24:16 +12:00
return (
2016-05-10 15:07:29 +12:00
<div className="cms-content__split cms-content__split--left-sm">
<div className="cms-content__left cms-campaigns collapse in" aria-expanded="true">
<Toolbar showBackButton handleBackButtonClick={this.props.handleBackButtonClick}>
<BreadcrumbComponent multiline crumbs={this.props.breadcrumbs} />
</Toolbar>
<div className="container-fluid campaign-items panel-scrollable--double-toolbar">
2016-05-10 15:07:29 +12:00
{body}
2016-04-12 10:24:16 +12:00
</div>
<div className="toolbar--south">
2016-04-12 09:15:04 +12:00
{this.renderButtonToolbar()}
</div>
2016-04-12 10:24:16 +12:00
</div>
2016-05-10 15:07:29 +12:00
<Preview itemLinks={itemLinks} itemId={itemId} />
2016-04-12 10:24:16 +12:00
</div>
);
}
2016-04-27 14:26:39 +12:00
/**
* Callback for items being clicked on
*
* @param {object} event
* @param {number} itemId
*/
handleItemSelected(event, itemId) {
this.props.campaignActions.selectChangeSetItem(itemId);
}
2016-04-12 09:15:04 +12:00
renderButtonToolbar() {
const items = this.getItems();
2016-04-12 09:15:04 +12:00
// let itemSummaryLabel;
2016-05-10 15:07:29 +12:00
if (!items || !items.length) {
return <div className="btn-toolbar"></div>;
2016-04-12 09:15:04 +12:00
}
// let itemSummaryLabel = i18n.sprintf(
// items.length === 1
// ? i18n._t('Campaigns.ITEM_SUMMARY_SINGULAR')
// : i18n._t('Campaigns.ITEM_SUMMARY_PLURAL'),
// items.length
// );
let actionProps = {};
if (this.props.record.State === 'open') {
actionProps = Object.assign(actionProps, {
label: i18n._t('Campaigns.PUBLISHCAMPAIGN'),
bootstrapButtonStyle: 'primary',
loading: this.props.campaign.isPublishing,
handleClick: this.handlePublish,
icon: 'rocket',
});
} else if (this.props.record.State === 'published') {
// TODO Implement "revert" feature
actionProps = Object.assign(actionProps, {
label: i18n._t('Campaigns.REVERTCAMPAIGN'),
bootstrapButtonStyle: 'default',
icon: 'back-in-time',
disabled: true,
});
}
// TODO Fix indicator positioning
// const itemCountIndicator = (
// <span className="text-muted">
// <span className="label label-warning label--empty">&nbsp;</span>
// &nbsp;{itemSummaryLabel}
// </span>
// );
return (
<div className="btn-toolbar">
<FormAction {...actionProps} />
</div>
);
2016-04-12 09:15:04 +12:00
}
2016-04-12 10:24:16 +12:00
2016-04-12 09:15:04 +12:00
/**
* @return {Array}
*/
getItems() {
if (this.props.record && this.props.record._embedded) {
return this.props.record._embedded.ChangeSetItems;
}
return null;
}
2016-04-12 10:24:16 +12:00
/**
* Group items for changeset display
*
* @return array
*/
groupItemsForSet() {
const groups = {};
2016-04-12 09:15:04 +12:00
const items = this.getItems();
if (!items) {
2016-04-12 10:24:16 +12:00
return groups;
}
// group by whatever
items.forEach(item => {
// Create new group if needed
const classname = item.BaseClass;
if (!groups[classname]) {
groups[classname] = {
singular: item.Singular,
plural: item.Plural,
items: [],
};
}
// Push items
groups[classname].items.push(item);
});
return groups;
}
2016-04-12 09:15:04 +12:00
handlePublish(e) {
e.preventDefault();
this.props.campaignActions.publishCampaign(
this.props.publishApi,
this.props.campaignId
);
}
2016-04-12 10:24:16 +12:00
}
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
CampaignAdminList.propTypes = {
campaign: React.PropTypes.shape({
isPublishing: React.PropTypes.bool.isRequired,
2016-04-27 14:26:39 +12:00
changeSetItemId: React.PropTypes.number,
}),
breadcrumbsActions: React.PropTypes.object.isRequired,
campaignActions: React.PropTypes.object.isRequired,
2016-04-12 09:15:04 +12:00
publishApi: React.PropTypes.func.isRequired,
record: React.PropTypes.object.isRequired,
recordActions: React.PropTypes.object.isRequired,
baseBreadcrumbs: React.PropTypes.array.isRequired,
sectionConfig: React.PropTypes.object.isRequired,
handleBackButtonClick: React.PropTypes.func,
2016-04-12 09:15:04 +12:00
};
2016-04-12 10:24:16 +12:00
function mapStateToProps(state, ownProps) {
// Find record specific to this item
let record = null;
if (state.records && state.records.ChangeSet && ownProps.campaignId) {
record = state.records.ChangeSet[parseInt(ownProps.campaignId, 10)];
2016-04-12 10:24:16 +12:00
}
return {
2016-05-10 15:07:29 +12:00
config: state.config,
record: record || {},
2016-04-13 10:12:42 +12:00
campaign: state.campaign,
breadcrumbs: state.breadcrumbs,
2016-04-12 10:24:16 +12:00
};
}
function mapDispatchToProps(dispatch) {
return {
breadcrumbsActions: bindActionCreators(breadcrumbsActions, dispatch),
2016-04-12 09:15:04 +12:00
recordActions: bindActionCreators(recordActions, dispatch),
campaignActions: bindActionCreators(campaignActions, dispatch),
2016-04-12 10:24:16 +12:00
};
}
Correct naming for JS and CSS files in client/ Removed some dist/js/*.js files since they're no longer built as individual files. This was a side effect of them living in the toplevel folder of admin/client/src/, which used to have all the legacy/*.js files in there (they do need to be built). Following AirBnB convention: https://github.com/airbnb/javascript#naming--filename-matches-export While it technically allows index.js files, we found them to be bad for dev and debugging in practice: Depending on the used IDE, editor tabs all look the same. Other views like Chrome Dev Tools with sourcemaps rely on path context, and are harder to auto-complete. There's no direct rules for CSS files, but same principles apply here. Also renamed the sections/ folder to containers/, which more clearly communicates the distinction between components/ (shouldn't contain state-dependant, smart components). Renamed state/ files to follow AirBnB naming conventions https://github.com/airbnb/javascript#naming--filename-matches-export https://github.com/airbnb/javascript#naming--camelCase-default-export https://github.com/airbnb/javascript#naming--PascalCase-singleton Leaving the folder name in state/<state-key> lowercase since that's also the key to reducers in the actual state object. References: http://engineering.kapost.com/2016/01/organizing-large-react-applications/ https://github.com/erikras/react-redux-universal-hot-example/tree/master/src https://github.com/RickWong/react-isomorphic-starterkit/tree/master/src https://github.com/react-toolbox/react-toolbox/issues/98 https://github.com/react-bootstrap/react-bootstrap/tree/master/src
2016-04-21 21:59:44 +12:00
export default connect(mapStateToProps, mapDispatchToProps)(CampaignAdminList);