2017-12-07 14:19:51 +00:00
|
|
|
import React, { Component } from 'react'
|
2018-03-29 12:57:44 +00:00
|
|
|
import { Trans, translate } from 'react-i18next'
|
2017-12-07 14:19:51 +00:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import Rule from 'Components/rule/Rule'
|
|
|
|
import { analyse } from 'Engine/traverse'
|
|
|
|
import { head, path } from 'ramda'
|
2018-02-12 14:53:07 +00:00
|
|
|
import {
|
|
|
|
decodeRuleName,
|
|
|
|
nameLeaf,
|
|
|
|
findRulesByName,
|
|
|
|
findRuleByDottedName
|
|
|
|
} from 'Engine/rules.js'
|
2018-01-03 15:54:19 +00:00
|
|
|
import { encodeRuleName } from 'Engine/rules'
|
2018-04-06 10:50:11 +00:00
|
|
|
import { pipe, pluck, join, map, pick } from 'ramda'
|
2018-01-03 15:54:19 +00:00
|
|
|
import { Link, Redirect } from 'react-router-dom'
|
|
|
|
import { animateScroll } from 'react-scroll'
|
2018-03-19 18:18:58 +00:00
|
|
|
import './RulePage.css'
|
2018-02-12 14:53:07 +00:00
|
|
|
import { Namespace } from './rule/Rule'
|
2018-03-19 18:18:58 +00:00
|
|
|
import SearchButton from 'Components/SearchButton'
|
2017-12-07 14:19:51 +00:00
|
|
|
|
2018-03-29 12:57:44 +00:00
|
|
|
@translate()
|
2018-04-06 10:50:11 +00:00
|
|
|
@connect(pick(['situationGate', 'parsedRules', 'analysis', 'themeColours']))
|
2017-12-07 14:19:51 +00:00
|
|
|
export default class RulePage extends Component {
|
|
|
|
nameFromParams = path(['match', 'params', 'name'])
|
|
|
|
componentWillMount() {
|
|
|
|
this.setRule(this.nameFromParams(this.props))
|
|
|
|
}
|
2018-01-03 15:54:19 +00:00
|
|
|
componentDidMount() {
|
|
|
|
animateScroll.scrollToTop({ duration: 300 })
|
2017-12-07 19:00:36 +00:00
|
|
|
}
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
if (this.nameFromParams(nextProps) !== this.nameFromParams(this.props)) {
|
|
|
|
this.setRule(this.nameFromParams(nextProps))
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 14:19:51 +00:00
|
|
|
setRule(name) {
|
2018-04-12 12:30:12 +00:00
|
|
|
let { parsedRules, situationGate } = this.props,
|
2018-02-12 14:53:07 +00:00
|
|
|
decodedRuleName = decodeRuleName(name)
|
|
|
|
if (decodedRuleName.includes(' . ')) {
|
2018-04-12 12:30:12 +00:00
|
|
|
let rule = findRuleByDottedName(parsedRules, decodedRuleName)
|
2018-02-12 14:53:07 +00:00
|
|
|
this.rule =
|
|
|
|
rule &&
|
2018-04-12 12:30:12 +00:00
|
|
|
head(analyse(parsedRules, rule.dottedName)(situationGate).targets)
|
2018-02-12 14:53:07 +00:00
|
|
|
this.multipleMatchingRules = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-12-07 14:19:51 +00:00
|
|
|
let ruleName = nameLeaf(decodeRuleName(name)),
|
2018-04-12 12:30:12 +00:00
|
|
|
rules = findRulesByName(parsedRules, ruleName)
|
2018-02-12 14:53:07 +00:00
|
|
|
if (!rules.length) return null
|
|
|
|
if (rules.length > 1) this.multipleMatchingRules = rules
|
2017-12-07 14:19:51 +00:00
|
|
|
this.rule = head(
|
2018-04-12 12:30:12 +00:00
|
|
|
analyse(parsedRules, head(rules).dottedName)(situationGate).targets
|
2017-12-07 14:19:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
render() {
|
2018-02-12 14:53:07 +00:00
|
|
|
if (this.multipleMatchingRules)
|
|
|
|
return <DisambiguateRuleQuery rules={this.multipleMatchingRules} />
|
2017-12-07 14:19:51 +00:00
|
|
|
if (!this.rule) return <Redirect to="/404" />
|
|
|
|
|
2017-12-07 19:00:36 +00:00
|
|
|
let targets = path(['analysis', 'targets'], this.props)
|
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
return (
|
2018-03-19 18:18:58 +00:00
|
|
|
<div id="RulePage">
|
2018-04-06 10:50:11 +00:00
|
|
|
{targets && (
|
|
|
|
<BackToSimulation
|
|
|
|
colour={this.props.themeColours.colour}
|
|
|
|
targets={targets}
|
|
|
|
/>
|
|
|
|
)}
|
2018-03-19 18:18:58 +00:00
|
|
|
<SearchButton />
|
2018-01-03 15:54:19 +00:00
|
|
|
<Rule rule={this.rule} />
|
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-04-06 10:50:11 +00:00
|
|
|
let BackToSimulation = ({ targets, colour }) => (
|
2018-01-03 15:54:19 +00:00
|
|
|
<Link
|
|
|
|
id="toSimulation"
|
|
|
|
to={'/simu/' + pipe(pluck('name'), map(encodeRuleName), join('+'))(targets)}
|
2018-04-06 10:50:11 +00:00
|
|
|
style={{ background: colour }}>
|
2018-03-29 12:57:44 +00:00
|
|
|
<i className="fa fa-arrow-circle-left" aria-hidden="true" />
|
|
|
|
<Trans i18nKey="back">Reprendre la simulation</Trans>
|
2017-12-07 19:00:36 +00:00
|
|
|
</Link>
|
2018-01-03 15:54:19 +00:00
|
|
|
)
|
2018-02-12 14:53:07 +00:00
|
|
|
|
|
|
|
let DisambiguateRuleQuery = ({ rules }) => (
|
|
|
|
<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}>
|
|
|
|
<Namespace ns={ns} />
|
|
|
|
<Link to={'/règle/' + encodeRuleName(dottedName)}>{title}</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
)
|