Skip to content
Snippets Groups Projects
Commit 0e8f16d3 authored by Loïc Correnson's avatar Loïc Correnson
Browse files

[ivette/states] cached GET requests

parent 1a6c23c9
No related branches found
No related tags found
No related merge requests found
......@@ -122,6 +122,40 @@ export function useState(id)
return [ value , (v) => setValue(id,project,v) ];
}
// --------------------------------------------------------------------------
// --- Cached GET Requests
// --------------------------------------------------------------------------
/**
@summary Cached GET request (Custom React Hook).
@param {string} rq - GET request name
@param {any} [params] - GET request parameter
@param {boolean} [cancel] - Cancel value when updating (default is `false`)
@return {any} [result] GET reequest response (when available)
@description
Sends the specified GET request and returns its result.
The request is send asynchronously and cached until any change in
`rq`, `params`, current project or server activity.
The result can be `undefined` when the Server is off or until
the server response has been actually received
(first request or `cancel=true`).
*/
export function useRequest( rq, params, cancel=false )
{
let project = useProject();
let [ value, setValue ] = React.useState();
React.useEffect( () => {
if (project) {
if (cancel) setValue(undefined);
Server.sendGET( rq , params ).then(setValue);
} else {
if (value !== undefined) setValue(undefined);
}
} , [ project, rq, JSON.stringify(params) ] );
return value;
}
// --------------------------------------------------------------------------
// --- Synchronized States
// --------------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment