Skip to content

Talking to APIs

Martin Hradil edited this page Jun 23, 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 { useHubView } from '../useHubView';
import { hubAPI, pulpIdKeyFn } from '../api';


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

  const view = useHubView<Approval>({
    url: hubAPI`/_ui/v1/collection-versions/`,
    keyFn: pulpIdKeyFn, // how to tell items apart: id (idKeyFn), pulp_id (pulpIdKeyFn) , pulp_href (pulpHrefKeyFn), name (nameKeyFn)
    toolbarFilters,
    tableColumns,
    queryParams: { 'foo': 'bar' }, // extra params
  });

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


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

  const view = usePulpView<Task>({
    url: pulpAPI`/tasks/`,
    keyFn: pulpHrefKeyFn,
    toolbarFilters,
    tableColumns
  });

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

under the hood, these use useSWR (via framework/useView) 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