|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { FormattedMessage } from 'react-intl'; |
| 3 | +import PropTypes from 'prop-types'; |
| 4 | +import Icon from './Icon'; |
| 5 | +import { useDeepLink } from '../util/vehicleRentalUtils'; |
| 6 | +import { useConfigContext } from '../configurations/ConfigContext'; |
| 7 | +import { useTranslationsContext } from '../util/useTranslationsContext'; |
| 8 | + |
| 9 | +export default function Disclaimer({ |
| 10 | + header, |
| 11 | + headerId, |
| 12 | + text, |
| 13 | + textId, |
| 14 | + linkLabel, |
| 15 | + linkLabelId, |
| 16 | + values, |
| 17 | + href, |
| 18 | + useLinkButton, |
| 19 | + closable, |
| 20 | + onClose, // hook e.g. for remembering closing |
| 21 | +}) { |
| 22 | + const { colors } = useConfigContext(); |
| 23 | + const intl = useTranslationsContext(); |
| 24 | + |
| 25 | + const [showCard, setShowCard] = useState(true); |
| 26 | + |
| 27 | + const handleClose = () => { |
| 28 | + setShowCard(false); |
| 29 | + if (onClose) { |
| 30 | + onClose(); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + const onClick = href?.startsWith('http') |
| 35 | + ? () => { |
| 36 | + window.open(href, '_blank', 'noopener,noreferrer'); |
| 37 | + } |
| 38 | + : () => useDeepLink(href, window.location.href); |
| 39 | + |
| 40 | + if (!showCard) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + const hdr = headerId ? ( |
| 44 | + <FormattedMessage id={headerId} values={values} /> |
| 45 | + ) : ( |
| 46 | + header |
| 47 | + ); |
| 48 | + const txt = textId ? <FormattedMessage id={textId} values={values} /> : text; |
| 49 | + const label = linkLabelId ? ( |
| 50 | + <FormattedMessage id={linkLabelId} values={values} /> |
| 51 | + ) : ( |
| 52 | + linkLabel |
| 53 | + ); |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className="disclaimer-container"> |
| 57 | + <Icon className="info" img="icon_info" /> |
| 58 | + <div className="disclaimer"> |
| 59 | + <div className="disclaimer-header"> |
| 60 | + {hdr && <h3>{hdr}</h3>} |
| 61 | + {closable && ( |
| 62 | + <button |
| 63 | + className="disclaimer-close" |
| 64 | + aria-label={intl.formatMessage({ id: 'close' })} |
| 65 | + onClick={handleClose} |
| 66 | + type="button" |
| 67 | + > |
| 68 | + <Icon color={colors.primary} img="icon_close" /> |
| 69 | + </button> |
| 70 | + )} |
| 71 | + </div> |
| 72 | + {txt} |
| 73 | + {href && useLinkButton && ( |
| 74 | + <button |
| 75 | + type="button" |
| 76 | + className="external-link-button" |
| 77 | + onClick={e => { |
| 78 | + e.stopPropagation(); |
| 79 | + onClick(e); |
| 80 | + }} |
| 81 | + > |
| 82 | + {label} |
| 83 | + </button> |
| 84 | + )} |
| 85 | + {href && !useLinkButton && ( |
| 86 | + <a |
| 87 | + className="external-link" |
| 88 | + href={href} |
| 89 | + target="_blank" |
| 90 | + rel="noreferrer" |
| 91 | + > |
| 92 | + {label} |
| 93 | + <Icon className="arrow" img="icon_arrow-collapse--right" /> |
| 94 | + </a> |
| 95 | + )} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +Disclaimer.propTypes = { |
| 102 | + header: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 103 | + headerId: PropTypes.string, |
| 104 | + text: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 105 | + textId: PropTypes.string, |
| 106 | + linkLabel: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), |
| 107 | + linkLabelId: PropTypes.string, |
| 108 | + values: PropTypes.objectOf(PropTypes.string), |
| 109 | + href: PropTypes.string, |
| 110 | + useLinkButton: PropTypes.bool, |
| 111 | + closable: PropTypes.bool, |
| 112 | + onClose: PropTypes.func, |
| 113 | +}; |
| 114 | + |
| 115 | +Disclaimer.defaultProps = { |
| 116 | + header: null, |
| 117 | + headerId: undefined, |
| 118 | + text: null, |
| 119 | + textId: undefined, |
| 120 | + linkLabel: null, |
| 121 | + linkLabelId: undefined, |
| 122 | + values: {}, |
| 123 | + href: undefined, |
| 124 | + useLinkButton: false, |
| 125 | + closable: false, |
| 126 | + onClose: undefined, |
| 127 | +}; |
0 commit comments