mon-entreprise/source/components/Controls.js

103 lines
2.5 KiB
JavaScript
Raw Normal View History

import { goToQuestion, hideControl } from 'Actions/actions'
import { makeJsx } from 'Engine/evaluation'
import { compose } from 'ramda'
import React from 'react'
import emoji from 'react-easy-emoji'
import { useTranslation } from 'react-i18next'
import { connect } from 'react-redux'
import { analysisWithDefaultsSelector } from 'Selectors/analyseSelectors'
2019-04-03 09:16:09 +00:00
import animate from 'Ui/animate'
import './Controls.css'
import { Markdown } from './utils/markdown'
import { ScrollToElement } from './utils/Scroll'
function Controls({
controls,
goToQuestion,
2018-10-02 16:14:01 +00:00
hideControl,
2018-10-02 14:52:27 +00:00
foldedSteps,
hiddenControls,
inversionFail
}) {
const { t } = useTranslation()
if (!controls) {
return null
}
let messages = [
...controls,
...(inversionFail
? [
{
message: t([
'simulateurs.inversionFail',
'Le montant saisi est trop faible ou aboutit à une situation impossible, essayez en un autre'
]),
level: 'avertissement'
}
]
: [])
]
if (!messages?.length) return null
return (
2018-09-11 15:33:11 +00:00
<div id="controlsBlock">
2019-04-03 09:16:09 +00:00
<ul style={{ margin: 0, padding: 0 }}>
{messages.map(({ level, test, message, solution, evaluated }) =>
hiddenControls.includes(test) ? null : (
<animate.fromTop key={message}>
<li key={test}>
<div className="control">
{emoji(level == 'avertissement' ? '⚠️' : '')}
<div className="controlText ui__ card">
{message ? (
<Markdown source={message} />
) : (
<span id="controlExplanation">{makeJsx(evaluated)}</span>
)}
{solution && !foldedSteps.includes(solution.cible) && (
<div>
<button
key={solution.cible}
className="ui__ link-button"
onClick={() => goToQuestion(solution.cible)}
>
{solution.texte}
</button>
</div>
)}
<button
className="hide"
aria-label="close"
onClick={() => hideControl(test)}
>
×
</button>
</div>
2019-04-03 09:16:09 +00:00
</div>
</li>
<ScrollToElement />
</animate.fromTop>
)
)}
</ul>
</div>
)
}
export default compose(
connect(
(state, props) => ({
foldedSteps: state.simulation?.foldedSteps,
controls: analysisWithDefaultsSelector(state)?.controls,
inversionFail: analysisWithDefaultsSelector(state)?.cache._meta
.inversionFail,
key: props.language,
hiddenControls: state.simulation.hiddenControls
}),
{
goToQuestion,
hideControl
}
2019-09-11 16:17:07 +00:00
)
)(Controls)