index.tsx
95 lines
| 1 | import React, {useState} from 'react'; |
| 2 | import styles from './style.module.scss'; |
| 3 | import {__} from '@wordpress/i18n'; |
| 4 | import {createInterpolateElement} from '@wordpress/element'; |
| 5 | import {RecommendedProductData, useRecommendations} from '@givewp/promotions/hooks/useRecommendations'; |
| 6 | |
| 7 | interface ProductRecommendationsProps { |
| 8 | apiSettings: {table; pluginUrl}; |
| 9 | options: any; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * @since 2.27.1 |
| 14 | */ |
| 15 | export default function ProductRecommendations({apiSettings, options}: ProductRecommendationsProps) { |
| 16 | const {getRandomRecommendation, getRecommendation, removeRecommendation} = useRecommendations(apiSettings, options); |
| 17 | const selectedOption = apiSettings === window.GiveDonors ? getRecommendation() : getRandomRecommendation(); |
| 18 | const [showRecommendation, setShowRecommendation] = useState<boolean>(!!selectedOption); |
| 19 | |
| 20 | const closeMessage = async (async) => { |
| 21 | setShowRecommendation(false); |
| 22 | |
| 23 | await removeRecommendation({ |
| 24 | option: selectedOption.enum, |
| 25 | }); |
| 26 | }; |
| 27 | |
| 28 | if (!showRecommendation) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | return ( |
| 33 | <RotatingMessage |
| 34 | columns={apiSettings.table.columns} |
| 35 | pluginUrl={apiSettings.pluginUrl} |
| 36 | selectedOption={selectedOption} |
| 37 | closeMessage={closeMessage} |
| 38 | /> |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | interface RotatingMessageProps { |
| 43 | selectedOption: RecommendedProductData; |
| 44 | closeMessage: (async: any) => Promise<void>; |
| 45 | pluginUrl: string; |
| 46 | columns: Array<{visible: boolean}>; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @since 2.27.1 |
| 51 | */ |
| 52 | function RotatingMessage({selectedOption, closeMessage, pluginUrl, columns}: RotatingMessageProps) { |
| 53 | const {message = '', documentationPage = '', innerHtml = ''} = selectedOption; |
| 54 | |
| 55 | const visibleColumns = columns?.filter((column) => column.visible || column.visible === undefined); |
| 56 | |
| 57 | return ( |
| 58 | <tr> |
| 59 | <td colSpan={visibleColumns.length + 1}> |
| 60 | <div className={styles.productRecommendation}> |
| 61 | <div className={styles.container}> |
| 62 | <img src={`${pluginUrl}/assets/dist/images/list-table/light-bulb-icon.svg`} /> |
| 63 | |
| 64 | <TranslatedMessage message={message} /> |
| 65 | |
| 66 | <a target="_blank" href={documentationPage}> |
| 67 | {innerHtml} |
| 68 | <img src={`${pluginUrl}/assets/dist/images/list-table/external-link-icon.svg`} /> |
| 69 | </a> |
| 70 | </div> |
| 71 | |
| 72 | <button onClick={closeMessage}> |
| 73 | <img src={`${pluginUrl}/assets/dist/images/list-table/circular-exit-icon.svg`} /> |
| 74 | </button> |
| 75 | </div> |
| 76 | </td> |
| 77 | </tr> |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | type TranslatedMessageProps = {message: string}; |
| 82 | |
| 83 | /** |
| 84 | * @since 2.27.1 |
| 85 | */ |
| 86 | function TranslatedMessage({message}: TranslatedMessageProps) { |
| 87 | |
| 88 | const translatedString = createInterpolateElement(__('<strong>PRO TIP: </strong> <message />', 'give'), { |
| 89 | strong: <strong />, |
| 90 | message: <p className={styles.message}>{message}</p>, |
| 91 | }); |
| 92 | |
| 93 | return translatedString; |
| 94 | } |
| 95 |