mon-entreprise/source/components/RulePage.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

import { encodeRuleName } from 'Engine/rules'
import {
decodeRuleName,
findRuleByDottedName,
findRulesByName
} from 'Engine/rules.js'
import { compose, head, path } from 'ramda'
import React, { Component } from 'react'
import { Trans, translate } from 'react-i18next'
import { connect } from 'react-redux'
2018-07-02 14:04:34 +00:00
import { withRouter } from 'react-router'
import { Link, Redirect } from 'react-router-dom'
import { animateScroll } from 'react-scroll'
import {
flatRulesSelector,
noUserInputSelector
} from 'Selectors/analyseSelectors'
import { setExample } from '../actions'
import Namespace from './rule/Namespace'
import Rule from './rule/Rule'
import './RulePage.css'
import SearchButton from './SearchButton'
@connect(state => ({
themeColours: state.themeColours,
valuesToShow: !noUserInputSelector(state),
flatRules: flatRulesSelector(state)
}))
2018-03-29 12:57:44 +00:00
@translate()
export default class RulePage extends Component {
componentDidMount() {
animateScroll.scrollToTop({ duration: 300 })
}
render() {
let { flatRules } = this.props,
name = path(['match', 'params', 'name'], this.props),
decodedRuleName = decodeRuleName(name)
2018-04-26 12:24:47 +00:00
if (decodedRuleName.includes(' . ')) {
if (!findRuleByDottedName(flatRules, decodedRuleName))
return <Redirect to="/404" />
return this.renderRule(decodedRuleName)
}
let rules = findRulesByName(flatRules, decodedRuleName)
if (!rules.length) return <Redirect to="/404" />
if (rules.length > 1)
return <DisambiguateRuleQuery rules={rules} flatRules={flatRules} />
2018-04-26 12:24:47 +00:00
let dottedName = head(rules).dottedName
return this.renderRule(dottedName)
}
renderRule(dottedName) {
return (
2018-03-19 18:18:58 +00:00
<div id="RulePage">
<div className="rule-page__header">
<SearchButton className="rule-page__search" />
{!this.props.noUserInputSelector && (
<BackToSimulation colour={this.props.themeColours.colour} />
)}
</div>
<Rule dottedName={dottedName} />
2018-03-19 18:18:58 +00:00
</div>
)
}
}
@connect(
null,
dispatch => ({
setExample: compose(
dispatch,
setExample
)
})
)
2018-07-02 14:04:34 +00:00
@withRouter
class BackToSimulation extends Component {
render() {
2018-07-02 14:04:34 +00:00
let { colour, setExample, history } = this.props
return (
2018-07-02 14:04:34 +00:00
<button
id="toSimulation"
2018-07-02 14:04:34 +00:00
onClick={() => {
setExample(null)
history.go(-1)
}}
style={{ background: colour }}>
<i className="fa fa-arrow-circle-left" aria-hidden="true" />
<Trans i18nKey="back">Reprendre la simulation</Trans>
2018-07-02 14:04:34 +00:00
</button>
)
}
}
let DisambiguateRuleQuery = ({ rules, flatRules }) => (
<div className="centeredMessage">
<p>
<Trans i18nKey="ambiguous">
Plusieurs règles de la base ont ce nom. Laquelle voulez-vous afficher ?
</Trans>
</p>
<ul>
{rules.map(({ dottedName, ns, title }) => (
<li key={dottedName}>
<Namespace ns={ns} flatRules={flatRules} />
<Link to={'/règle/' + encodeRuleName(dottedName)}>{title}</Link>
</li>
))}
</ul>
</div>
)