Skip to content

Talking to APIs

Martin Hradil edited this page Jun 20, 2023 · 7 revisions
requestGet, postRequest, requestDelete, patchRequest, use*Request

these just do a http request (using ky) with the right csrf token use in actions and other hooks

use*Request seem to be similar to the above, with more error handling? eg. useGetRequest returns a function (url, query?) for use by useSWR

useGet

useGet takes (url, query?, refreshInterval?) and returns { data, error, refresh, isLoading } - using useSWR

use in detail screen

use*View

use in list screens

import { idKeyFn, useHubView } from '../useHubView';
import { hubAPI } from '../api';


  // const toolbarFilters = useApprovalFilters();
  // const tableColumns = useApprovalsColumns();

  const view = useHubView<Approval>(
    hubAPI`/_ui/v1/collection-versions/`,
    idKeyFn,
    toolbarFilters,
    tableColumns
  );

  // <PageTable {...view} />
import { getIdFromPulpHref, usePulpView } from '../usePulpView';
import { pulpAPI } from '../api';


  // const toolbarFilters = useTaskFilters();
  // const tableColumns = useTaskColumns();

  const view = usePulpView<Task>(
    pulpAPI`/tasks/`,
    pulpHRefKeyFn,
    toolbarFilters,
    tableColumns
  );

  // <PageTable {...view} />

under the hood, these use useSWR and the view returns:

// from useView
page, setPage, perPage, setPerPage,
sort, setSort, sortDirection, setSortDirection,
filters, setFilters, clearAllFilters,
// from useSelected
selectedItems, selectItem, unselectItem, isSelected,
selectItems, selectAll, unselectAll, allSelected,
keyFn, unselectItems,
// only in useHubView
unselectItemsAndRefresh,
// common
error
itemCount
pageItems
refresh
Clone this wiki locally