mon-entreprise/source/components/RulePage.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

import React, { Component } from 'react'
2018-03-29 12:57:44 +00:00
import { Trans, translate } from 'react-i18next'
import { connect } from 'react-redux'
import Rule from 'Components/rule/Rule'
import { analyse } from 'Engine/traverse'
import { head, path } from 'ramda'
import {
decodeRuleName,
nameLeaf,
findRulesByName,
findRuleByDottedName
} from 'Engine/rules.js'
import { encodeRuleName } from 'Engine/rules'
import { pipe, pluck, join, map, pick } from 'ramda'
import { Link, Redirect } from 'react-router-dom'
import { animateScroll } from 'react-scroll'
2018-03-19 18:18:58 +00:00
import './RulePage.css'
import { Namespace } from './rule/Rule'
2018-03-19 18:18:58 +00:00
import SearchButton from 'Components/SearchButton'
2018-03-29 12:57:44 +00:00
@translate()
@connect(pick(['situationGate', 'parsedRules', 'analysis', 'themeColours']))
export default class RulePage extends Component {
nameFromParams = path(['match', 'params', 'name'])
componentWillMount() {
this.setRule(this.nameFromParams(this.props))
}
componentDidMount() {
animateScroll.scrollToTop({ duration: 300 })
}
componentWillReceiveProps(nextProps) {
if (this.nameFromParams(nextProps) !== this.nameFromParams(this.props)) {
this.setRule(this.nameFromParams(nextProps))
}
}
setRule(name) {
2018-04-26 12:24:47 +00:00
let { parsedRules } = this.props,
decodedRuleName = decodeRuleName(name)
2018-04-26 12:24:47 +00:00
if (decodedRuleName.includes(' . ')) {
this.multipleMatchingRules = false
2018-04-26 12:24:47 +00:00
this.setDottedRule(decodedRuleName)
return
}
let ruleName = nameLeaf(decodeRuleName(name)),
rules = findRulesByName(parsedRules, ruleName)
if (!rules.length) return null
if (rules.length > 1) this.multipleMatchingRules = rules
2018-04-26 12:24:47 +00:00
let dottedName = head(rules).dottedName
this.setDottedRule(dottedName)
}
setDottedRule(dottedName) {
let { parsedRules, situationGate } = this.props,
rule = findRuleByDottedName(parsedRules, dottedName)
if (!rule) return null
this.rule = head(
2018-04-26 12:24:47 +00:00
analyse(parsedRules, rule.dottedName)(situationGate).targets
)
}
render() {
if (this.multipleMatchingRules)
return <DisambiguateRuleQuery rules={this.multipleMatchingRules} />
if (!this.rule) return <Redirect to="/404" />
let targets = path(['analysis', 'targets'], this.props)
return (
2018-03-19 18:18:58 +00:00
<div id="RulePage">
{targets && (
<BackToSimulation
colour={this.props.themeColours.colour}
targets={targets}
/>
)}
2018-03-19 18:18:58 +00:00
<SearchButton />
<Rule rule={this.rule} />
2018-03-19 18:18:58 +00:00
</div>
)
}
}
let BackToSimulation = ({ targets, colour }) => (
<Link
id="toSimulation"
to={'/simu/' + pipe(pluck('name'), map(encodeRuleName), join('+'))(targets)}
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>
</Link>
)
let DisambiguateRuleQuery = ({ rules }) => (
<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} />
<Link to={'/règle/' + encodeRuleName(dottedName)}>{title}</Link>
</li>
))}
</ul>
</div>
)