|
| 1 | +import PropTypes from 'prop-types'; |
| 2 | +import cx from 'classnames'; |
| 3 | +import React from 'react'; |
| 4 | +import { FormattedMessage, useIntl } from 'react-intl'; |
| 5 | +import Icon from '../Icon'; |
| 6 | +import { useConfigContext } from '../../configurations/ConfigContext'; |
| 7 | + |
| 8 | +export default function Feedback({ |
| 9 | + recommended, // true if ranked as best by personalisation algo |
| 10 | + feedback, // true=likes, false=dislikes, undefined=no feedback yet |
| 11 | + giveFeedback, // callback to submit user's feedback action |
| 12 | +}) { |
| 13 | + const intl = useIntl(); |
| 14 | + const { colors } = useConfigContext(); |
| 15 | + |
| 16 | + let status; |
| 17 | + if (feedback === true) { |
| 18 | + status = 'personalisation-liked'; |
| 19 | + } else if (feedback === false) { |
| 20 | + status = 'personalisation-disliked'; |
| 21 | + } else { |
| 22 | + status = 'personalisation-ask'; |
| 23 | + } |
| 24 | + |
| 25 | + const favIcon = recommended |
| 26 | + ? 'icon_star-with-circle' |
| 27 | + : 'icon_star-unselected'; |
| 28 | + const iconMap = { |
| 29 | + 'personalisation-ask': { |
| 30 | + img: favIcon, |
| 31 | + className: cx('favourite', { selected: recommended }), |
| 32 | + fill: recommended ? '#c53291' : '#FFF', |
| 33 | + }, |
| 34 | + 'personalisation-liked': { img: 'icon_thumb', color: colors.primary }, |
| 35 | + 'personalisation-disliked': { |
| 36 | + img: 'icon_thumb-down', |
| 37 | + color: colors.primary, |
| 38 | + }, |
| 39 | + }; |
| 40 | + const iconProps = iconMap[status]; |
| 41 | + |
| 42 | + return ( |
| 43 | + <div className="feedback-container"> |
| 44 | + <div |
| 45 | + className={cx('feedback-section', { |
| 46 | + 'feedback-text-posted': status !== 'personalisation-ask', |
| 47 | + })} |
| 48 | + > |
| 49 | + <Icon {...iconProps} height={1.4} width={1.4} /> |
| 50 | + <span> </span> |
| 51 | + <FormattedMessage id={status} /> |
| 52 | + </div> |
| 53 | + {status === 'personalisation-ask' && ( |
| 54 | + <div className="feedback-section"> |
| 55 | + <button |
| 56 | + type="button" |
| 57 | + className="thumb-button" |
| 58 | + onClick={() => giveFeedback(true)} |
| 59 | + aria-label={intl.formatMessage({ id: 'personalisation-aria-like' })} |
| 60 | + > |
| 61 | + <Icon |
| 62 | + img="icon_thumb" |
| 63 | + color={colors.primary} |
| 64 | + height={1} |
| 65 | + width={1} |
| 66 | + /> |
| 67 | + </button> |
| 68 | + <button |
| 69 | + type="button" |
| 70 | + className="thumb-button" |
| 71 | + onClick={() => giveFeedback(false)} |
| 72 | + aria-label={intl.formatMessage({ |
| 73 | + id: 'personalisation-aria-dislike', |
| 74 | + })} |
| 75 | + > |
| 76 | + <Icon |
| 77 | + img="icon_thumb-down" |
| 78 | + color={colors.primary} |
| 79 | + height={1} |
| 80 | + width={1} |
| 81 | + /> |
| 82 | + </button> |
| 83 | + </div> |
| 84 | + )} |
| 85 | + </div> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +Feedback.propTypes = { |
| 90 | + recommended: PropTypes.bool, |
| 91 | + feedback: PropTypes.bool, |
| 92 | + giveFeedback: PropTypes.func.isRequired, |
| 93 | +}; |
0 commit comments