2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-11-09 19:28:43 +00:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { me } from '../initial_state'
|
|
|
|
import { getPromotions } from '../selectors'
|
2020-08-12 23:52:46 +01:00
|
|
|
import Bundle from '../features/ui/util/bundle'
|
2020-10-06 14:42:53 +01:00
|
|
|
import WrappedBundle from '../features/ui/util/wrapped_bundle'
|
2020-08-12 23:52:46 +01:00
|
|
|
import {
|
|
|
|
StatusPromotionPanel
|
|
|
|
} from '../features/ui/util/async_components'
|
2020-07-16 05:00:44 +01:00
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
class SidebarPanelGroup extends React.PureComponent {
|
2020-07-16 05:00:44 +01:00
|
|
|
|
|
|
|
render() {
|
2020-11-09 19:28:43 +00:00
|
|
|
const {
|
|
|
|
layout,
|
|
|
|
page,
|
|
|
|
promotions,
|
|
|
|
} = this.props
|
2020-07-16 05:00:44 +01:00
|
|
|
|
2020-11-09 20:31:19 +00:00
|
|
|
if (Array.isArray(promotions) && Array.isArray(layout) && !!me) {
|
2020-07-16 05:00:44 +01:00
|
|
|
const sidebarPromotionPageId = `${page}.sidebar`
|
2020-11-09 20:31:19 +00:00
|
|
|
const promotion = promotions.find((promotion) => promotion.timeline_id === sidebarPromotionPageId)
|
2020-07-16 05:00:44 +01:00
|
|
|
|
|
|
|
if (!!promotion) {
|
2020-11-09 20:31:19 +00:00
|
|
|
const correctedPosition = promotion.position - 1 > layout.length ? layout.length - 1 : promotion.position
|
2020-07-17 21:26:05 +01:00
|
|
|
if (!layout.find(p => p.key === 'status-promotion-panel')) {
|
2020-11-09 20:31:19 +00:00
|
|
|
layout.splice(correctedPosition, 0, <WrappedBundle key='status-promotion-panel' component={StatusPromotionPanel} componentParams={{ statusId: promotion.status_id }} />)
|
2020-07-17 21:26:05 +01:00
|
|
|
}
|
2020-07-16 05:00:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 19:11:25 +00:00
|
|
|
if (!Array.isArray(layout)) return null
|
|
|
|
|
2020-07-16 05:00:44 +01:00
|
|
|
return (
|
2020-08-17 21:07:16 +01:00
|
|
|
<React.Fragment>
|
2020-08-12 23:52:46 +01:00
|
|
|
{
|
2020-12-09 20:02:43 +00:00
|
|
|
layout.map((panel, i) => {
|
2020-08-19 17:14:26 +01:00
|
|
|
if (!panel) return null
|
|
|
|
|
2020-10-06 14:42:53 +01:00
|
|
|
if (typeof panel !== 'function' || panel.key === 'status-promotion-panel') {
|
2020-08-12 23:52:46 +01:00
|
|
|
return panel
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Bundle
|
2020-12-09 20:02:43 +00:00
|
|
|
key={`sidebar-panel-group-item-${i}`}
|
2020-08-12 23:52:46 +01:00
|
|
|
fetchComponent={panel}
|
|
|
|
loading={this.renderLoading}
|
|
|
|
error={this.renderError}
|
|
|
|
renderDelay={150}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
(Component) => <Component />
|
|
|
|
}
|
|
|
|
</Bundle>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2020-08-17 21:07:16 +01:00
|
|
|
</React.Fragment>
|
2020-07-16 05:00:44 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:28:43 +00:00
|
|
|
const mapStateToProps = (state) => ({
|
|
|
|
promotions: getPromotions()(state)
|
|
|
|
})
|
|
|
|
|
2020-08-18 01:57:35 +01:00
|
|
|
SidebarPanelGroup.propTypes = {
|
|
|
|
layout: PropTypes.array.isRequired,
|
|
|
|
page: PropTypes.string.isRequired,
|
|
|
|
promotion: PropTypes.object,
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:28:43 +00:00
|
|
|
export default connect(mapStateToProps)(SidebarPanelGroup)
|