Reorganize component responsibility

pull/2859/head
Adam Rutkowski 4 years ago
parent 0ab38e7d57
commit 55cc7df8bc

@ -22,7 +22,7 @@ export default class Funnel extends React.Component {
componentDidUpdate(prevProps) {
const queryChanged = this.props.query !== prevProps.query
const funnelChanged = this.props.funnel !== prevProps.funnel
const funnelChanged = this.props.funnelName !== prevProps.funnelName
if (queryChanged || funnelChanged) {
this.setState({ loading: true, error: null })
@ -41,8 +41,9 @@ export default class Funnel extends React.Component {
}
fetchFunnel() {
if (this.props.funnel) {
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${this.props.funnel.id}`, this.props.query)
if (this.props.funnelName) {
const funnel = this.props.site.funnels.find(funnel => funnel.name === this.props.funnelName)
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/funnels/${funnel.id}`, this.props.query)
.then((res) => {
this.setState({ loading: false, funnel: res, error: null })
})
@ -129,10 +130,10 @@ export default class Funnel extends React.Component {
this.chart = new Chart(ctx, config);
}
tabs() {
header() {
return (
<div className="flex justify-between w-full">
<h3 className="font-bold dark:text-gray-100">Funnels</h3>
<h3 className="font-bold dark:text-gray-100">Funnel: {this.props.funnelName}</h3>
{this.props.tabs}
</div>
)
@ -142,15 +143,15 @@ export default class Funnel extends React.Component {
if (this.state.loading) {
return <div className="mx-auto loading pt-44"><div></div></div>
} else if (this.state.error) {
if (this.state.error && this.state.error.payload.level === "normal") {
if (this.state.error.payload.level === "normal") {
return (<React.Fragment>
{this.tabs()}
{this.header()}
<div className="font-medium text-center text-gray-500 mt-44 dark:text-gray-400">{this.state.error.message}</div>
</React.Fragment>)
} else {
return (
<React.Fragment>
{this.tabs()}
{this.header()}
<div className="text-center text-gray-900 dark:text-gray-100 mt-16">
<RocketIcon />
<div className="text-lg font-bold">Oops! Something went wrong</div>
@ -167,7 +168,7 @@ export default class Funnel extends React.Component {
return (
<React.Fragment>
{this.tabs()}
{this.header()}
<p className="mt-1 text-gray-500 text-sm">{this.state.funnel.steps.length}-step funnel • {conversionRate}% conversion rate</p>
<canvas className="py-4" id="funnel" height="100px"></canvas>
</React.Fragment>

@ -45,10 +45,6 @@ export default class Behaviours extends React.Component {
}
}
selectedFunnel() {
return this.props.site.funnels.find(funnel => funnel.name === this.state.selectedFunnelName)
}
hasFunnels() {
const site = this.props.site
return site.flags.funnels && site.funnels.length > 0
@ -74,19 +70,19 @@ export default class Behaviours extends React.Component {
>
<Menu.Items className="text-left origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none z-10">
<div className="py-1">
{this.state.funnelNames.map((option) => {
{this.state.funnelNames.map((funnelName) => {
return (
<Menu.Item key={option}>
<Menu.Item key={funnelName}>
{({ active }) => (
<span
onClick={this.setFunnel(option)}
onClick={this.setFunnel(funnelName)}
className={classNames(
active ? 'bg-gray-100 dark:bg-gray-900 text-gray-900 dark:text-gray-200 cursor-pointer' : 'text-gray-700 dark:text-gray-200',
'block px-4 py-2 text-sm',
this.state.mode === option ? 'font-bold' : ''
this.state.mode === funnelName ? 'font-bold' : ''
)}
>
{option}
{funnelName}
</span>
)}
</Menu.Item>
@ -118,7 +114,7 @@ export default class Behaviours extends React.Component {
case CONVERSIONS:
return <Conversions tabs={this.tabs()} site={this.props.site} query={this.props.query} />
case FUNNELS:
return <Funnel tabs={this.tabs()} funnel={this.selectedFunnel()} query={this.props.query} site={this.props.site} />
return <Funnel tabs={this.tabs()} funnelName={this.state.selectedFunnelName} query={this.props.query} site={this.props.site} />
}
}

Loading…
Cancel
Save