index.tsx
277 lines
| 1 | import {createContext, useRef, useState} from 'react'; |
| 2 | import {__} from '@wordpress/i18n'; |
| 3 | import {A11yDialog} from 'react-a11y-dialog'; |
| 4 | import A11yDialogInstance from 'a11y-dialog'; |
| 5 | import {GiveIcon} from '@givewp/components'; |
| 6 | import {ListTable} from '../ListTable'; |
| 7 | import Pagination from '../Pagination'; |
| 8 | import {Filter, getInitialFilterState} from '../Filters'; |
| 9 | import useDebounce from '../hooks/useDebounce'; |
| 10 | import {useResetPage} from '../hooks/useResetPage'; |
| 11 | import ListTableApi from '../api'; |
| 12 | import styles from './ListTablePage.module.scss'; |
| 13 | import cx from 'classnames'; |
| 14 | import {BulkActionSelect} from '@givewp/components/ListTable/BulkActions/BulkActionSelect'; |
| 15 | import ToggleSwitch from '@givewp/components/ListTable/ToggleSwitch'; |
| 16 | |
| 17 | export interface ListTablePageProps { |
| 18 | //required |
| 19 | title: string; |
| 20 | apiSettings: {apiRoot; apiNonce; table}; |
| 21 | |
| 22 | //optional |
| 23 | bulkActions?: Array<BulkActionsConfig> | null; |
| 24 | pluralName?: string; |
| 25 | singleName?: string; |
| 26 | children?: JSX.Element | JSX.Element[] | null; |
| 27 | rowActions?: JSX.Element | JSX.Element[] | Function | null; |
| 28 | filterSettings?; |
| 29 | align?: 'start' | 'center' | 'end'; |
| 30 | paymentMode?: boolean; |
| 31 | listTableBlankSlate: JSX.Element; |
| 32 | productRecommendation?: JSX.Element; |
| 33 | } |
| 34 | |
| 35 | export interface FilterConfig { |
| 36 | // required |
| 37 | name: string; |
| 38 | type: 'select' | 'formselect' | 'search' | 'checkbox'; |
| 39 | |
| 40 | // optional |
| 41 | ariaLabel?: string; |
| 42 | inlineSize?: string; |
| 43 | text?: string; |
| 44 | options?: Array<{text: string; value: string}>; |
| 45 | } |
| 46 | |
| 47 | export interface BulkActionsConfig { |
| 48 | //required |
| 49 | label: string; |
| 50 | value: string | number; |
| 51 | action: (selected: Array<string | number>) => Promise<{errors: string | number; successes: string | number}>; |
| 52 | confirm: (selected: Array<string | number>, names?: Array<string>) => JSX.Element | JSX.Element[] | string; |
| 53 | |
| 54 | //optional |
| 55 | isVisible?: (data, parameters) => Boolean; |
| 56 | type?: 'normal' | 'warning' | 'danger'; |
| 57 | } |
| 58 | |
| 59 | export const ShowConfirmModalContext = createContext((label, confirm, action, type = null) => {}); |
| 60 | export const CheckboxContext = createContext(null); |
| 61 | |
| 62 | export default function ListTablePage({ |
| 63 | title, |
| 64 | apiSettings, |
| 65 | bulkActions = null, |
| 66 | filterSettings = [], |
| 67 | singleName = __('item', 'give'), |
| 68 | pluralName = __('items', 'give'), |
| 69 | rowActions = null, |
| 70 | children = null, |
| 71 | align = 'start', |
| 72 | paymentMode, |
| 73 | listTableBlankSlate, |
| 74 | productRecommendation, |
| 75 | }: ListTablePageProps) { |
| 76 | const [page, setPage] = useState<number>(1); |
| 77 | const [perPage, setPerPage] = useState<number>(30); |
| 78 | const [filters, setFilters] = useState(getInitialFilterState(filterSettings)); |
| 79 | const [modalContent, setModalContent] = useState<{confirm; action; label; type?: 'normal' | 'warning' | 'danger'}>({ |
| 80 | confirm: (selected) => {}, |
| 81 | action: (selected) => {}, |
| 82 | label: '', |
| 83 | }); |
| 84 | const [selectedIds, setSelectedIds] = useState([]); |
| 85 | const [selectedNames, setSelectedNames] = useState([]); |
| 86 | const dialog = useRef() as {current: A11yDialogInstance}; |
| 87 | const checkboxRefs = useRef([]); |
| 88 | const [sortField, setSortField] = useState<{sortColumn: string; sortDirection: string}>({ |
| 89 | sortColumn: 'id', |
| 90 | sortDirection: 'desc', |
| 91 | }); |
| 92 | const [testMode, setTestMode] = useState(paymentMode); |
| 93 | |
| 94 | const {sortColumn, sortDirection} = sortField; |
| 95 | const locale = navigator.language || navigator.languages[0]; |
| 96 | const testModeFilter = filterSettings.find((filter) => filter.name === 'toggle'); |
| 97 | |
| 98 | const parameters = { |
| 99 | page, |
| 100 | perPage, |
| 101 | sortColumn, |
| 102 | sortDirection, |
| 103 | locale, |
| 104 | testMode, |
| 105 | ...filters, |
| 106 | }; |
| 107 | |
| 108 | const archiveApi = useRef(new ListTableApi(apiSettings)).current; |
| 109 | |
| 110 | const {data, error, isValidating, mutate} = archiveApi.useListTable(parameters); |
| 111 | |
| 112 | useResetPage(data, page, setPage, filters); |
| 113 | |
| 114 | const handleFilterChange = (name, value) => { |
| 115 | setFilters((prevState) => ({...prevState, [name]: value})); |
| 116 | }; |
| 117 | |
| 118 | const handleDebouncedFilterChange = useDebounce(handleFilterChange); |
| 119 | |
| 120 | const showConfirmActionModal = (label, confirm, action, type: 'normal' | 'warning' | 'danger' | null = null) => { |
| 121 | setModalContent({confirm, action, label, type}); |
| 122 | dialog.current.show(); |
| 123 | }; |
| 124 | |
| 125 | const openBulkActionModal = (event) => { |
| 126 | event.preventDefault(); |
| 127 | |
| 128 | if (window.GiveDonations && window.GiveDonations.addonsBulkActions) { |
| 129 | bulkActions = [...bulkActions, ...window.GiveDonations.addonsBulkActions]; |
| 130 | } |
| 131 | |
| 132 | const formData = new FormData(event.target); |
| 133 | const action = formData.get('giveListTableBulkActions'); |
| 134 | const actionIndex = bulkActions.findIndex((config) => action == config.value); |
| 135 | if (actionIndex < 0) return; |
| 136 | const selected = []; |
| 137 | const names = []; |
| 138 | checkboxRefs.current.forEach((checkbox) => { |
| 139 | if (checkbox.checked) { |
| 140 | selected.push(checkbox.dataset.id); |
| 141 | names.push(checkbox.dataset.name); |
| 142 | } |
| 143 | }); |
| 144 | setSelectedIds(selected); |
| 145 | setSelectedNames(names); |
| 146 | if (selected.length) { |
| 147 | setModalContent({...bulkActions[actionIndex]}); |
| 148 | dialog.current.show(); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | const setSortDirectionForColumn = (column, direction) => { |
| 153 | setSortField((previousState) => { |
| 154 | return { |
| 155 | ...previousState, |
| 156 | sortColumn: column, |
| 157 | sortDirection: direction, |
| 158 | }; |
| 159 | }); |
| 160 | }; |
| 161 | |
| 162 | const showPagination = () => ( |
| 163 | <Pagination |
| 164 | currentPage={page} |
| 165 | totalPages={data ? data.totalPages : 1} |
| 166 | disabled={!data} |
| 167 | totalItems={data ? parseInt(data.totalItems) : -1} |
| 168 | setPage={setPage} |
| 169 | singleName={singleName} |
| 170 | pluralName={pluralName} |
| 171 | /> |
| 172 | ); |
| 173 | |
| 174 | const PageActions = ({PageActionsTop}: {PageActionsTop?: boolean}) => ( |
| 175 | <div className={cx(styles.pageActions, {[styles.alignEnd]: !bulkActions})}> |
| 176 | <BulkActionSelect |
| 177 | parameters={parameters} |
| 178 | data={data} |
| 179 | bulkActions={bulkActions} |
| 180 | showModal={openBulkActionModal} |
| 181 | /> |
| 182 | {PageActionsTop && testModeFilter && <TestModeFilter />} |
| 183 | {page && setPage && showPagination()} |
| 184 | </div> |
| 185 | ); |
| 186 | |
| 187 | const TestModeFilter = () => ( |
| 188 | <ToggleSwitch ariaLabel={testModeFilter?.ariaLabel} onChange={setTestMode} checked={testMode} /> |
| 189 | ); |
| 190 | |
| 191 | const TestModeBadge = () => <span>{testModeFilter?.text}</span>; |
| 192 | |
| 193 | return ( |
| 194 | <> |
| 195 | <article className={styles.page}> |
| 196 | <header className={styles.pageHeader}> |
| 197 | <div className={styles.flexRow}> |
| 198 | <GiveIcon size={'1.875rem'} /> |
| 199 | <h1 className={styles.pageTitle}>{title}</h1> |
| 200 | {testModeFilter && testMode && <TestModeBadge />} |
| 201 | </div> |
| 202 | {children && <div className={styles.flexRow}>{children}</div>} |
| 203 | </header> |
| 204 | <section role="search" id={styles.searchContainer}> |
| 205 | {filterSettings.map((filter) => ( |
| 206 | <Filter |
| 207 | key={filter.name} |
| 208 | value={filters[filter.name]} |
| 209 | filter={filter} |
| 210 | onChange={handleFilterChange} |
| 211 | debouncedOnChange={handleDebouncedFilterChange} |
| 212 | /> |
| 213 | ))} |
| 214 | </section> |
| 215 | <div className={cx('wp-header-end', 'hidden')} /> |
| 216 | <div className={styles.pageContent}> |
| 217 | <PageActions PageActionsTop /> |
| 218 | <CheckboxContext.Provider value={checkboxRefs}> |
| 219 | <ShowConfirmModalContext.Provider value={showConfirmActionModal}> |
| 220 | <ListTable |
| 221 | apiSettings={apiSettings} |
| 222 | sortField={sortField} |
| 223 | setSortDirectionForColumn={setSortDirectionForColumn} |
| 224 | singleName={singleName} |
| 225 | pluralName={pluralName} |
| 226 | title={title} |
| 227 | rowActions={rowActions} |
| 228 | parameters={parameters} |
| 229 | data={data} |
| 230 | error={error} |
| 231 | isLoading={isValidating} |
| 232 | align={align} |
| 233 | testMode={testMode} |
| 234 | listTableBlankSlate={listTableBlankSlate} |
| 235 | productRecommendation={productRecommendation} |
| 236 | /> |
| 237 | </ShowConfirmModalContext.Provider> |
| 238 | </CheckboxContext.Provider> |
| 239 | <PageActions /> |
| 240 | </div> |
| 241 | </article> |
| 242 | <A11yDialog |
| 243 | id="giveListTableModal" |
| 244 | dialogRef={(instance) => (dialog.current = instance)} |
| 245 | title={modalContent.label} |
| 246 | titleId={styles.modalTitle} |
| 247 | classNames={{ |
| 248 | container: styles.container, |
| 249 | overlay: styles.overlay, |
| 250 | dialog: cx(styles.dialog, { |
| 251 | [styles.warning]: modalContent?.type === 'warning', |
| 252 | [styles.danger]: modalContent?.type === 'danger', |
| 253 | }), |
| 254 | closeButton: 'hidden', |
| 255 | }} |
| 256 | > |
| 257 | <div className={styles.modalContent}>{modalContent?.confirm(selectedIds, selectedNames) || null}</div> |
| 258 | <div className={styles.gutter}> |
| 259 | <button id={styles.cancel} onClick={(event) => dialog.current?.hide()}> |
| 260 | {__('Cancel', 'give')} |
| 261 | </button> |
| 262 | <button |
| 263 | id={styles.confirm} |
| 264 | onClick={async (event) => { |
| 265 | dialog.current?.hide(); |
| 266 | await modalContent.action(selectedIds); |
| 267 | await mutate(); |
| 268 | }} |
| 269 | > |
| 270 | {__('Confirm', 'give')} |
| 271 | </button> |
| 272 | </div> |
| 273 | </A11yDialog> |
| 274 | </> |
| 275 | ); |
| 276 | } |
| 277 |