index.tsx
92 lines
| 1 | import styles from './ListTableRows.module.scss'; |
| 2 | import {__} from '@wordpress/i18n'; |
| 3 | import cx from 'classnames'; |
| 4 | import {useEffect, useState} from 'react'; |
| 5 | import TableCell from '../TableCell'; |
| 6 | import {BulkActionCheckbox} from '@givewp/components/ListTable/BulkActions/BulkActionCheckbox'; |
| 7 | import InterweaveSSR from '@givewp/components/ListTable/InterweaveSSR'; |
| 8 | |
| 9 | export default function ListTableRows({columns, data, isLoading, rowActions, setUpdateErrors, parameters, singleName}) { |
| 10 | const [removed, setRemoved] = useState([]); |
| 11 | const [added, setAdded] = useState([]); |
| 12 | |
| 13 | useEffect(() => { |
| 14 | if (added.length && !isLoading) { |
| 15 | const timeouts = []; |
| 16 | timeouts[0] = setTimeout(() => { |
| 17 | const addedItem = document.getElementsByClassName(styles.duplicated); |
| 18 | if (addedItem.length == 1) { |
| 19 | addedItem[0].scrollIntoView({behavior: 'smooth', block: 'center'}); |
| 20 | } |
| 21 | }, 100); |
| 22 | timeouts[1] = setTimeout(() => { |
| 23 | setAdded([]); |
| 24 | }, 600); |
| 25 | return () => { |
| 26 | timeouts.forEach((timeout) => clearTimeout(timeout)); |
| 27 | }; |
| 28 | } |
| 29 | }, [added, isLoading]); |
| 30 | |
| 31 | function removeRow(removeCallback) { |
| 32 | return async (event) => { |
| 33 | const id = event.target.dataset.actionid; |
| 34 | setRemoved([id]); |
| 35 | await removeCallback(id); |
| 36 | setRemoved([]); |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | function addRow(addCallback) { |
| 41 | return async (event) => { |
| 42 | const id = event.target.dataset.actionid; |
| 43 | const addedItem = await addCallback(id); |
| 44 | setAdded([...addedItem.successes]); |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | if (!data) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return data?.items.map((item) => ( |
| 53 | <tr |
| 54 | key={item.id} |
| 55 | className={cx(styles.tableRow, { |
| 56 | [styles.deleted]: removed.indexOf(item.id) > -1, |
| 57 | [styles.duplicated]: added.indexOf(parseInt(item.id)) > -1, |
| 58 | })} |
| 59 | > |
| 60 | <TableCell> |
| 61 | <BulkActionCheckbox |
| 62 | id={item.id} |
| 63 | name={item?.donor ?? item?.title ?? item?.donorInformation} |
| 64 | singleName={singleName} |
| 65 | data={data} |
| 66 | /> |
| 67 | </TableCell> |
| 68 | <> |
| 69 | {columns?.map((column) => { |
| 70 | return ( |
| 71 | <TableCell key={column.id} heading={columns[0].id === column.id}> |
| 72 | <InterweaveSSR column={column} item={item} /> |
| 73 | {columns[0].id === column.id && !isLoading && rowActions && ( |
| 74 | <div role="group" aria-label={__('Actions', 'give')} className={styles.tableRowActions}> |
| 75 | {rowActions({ |
| 76 | data, |
| 77 | item, |
| 78 | removeRow, |
| 79 | addRow, |
| 80 | setUpdateErrors, |
| 81 | parameters, |
| 82 | })} |
| 83 | </div> |
| 84 | )} |
| 85 | </TableCell> |
| 86 | ); |
| 87 | })} |
| 88 | </> |
| 89 | </tr> |
| 90 | )); |
| 91 | } |
| 92 |