hooks
4 years ago
images
4 years ago
BulkActionCheckbox.tsx
4 years ago
BulkActionSelect.module.scss
4 years ago
BulkActionSelect.tsx
4 years ago
Filters.tsx
4 years ago
FormSelect.module.scss
4 years ago
FormSelect.tsx
4 years ago
Input.module.scss
4 years ago
Input.tsx
4 years ago
ListTable.module.scss
4 years ago
ListTable.tsx
4 years ago
ListTablePage.module.scss
3 years ago
ListTableRows.module.scss
3 years ago
ListTableRows.tsx
4 years ago
Pagination.module.scss
4 years ago
Pagination.tsx
4 years ago
README.MD
4 years ago
RowAction.module.scss
4 years ago
RowAction.tsx
4 years ago
Select.module.scss
4 years ago
Select.tsx
4 years ago
TableCell.module.scss
4 years ago
TableCell.tsx
4 years ago
TestLabel.module.scss
4 years ago
TestLabel.tsx
4 years ago
TypeBadge.module.scss
4 years ago
TypeBadge.tsx
4 years ago
api.ts
4 years ago
index.tsx
4 years ago
api.ts
62 lines
| 1 | import useSWR from 'swr'; |
| 2 | import lagData from './hooks/lagData'; |
| 3 | import useFallbackAsInitial from "@givewp/components/ListTable/hooks/useFallbackAsInitial"; |
| 4 | |
| 5 | export default class ListTableApi { |
| 6 | private readonly apiRoot: string; |
| 7 | private controller: AbortController|null; |
| 8 | private readonly headers: { "X-WP-Nonce": string; "Content-Type": string }; |
| 9 | private readonly swrOptions; |
| 10 | constructor({apiNonce, apiRoot, preload = null}) { |
| 11 | this.controller = null; |
| 12 | this.apiRoot = apiRoot; |
| 13 | this.headers = { |
| 14 | 'Content-Type': 'application/json', |
| 15 | 'X-WP-Nonce': apiNonce, |
| 16 | }; |
| 17 | this.swrOptions = { |
| 18 | use: [lagData], |
| 19 | onErrorRetry: (error, key, config, revalidate, { retryCount }) => { |
| 20 | //don't retry if we cancelled the initial request |
| 21 | if(error.name == 'AbortError') return; |
| 22 | if (retryCount >= 5) return; |
| 23 | const retryAfter = (retryCount + 1) * 500; |
| 24 | setTimeout(() => revalidate({ retryCount }), retryAfter); |
| 25 | } |
| 26 | }; |
| 27 | if(preload){ |
| 28 | this.swrOptions.fallbackData = preload; |
| 29 | this.swrOptions.use.push(useFallbackAsInitial); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fetchWithArgs = (endpoint, args, method = 'GET', signal = null) => { |
| 34 | const url = new URL(this.apiRoot + endpoint); |
| 35 | for (const [param, value] of Object.entries(args)) { |
| 36 | value !== '' && url.searchParams.set(param, value as string); |
| 37 | } |
| 38 | return fetch(url.href, { |
| 39 | method: method, |
| 40 | signal: signal, |
| 41 | headers: this.headers, |
| 42 | }).then((res) => { |
| 43 | if(!res.ok){ |
| 44 | throw new Error(); |
| 45 | } |
| 46 | return res.json(); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | fetcher = (params) => { |
| 51 | if(this.controller instanceof AbortController) this.controller.abort(); |
| 52 | this.controller = new AbortController(); |
| 53 | return this.fetchWithArgs('', params, 'GET', this.controller.signal); |
| 54 | } |
| 55 | |
| 56 | // SWR Fetcher |
| 57 | useListTable = ({page, perPage, ...filters}) => { |
| 58 | const {data, error, mutate, isValidating} = useSWR({page, perPage, ...filters}, this.fetcher, this.swrOptions); |
| 59 | return {data, error, mutate, isValidating}; |
| 60 | } |
| 61 | } |
| 62 |