Skip to main content

Infrastructure hooks

Hooks for managing infrastructure data throughout the application. Built on SWR for caching and revalidation, with mutation hooks for create, update, and delete operations. All hooks are accessed through the core hooks provider and delegate to an ApiAdapter for actual API calls.

Hooks

HookDescription
useInfrastructuresFetches all infrastructure records
useInfrastructureFetches a single infrastructure by ID, with update capability
useCreateInfrastructureCreates a new infrastructure record
useDeleteInfrastructureDeletes an infrastructure record by ID

useInfrastructures

Fetches the complete list of infrastructure records. Returns an empty array while loading or if no data exists.

Signature

function useInfrastructures(): UseInfrastructuresReturn

Parameters

None.

Returns

FieldTypeDescription
infrastructuresInfrastructure[]Array of infrastructure records, defaults to empty array
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state

Example

const { infrastructures, isLoading, isError } = useInfrastructures();

if (isLoading) return <Skeleton />;
if (isError) return <ErrorMessage />;

return <InfrastructureList items={infrastructures} />;

useInfrastructure

Fetches a single infrastructure record by ID. Also provides an update mutation that revalidates both the individual record and the list cache on success.

Signature

function useInfrastructure(infrastructureId: number): UseInfrastructureReturn

Parameters

ParamTypeRequiredDescription
infrastructureIdnumberYesThe ID of the infrastructure to fetch

Returns

FieldTypeDescription
infrastructureInfrastructure | nullThe infrastructure record, or null if not loaded
isLoadingbooleanSWR loading state
isErrorError | undefinedSWR error state
updateInfrastructure(arg: Partial<Infrastructure>) => Promise<Infrastructure>Mutation trigger to update the record
isMutatingbooleanWhether an update is in progress
updateErrorError | undefinedError from the last update attempt
updatedDataInfrastructure | undefinedResponse data from successful update

Example

const { infrastructure, isLoading, updateInfrastructure, isMutating } = useInfrastructure(42);

const handleSave = async (changes: Partial<Infrastructure>) => {
await updateInfrastructure(changes);
};

useCreateInfrastructure

Creates a new infrastructure record. Revalidates the infrastructure list cache on success.

Signature

function useCreateInfrastructure(): UseCreateInfrastructureReturn

Parameters

None.

Returns

FieldTypeDescription
createInfrastructure(arg: Partial<Infrastructure>) => Promise<Infrastructure>Mutation trigger to create a record
isMutatingbooleanWhether creation is in progress
createErrorError | undefinedError from the last create attempt
createdDataInfrastructure | undefinedThe newly created infrastructure record

Example

const { createInfrastructure, isMutating, createError } = useCreateInfrastructure();

const handleCreate = async (data: Partial<Infrastructure>) => {
const created = await createInfrastructure(data);
router.push(`/infrastructures/${created.id}`);
};

useDeleteInfrastructure

Deletes an infrastructure record by ID. Manually revalidates the infrastructure list cache after deletion.

Signature

function useDeleteInfrastructure(infrastructureId?: number): UseDeleteInfrastructureReturn

Parameters

ParamTypeRequiredDescription
infrastructureIdnumberNoOptional ID used for SWR cache key

Returns

FieldTypeDescription
deleteInfrastructure(id: number) => Promise<void>Function to delete an infrastructure by ID
isMutatingbooleanWhether deletion is in progress
deleteErrorError | undefinedError from the last delete attempt
deletedDataunknownResponse data from successful deletion

Example

const { deleteInfrastructure, isMutating } = useDeleteInfrastructure();

const handleDelete = async (id: number) => {
if (confirm("Delete this infrastructure?")) {
await deleteInfrastructure(id);
}
};