From 2a235da6ffb25abdf839f20c82d6b5a37fb6d44a Mon Sep 17 00:00:00 2001 From: marcialfps Date: Mon, 22 Jan 2024 13:19:45 +0100 Subject: [PATCH] refactor(types): :label: Update types in useHalResource Update useHalResource hook with the new types --- lib/src/hooks/useHalResource.tsx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/src/hooks/useHalResource.tsx b/lib/src/hooks/useHalResource.tsx index 21874a3..2ab5792 100644 --- a/lib/src/hooks/useHalResource.tsx +++ b/lib/src/hooks/useHalResource.tsx @@ -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 = { @@ -21,8 +28,8 @@ const useHalResource = ({ }: UseHalResource): UseHalResourceResponse => { const [status, setStatus] = useState("idle"); const [error, setError] = useState(); - const [resource, setResource] = useState(null); - const [interactions, setInteractions] = useState(null); + const [resource, setResource] = useState(); + const [interactions, setInteractions] = useState(); useEffect(() => { const getInteractionHandler = (method, resourceSelf, methodHref) => { @@ -65,7 +72,7 @@ const useHalResource = ({ }; }; - const getInteractions = (halResource) => { + const getInteractions = (halResource: HalResourceType) => { return halResource .getInteractions() .map((interaction) => ({ @@ -98,7 +105,7 @@ 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", }); @@ -106,7 +113,7 @@ const useHalResource = ({ setStatus("rejected"); } } catch (err) { - setResource(null); + setResource(undefined); setError(buildErrorResponse(err)); setStatus("rejected"); }