Skip to main content

useOpenDataPortals hooks

These hooks provide read access to open data portal records, supporting queries by ID, municipality, country subdivision, dataset group, and name. All hooks use SWR for caching and revalidation, and are created via a factory function that accepts an ApiAdapter for dependency injection.

Hooks

HookDescription
useOpenDataPortalsFetches all open data portals
useOpenDataPortalByIdFetches a single portal by numeric ID
useOpenDataPortalsByMunicipalityFetches portals filtered by municipality name
useOpenDataPortalsByMunicipalityAndCountrySubdivisionFetches portals filtered by both municipality and country subdivision
useOpenDataPortalsByCountrySubdivisionFetches portals filtered by country subdivision (province/territory)
useOpenDataPortalsByGroupFetches portals filtered by dataset group
useOpenDataPortalsByNameFetches portals filtered by portal name

useOpenDataPortals

Fetches all open data portal records. Returns an empty array while loading or on error.

Signature

function useOpenDataPortals(): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

None.

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Array of portal records, defaults to []
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { openDataPortals, isLoading } = useOpenDataPortals();

if (isLoading) return <Skeleton />;

return (
<ul>
{openDataPortals.map((portal) => (
<li key={portal.id}>{portal.name}</li>
))}
</ul>
);

useOpenDataPortalById

Fetches a single open data portal by its numeric ID. Passes null as the SWR key when id is null, preventing the fetch.

Signature

function useOpenDataPortalById(id: number | null): {
openDataPortal: OpenDataPortal | null;
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
idnumber | nullYesPortal ID to fetch, or null to skip

Returns

FieldTypeDescription
openDataPortalOpenDataPortal | nullPortal record or null if not found/loading
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { openDataPortal, isLoading } = useOpenDataPortalById(selectedPortalId);

if (!openDataPortal) return null;

useOpenDataPortalsByMunicipality

Fetches portals filtered by municipality name. Skips the request when municipality is null.

Signature

function useOpenDataPortalsByMunicipality(municipality: string | null): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
municipalitystring | nullYesMunicipality name to filter by, or null to skip

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Filtered portal records
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

useOpenDataPortalsByMunicipalityAndCountrySubdivision

Fetches portals filtered by both municipality and country subdivision. Skips the request when either parameter is null.

Signature

function useOpenDataPortalsByMunicipalityAndCountrySubdivision(
municipality: string | null,
countrySubdivision: string | null
): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
municipalitystring | nullYesMunicipality name to filter by
countrySubdivisionstring | nullYesProvince/territory code to filter by

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Filtered portal records
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

useOpenDataPortalsByCountrySubdivision

Fetches portals filtered by country subdivision (province/territory). Skips the request when countrySubdivision is null.

Signature

function useOpenDataPortalsByCountrySubdivision(countrySubdivision: string | null): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
countrySubdivisionstring | nullYesProvince/territory code to filter by

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Filtered portal records
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

useOpenDataPortalsByGroup

Fetches portals filtered by dataset group. Skips the request when group is null.

Signature

function useOpenDataPortalsByGroup(group: DatasetGroup | null): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
groupDatasetGroup | nullYesDataset group enum value to filter by

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Filtered portal records
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

useOpenDataPortalsByName

Fetches portals filtered by portal name. Skips the request when name is null.

Signature

function useOpenDataPortalsByName(name: string | null): {
openDataPortals: OpenDataPortal[];
isLoading: boolean;
isError: Error | undefined;
}

Parameters

ParamTypeRequiredDescription
namestring | nullYesPortal name to search for

Returns

FieldTypeDescription
openDataPortalsOpenDataPortal[]Filtered portal records
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state