mon-entreprise/source/components/Sondage.js

109 lines
2.9 KiB
JavaScript
Raw Normal View History

2018-05-02 15:55:25 +00:00
import React, { Component } from 'react'
import './Sondage.css'
import { connect } from 'react-redux'
import ReactPiwik from './Tracker'
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'
2018-05-02 15:55:25 +00:00
import Smiley from './SatisfactionSmiley'
2018-05-03 13:24:19 +00:00
import TypeFormEmbed from './TypeFormEmbed'
import withLanguage from './withLanguage'
import { Trans, translate } from 'react-i18next'
import withColours from './withColours';
2018-05-02 15:55:25 +00:00
@connect(state => ({
targets: state.analysis ? state.analysis.targets : [],
activeInput: state.activeTargetInput,
currentQuestion: state.currentQuestion,
conversationStarted: state.conversationStarted
2018-05-02 15:55:25 +00:00
}))
@translate()
@withLanguage
@withColours
export default class Sondage extends Component {
state = {
visible: false,
showForm: false,
askFeedbackTime: 'AFTER_FIRST_ESTIMATE'
}
static getDerivedStateFromProps(nextProps, currentState) {
let feedbackAlreadyAsked = !!document.cookie.includes('feedback_asked=true')
let conditions = {
AFTER_FIRST_ESTIMATE: nextProps.activeInput && nextProps.targets.length,
AFTER_SIMULATION_COMPLETED:
!nextProps.currentQuestion && nextProps.conversationStarted
}
return {
visible: conditions[currentState.askFeedbackTime] && !feedbackAlreadyAsked
}
}
componentDidMount() {
this.setState({
askFeedbackTime:
Math.random() > 0.5
? 'AFTER_SIMULATION_COMPLETED'
: 'AFTER_FIRST_ESTIMATE'
})
}
2018-05-03 13:24:19 +00:00
handleClose = () => {
this.setState({ visible: false })
this.setCookie()
}
setCookie = () => {
document.cookie = 'feedback_asked=true;'
}
onSmileyClick = satisfaction => {
ReactPiwik.push(['trackEvent', 'feedback', 'smiley', satisfaction])
this.setState({ showForm: true, satisfaction, visible: false })
this.setCookie()
}
2018-05-02 15:55:25 +00:00
render() {
let { satisfaction, showForm, visible, askFeedbackTime } = this.state,
{ language, colours: {colour} } = this.props
2018-05-02 15:55:25 +00:00
return (
<>
{showForm && (
<TypeFormEmbed
hiddenVariables={{
exterieur: false,
satisfaction,
answertiming: askFeedbackTime,
language
}}
/>
2018-05-03 13:24:19 +00:00
)}
<ReactCSSTransitionGroup
transitionName="slide-blurred-bottom"
transitionEnterTimeout={2800}
transitionLeaveTimeout={300}>
{visible && (
<div className="sondage__container">
<div className="sondage" style={{color: colour, borderColor: colour}}>
<span className="sondage__text">
<Trans>Votre avis nous intéresse !</Trans>
</span>
<Smiley
text=":)"
hoverColor="#16a085"
onClick={this.onSmileyClick}
/>
<Smiley
text=":|"
hoverColor="#f39c12"
onClick={this.onSmileyClick}
/>
<button
className="sondage__closeButton unstyledButton"
onClick={this.handleClose}
aria-label="close">
X
</button>
</div>
</div>
)}
</ReactCSSTransitionGroup>
</>
2018-05-02 15:55:25 +00:00
)
}
}