AdminDonationFormsPage.module.scss
4 years ago
AdminDonationFormsPage.tsx
4 years ago
DonationFormsTable.module.scss
4 years ago
DonationFormsTable.tsx
4 years ago
DonationFormsTableRows.module.scss
4 years ago
DonationFormsTableRows.tsx
4 years ago
Pagination.js
4 years ago
Pagination.module.scss
4 years ago
DonationFormsTable.tsx
283 lines
| 1 | import {useEffect, useState} from 'react'; |
| 2 | import {__, _n} from '@wordpress/i18n'; |
| 3 | import {useSWRConfig, unstable_serialize} from 'swr'; |
| 4 | import cx from 'classnames'; |
| 5 | |
| 6 | import styles from './DonationFormsTable.module.scss'; |
| 7 | import Pagination from './Pagination.js'; |
| 8 | import DonationFormTableRows from './DonationFormsTableRows'; |
| 9 | import {Spinner} from '../../../Views/Components'; |
| 10 | import {fetchWithArgs, useDonationForms} from '../api'; |
| 11 | |
| 12 | export enum DonationStatus { |
| 13 | Any = 'any', |
| 14 | Publish = 'publish', |
| 15 | Pending = 'pending', |
| 16 | Draft = 'draft', |
| 17 | Trash = 'trash', |
| 18 | } |
| 19 | |
| 20 | interface DonationFormsTableProps { |
| 21 | statusFilter: DonationStatus; |
| 22 | search: string; |
| 23 | } |
| 24 | |
| 25 | export default function DonationFormsTable({statusFilter: status, search}: DonationFormsTableProps) { |
| 26 | const [page, setPage] = useState<number>(1); |
| 27 | const [perPage, setPerPage] = useState<number>(10); |
| 28 | const [errors, setErrors] = useState<[]>([]); |
| 29 | const [successes, setSuccesses] = useState<[]>([]); |
| 30 | const [initialLoad, setInitialLoad] = useState<boolean>(true); |
| 31 | const [loadingOverlay, setLoadingOverlay] = useState<any>(false); |
| 32 | const [errorOverlay, setErrorOverlay] = useState<any>(false); |
| 33 | const listParams = { |
| 34 | page, |
| 35 | perPage, |
| 36 | status, |
| 37 | search, |
| 38 | }; |
| 39 | const {data, error, isValidating} = useDonationForms(listParams); |
| 40 | const {mutate, cache} = useSWRConfig(); |
| 41 | const isEmpty = !error && data?.forms.length === 0; |
| 42 | useEffect(() => { |
| 43 | setPage(1); |
| 44 | }, [status, search]); |
| 45 | useEffect(() => { |
| 46 | initialLoad && data && setInitialLoad(false); |
| 47 | }, [data]); |
| 48 | useEffect(() => { |
| 49 | if (isValidating && !cache.get(unstable_serialize(listParams))) { |
| 50 | setLoadingOverlay(styles.appear); |
| 51 | } |
| 52 | if (!isValidating && loadingOverlay) { |
| 53 | setLoadingOverlay(styles.disappear); |
| 54 | const timeoutId = setTimeout(() => setLoadingOverlay(false), 100); |
| 55 | return () => clearTimeout(timeoutId); |
| 56 | } |
| 57 | }, [isValidating]); |
| 58 | useEffect(() => { |
| 59 | let timeoutId; |
| 60 | if (errors.length) { |
| 61 | setErrorOverlay(styles.appear); |
| 62 | timeoutId = setTimeout( |
| 63 | () => |
| 64 | document.getElementById(styles.updateError).scrollIntoView?.({behavior: 'smooth', block: 'center'}), |
| 65 | 100 |
| 66 | ); |
| 67 | } else if (errorOverlay) { |
| 68 | setErrorOverlay(styles.disappear); |
| 69 | timeoutId = setTimeout(() => setErrorOverlay(false), 100); |
| 70 | } |
| 71 | return () => clearTimeout(timeoutId); |
| 72 | }, [errors]); |
| 73 | |
| 74 | async function mutateForm(ids, endpoint, method) { |
| 75 | try { |
| 76 | const response = await fetchWithArgs(endpoint, {ids}, method); |
| 77 | // if we just removed the last entry from the page and we're not on the first page, go back a page |
| 78 | if ( |
| 79 | !response.errors.length && |
| 80 | data.forms.length == 1 && |
| 81 | data.totalPages > 1 && |
| 82 | (endpoint == '/delete' || endpoint == '/trash' || endpoint == '/restore') |
| 83 | ) { |
| 84 | setPage(page - 1); |
| 85 | } |
| 86 | // otherwise, revalidate current page |
| 87 | else { |
| 88 | await mutate(listParams); |
| 89 | } |
| 90 | //revalidate all pages after the current page |
| 91 | const mutations = []; |
| 92 | for (let i = page + 1; i <= data.totalPages; i++) { |
| 93 | mutations.push(mutate({...listParams, page: i})); |
| 94 | } |
| 95 | setErrors(response.errors); |
| 96 | setSuccesses(response.successes); |
| 97 | return response; |
| 98 | } catch (error) { |
| 99 | setErrors(ids.split(',')); |
| 100 | setSuccesses([]); |
| 101 | return {errors: ids.split(','), successes: []}; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return ( |
| 106 | <> |
| 107 | <div className={styles.pageActions}> |
| 108 | <Pagination |
| 109 | currentPage={page} |
| 110 | totalPages={data ? data.totalPages : 1} |
| 111 | disabled={!data} |
| 112 | totalItems={data ? parseInt(data.totalForms) : -1} |
| 113 | setPage={setPage} |
| 114 | /> |
| 115 | </div> |
| 116 | {( initialLoad && !error ) ? ( |
| 117 | <div className={styles.initialLoad}> |
| 118 | <div className={cx(styles.tableGroup)}> |
| 119 | <Spinner size={'large'} /> |
| 120 | <h2>{__('Loading donation forms', 'give')}</h2> |
| 121 | </div> |
| 122 | </div> |
| 123 | ) : ( |
| 124 | <div |
| 125 | role="group" |
| 126 | aria-labelledby="giveDonationFormsTableCaption" |
| 127 | aria-describedby="giveDonationFormsTableMessage" |
| 128 | className={styles.tableGroup} |
| 129 | > |
| 130 | <table className={styles.table}> |
| 131 | <caption id="giveDonationFormsTableCaption" className={styles.tableCaption}> |
| 132 | {__('Donation Forms', 'give')} |
| 133 | </caption> |
| 134 | <thead> |
| 135 | <tr> |
| 136 | <th scope="col" aria-sort="none" className={styles.tableColumnHeader} data-column="id"> |
| 137 | {__('ID', 'give')} |
| 138 | </th> |
| 139 | <th |
| 140 | scope="col" |
| 141 | aria-sort="none" |
| 142 | className={styles.tableColumnHeader} |
| 143 | data-column="name" |
| 144 | > |
| 145 | {__('Name', 'give')} |
| 146 | </th> |
| 147 | <th |
| 148 | scope="col" |
| 149 | aria-sort="none" |
| 150 | className={styles.tableColumnHeader} |
| 151 | data-column="amount" |
| 152 | > |
| 153 | {__('Amount', 'give')} |
| 154 | </th> |
| 155 | <th |
| 156 | scope="col" |
| 157 | aria-sort="none" |
| 158 | className={styles.tableColumnHeader} |
| 159 | data-column="goal" |
| 160 | > |
| 161 | {__('Goal', 'give')} |
| 162 | </th> |
| 163 | <th |
| 164 | scope="col" |
| 165 | aria-sort="none" |
| 166 | className={styles.tableColumnHeader} |
| 167 | data-column="donations" |
| 168 | > |
| 169 | {__('Donations', 'give')} |
| 170 | </th> |
| 171 | <th |
| 172 | scope="col" |
| 173 | aria-sort="none" |
| 174 | className={styles.tableColumnHeader} |
| 175 | data-column="revenue" |
| 176 | > |
| 177 | {__('Revenue', 'give')} |
| 178 | </th> |
| 179 | <th |
| 180 | scope="col" |
| 181 | aria-sort="none" |
| 182 | className={styles.tableColumnHeader} |
| 183 | data-column="shortcode" |
| 184 | > |
| 185 | {__('Shortcode', 'give')} |
| 186 | </th> |
| 187 | <th |
| 188 | scope="col" |
| 189 | aria-sort="ascending" |
| 190 | className={styles.tableColumnHeader} |
| 191 | data-column="date" |
| 192 | > |
| 193 | {__('Date', 'give')} |
| 194 | </th> |
| 195 | <th |
| 196 | scope="col" |
| 197 | aria-sort="none" |
| 198 | className={styles.tableColumnHeader} |
| 199 | data-column="status" |
| 200 | > |
| 201 | {__('Status', 'give')} |
| 202 | </th> |
| 203 | </tr> |
| 204 | </thead> |
| 205 | <tbody className={styles.tableContent}> |
| 206 | <DonationFormTableRows listParams={listParams} mutateForm={mutateForm} status={status} /> |
| 207 | </tbody> |
| 208 | </table> |
| 209 | {loadingOverlay && ( |
| 210 | <div className={cx(styles.overlay, loadingOverlay)}> |
| 211 | <Spinner size={'medium'} /> |
| 212 | </div> |
| 213 | )} |
| 214 | {errorOverlay && ( |
| 215 | <div className={cx(styles.overlay, errorOverlay)}> |
| 216 | <div id={styles.updateError}> |
| 217 | {!!successes.length && ( |
| 218 | <span> |
| 219 | {successes.length + |
| 220 | ' ' + |
| 221 | _n( |
| 222 | 'form was updated successfully', |
| 223 | 'forms were updated successfully.', |
| 224 | successes.length, |
| 225 | 'give' |
| 226 | )} |
| 227 | </span> |
| 228 | )} |
| 229 | <span> |
| 230 | {errors.length + |
| 231 | ' ' + |
| 232 | _n( |
| 233 | "form couldn't be updated.", |
| 234 | "forms couldn't be updated.", |
| 235 | errors.length, |
| 236 | 'give' |
| 237 | )} |
| 238 | </span> |
| 239 | <button |
| 240 | type="button" |
| 241 | className={cx('dashicons dashicons-dismiss', styles.dismiss)} |
| 242 | onClick={() => { |
| 243 | setErrors([]); |
| 244 | setSuccesses([]); |
| 245 | }} |
| 246 | > |
| 247 | <span className="give-visually-hidden">dismiss</span> |
| 248 | </button> |
| 249 | </div> |
| 250 | </div> |
| 251 | )} |
| 252 | <div id="giveDonationFormsTableMessage"> |
| 253 | {isEmpty && ( |
| 254 | <div className={styles.statusMessage}>{__('No donation forms found.', 'give')}</div> |
| 255 | )} |
| 256 | {error && ( |
| 257 | <> |
| 258 | <div className={styles.statusMessage}> |
| 259 | {__('There was a problem retrieving the donation forms.', 'give')} |
| 260 | </div> |
| 261 | <div className={styles.statusMessage}> |
| 262 | {__('Click', 'give') + ' '} |
| 263 | <a href={'edit.php?post_type=give_forms&page=give-forms'}>{__('here', 'give')}</a> |
| 264 | {' ' + __('to reload the page.')} |
| 265 | </div> |
| 266 | </> |
| 267 | )} |
| 268 | </div> |
| 269 | </div> |
| 270 | )} |
| 271 | <div className={styles.pageActions}> |
| 272 | <Pagination |
| 273 | currentPage={page} |
| 274 | totalPages={data ? data.totalPages : 1} |
| 275 | disabled={!data} |
| 276 | totalItems={data ? parseInt(data.totalForms) : -1} |
| 277 | setPage={setPage} |
| 278 | /> |
| 279 | </div> |
| 280 | </> |
| 281 | ); |
| 282 | } |
| 283 |