2018-06-19 11:51:00 +00:00
|
|
|
import { encodeRuleName } from 'Engine/rules'
|
2018-02-12 14:53:07 +00:00
|
|
|
import {
|
|
|
|
decodeRuleName,
|
2018-06-19 11:51:00 +00:00
|
|
|
findRuleByDottedName,
|
|
|
|
findRulesByName
|
2018-02-12 14:53:07 +00:00
|
|
|
} from 'Engine/rules.js'
|
2018-06-19 11:51:00 +00:00
|
|
|
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'
|
2018-01-03 15:54:19 +00:00
|
|
|
import { Link, Redirect } from 'react-router-dom'
|
|
|
|
import { animateScroll } from 'react-scroll'
|
2018-06-12 13:52:41 +00:00
|
|
|
import {
|
2018-06-19 11:51:00 +00:00
|
|
|
flatRulesSelector,
|
|
|
|
noUserInputSelector
|
2018-06-12 13:52:41 +00:00
|
|
|
} from 'Selectors/analyseSelectors'
|
2018-06-19 11:51:00 +00:00
|
|
|
import { setExample } from '../actions'
|
|
|
|
import Namespace from './rule/Namespace'
|
|
|
|
import Rule from './rule/Rule'
|
|
|
|
import './RulePage.css'
|
|
|
|
import SearchButton from './SearchButton'
|
2017-12-07 14:19:51 +00:00
|
|
|
|
2018-06-12 13:52:41 +00:00
|
|
|
@connect(state => ({
|
|
|
|
themeColours: state.themeColours,
|
|
|
|
valuesToShow: !noUserInputSelector(state),
|
|
|
|
flatRules: flatRulesSelector(state)
|
|
|
|
}))
|
2018-03-29 12:57:44 +00:00
|
|
|
@translate()
|
2017-12-07 14:19:51 +00:00
|
|
|
export default class RulePage extends Component {
|
2018-01-03 15:54:19 +00:00
|
|
|
componentDidMount() {
|
|
|
|
animateScroll.scrollToTop({ duration: 300 })
|
2017-12-07 19:00:36 +00:00
|
|
|
}
|
2018-06-12 13:52:41 +00:00
|
|
|
render() {
|
|
|
|
let { flatRules } = this.props,
|
|
|
|
name = path(['match', 'params', 'name'], this.props),
|
2018-02-12 14:53:07 +00:00
|
|
|
decodedRuleName = decodeRuleName(name)
|
2018-04-26 12:24:47 +00:00
|
|
|
|
2018-02-12 14:53:07 +00:00
|
|
|
if (decodedRuleName.includes(' . ')) {
|
2018-06-12 13:52:41 +00:00
|
|
|
if (!findRuleByDottedName(flatRules, decodedRuleName))
|
|
|
|
return <Redirect to="/404" />
|
|
|
|
|
|
|
|
return this.renderRule(decodedRuleName)
|
2018-02-12 14:53:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 13:52:41 +00:00
|
|
|
let rules = findRulesByName(flatRules, decodedRuleName)
|
|
|
|
if (!rules.length) return <Redirect to="/404" />
|
2018-06-19 16:01:49 +00:00
|
|
|
if (rules.length > 1)
|
|
|
|
return <DisambiguateRuleQuery rules={rules} flatRules={flatRules} />
|
2018-04-26 12:24:47 +00:00
|
|
|
let dottedName = head(rules).dottedName
|
2018-06-12 13:52:41 +00:00
|
|
|
return this.renderRule(dottedName)
|
2017-12-07 14:19:51 +00:00
|
|
|
}
|
2018-06-12 13:52:41 +00:00
|
|
|
renderRule(dottedName) {
|
2018-01-03 15:54:19 +00:00
|
|
|
return (
|
2018-03-19 18:18:58 +00:00
|
|
|
<div id="RulePage">
|
2018-06-19 11:51:00 +00:00
|
|
|
<div className="rule-page__header">
|
|
|
|
<SearchButton className="rule-page__search" />
|
|
|
|
{!this.props.noUserInputSelector && (
|
|
|
|
<BackToSimulation colour={this.props.themeColours.colour} />
|
|
|
|
)}
|
|
|
|
</div>
|
2018-06-12 13:52:41 +00:00
|
|
|
<Rule dottedName={dottedName} />
|
2018-03-19 18:18:58 +00:00
|
|
|
</div>
|
2018-01-03 15:54:19 +00:00
|
|
|
)
|
2017-12-07 14:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-07 19:00:36 +00:00
|
|
|
|
2018-06-19 11:51:00 +00:00
|
|
|
@connect(
|
|
|
|
null,
|
|
|
|
dispatch => ({
|
|
|
|
setExample: compose(
|
|
|
|
dispatch,
|
|
|
|
setExample
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
2018-07-02 14:04:34 +00:00
|
|
|
@withRouter
|
2018-04-27 09:04:45 +00:00
|
|
|
class BackToSimulation extends Component {
|
|
|
|
render() {
|
2018-07-02 14:04:34 +00:00
|
|
|
let { colour, setExample, history } = this.props
|
2018-04-27 09:04:45 +00:00
|
|
|
return (
|
2018-07-02 14:04:34 +00:00
|
|
|
<button
|
2018-04-27 09:04:45 +00:00
|
|
|
id="toSimulation"
|
2018-07-02 14:04:34 +00:00
|
|
|
onClick={() => {
|
|
|
|
setExample(null)
|
|
|
|
history.go(-1)
|
|
|
|
}}
|
2018-04-27 09:04:45 +00:00
|
|
|
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>
|
2018-04-27 09:04:45 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-02-12 14:53:07 +00:00
|
|
|
|
2018-06-19 16:01:49 +00:00
|
|
|
let DisambiguateRuleQuery = ({ rules, flatRules }) => (
|
2018-02-12 14:53:07 +00:00
|
|
|
<div className="centeredMessage">
|
|
|
|
<p>
|
2018-04-06 10:50:11 +00:00
|
|
|
<Trans i18nKey="ambiguous">
|
|
|
|
Plusieurs règles de la base ont ce nom. Laquelle voulez-vous afficher ?
|
|
|
|
</Trans>
|
2018-02-12 14:53:07 +00:00
|
|
|
</p>
|
|
|
|
<ul>
|
|
|
|
{rules.map(({ dottedName, ns, title }) => (
|
|
|
|
<li key={dottedName}>
|
2018-06-19 16:01:49 +00:00
|
|
|
<Namespace ns={ns} flatRules={flatRules} />
|
2018-02-12 14:53:07 +00:00
|
|
|
<Link to={'/règle/' + encodeRuleName(dottedName)}>{title}</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|