From 9ae1966a4c7b835093f69e44c2f17a31e9415a67 Mon Sep 17 00:00:00 2001 From: Olmo del Corral Date: Wed, 20 Feb 2019 23:27:18 +0100 Subject: [PATCH] add useAPI --- Signum.React/Scripts/Services.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Signum.React/Scripts/Services.ts b/Signum.React/Scripts/Services.ts index b0ab37ee57..2fb47f8798 100644 --- a/Signum.React/Scripts/Services.ts +++ b/Signum.React/Scripts/Services.ts @@ -1,5 +1,6 @@ import { ModelState } from './Signum.Entities' import { GraphExplorer } from './Reflection' +import * as React from 'react' export interface AjaxOptions { url: string; @@ -383,3 +384,24 @@ export class AbortableRequest { }) as Promise; } } + + +export function useAPI(defaultValue: T, key: ReadonlyArray | undefined, makeCall: (signal: AbortSignal) => Promise): T { + + const [data, updateData] = React.useState(defaultValue) + + React.useEffect(() => { + var abortController = new AbortController(); + + makeCall(abortController.signal) + .then(result => updateData(result)) + .done(); + + return () => { + abortController.abort(); + } + }, key); + + return data; + +}