react-ketting
Version:
Ketting bindings for React
49 lines • 1.87 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.useResourceSimple = void 0;
const use_read_resource_1 = require("./use-read-resource");
/**
* useResourceSimple is a useResource replacement.
*
* It currently exists as a basis for experimentation to reduce the complexity of useResource.
* The main thing that's not implemented is support for 'POST'
*/
function useResourceSimple(resourceLike, options) {
if (resourceLike === undefined) {
console.warn('useCollection was called with "undefined" as the "resourceLike" argument. This is a bug. Did you forget to wait for \'loading\' to complete somewhere?');
}
const { resourceState, loading, error, resource } = (0, use_read_resource_1.useReadResource)(resourceLike, {
refreshOnStale: options === null || options === void 0 ? void 0 : options.refreshOnStale,
});
const setResourceState = (newState) => {
if (!resource) {
throw new Error('Too early to call setResourceState, we don\'t have a current state to update');
}
resource.updateCache(newState);
};
const submit = async () => {
if (!resourceState || !resource) {
throw new Error('Too early to call submit()');
}
await resource.put(resourceState);
};
const setData = (newData) => {
if (!resourceState || !resource) {
throw new Error('Too early to call setData, we don\'t have a current state to update');
}
resourceState.data = newData;
setResourceState(resourceState);
};
return {
loading,
error,
data: resourceState.data,
setData,
resourceState,
setResourceState,
resource,
submit,
};
}
exports.useResourceSimple = useResourceSimple;
//# sourceMappingURL=use-resource-simple.js.map
;