Skip to content

Commit

Permalink
refactor(types): 🏷️ Update types in useHalResource
Browse files Browse the repository at this point in the history
Update useHalResource hook with the new types
  • Loading branch information
marcialfps committed Jan 22, 2024
1 parent 183f0d7 commit 2a235da
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/src/hooks/useHalResource.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useState, useEffect } from "react";
import { HalApiCaller } from "@dxc-technology/halstack-client";
import { ErrorResponse, RequestStatus, UseHalResource, UseHalResourceResponse } from "./types";
import {
ErrorResponse,
HalResourceType,
Interactions,
RequestStatus,
UseHalResource,
UseHalResourceResponse,
} from "./types";

const buildErrorResponse = (error) => {
const errorResponse: ErrorResponse = {
Expand All @@ -21,8 +28,8 @@ const useHalResource = ({
}: UseHalResource): UseHalResourceResponse => {
const [status, setStatus] = useState<RequestStatus>("idle");
const [error, setError] = useState<ErrorResponse>();
const [resource, setResource] = useState<object>(null);
const [interactions, setInteractions] = useState<object>(null);
const [resource, setResource] = useState<HalResourceType>();
const [interactions, setInteractions] = useState<Interactions>();

useEffect(() => {
const getInteractionHandler = (method, resourceSelf, methodHref) => {
Expand Down Expand Up @@ -65,7 +72,7 @@ const useHalResource = ({
};
};

const getInteractions = (halResource) => {
const getInteractions = (halResource: HalResourceType) => {
return halResource
.getInteractions()
.map((interaction) => ({
Expand Down Expand Up @@ -98,15 +105,15 @@ const useHalResource = ({
setInteractions(getInteractions(response.halResource));
setStatus("resolved");
} else {
setResource(null);
setResource(undefined);
const errorResponse = buildErrorResponse({
message: "Response does not contain a valid HAL resource",
});
setError(errorResponse);
setStatus("rejected");
}
} catch (err) {
setResource(null);
setResource(undefined);
setError(buildErrorResponse(err));
setStatus("rejected");
}
Expand Down

0 comments on commit 2a235da

Please sign in to comment.