UNPKG

synapse-react-client

Version:

[![Build Status](https://travis-ci.com/Sage-Bionetworks/Synapse-React-Client.svg?branch=main)](https://travis-ci.com/Sage-Bionetworks/Synapse-React-Client) [![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synaps

132 lines 7.3 kB
"use strict"; /* * Hooks to access Entity Services in Synapse */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useUpdateViaJson = exports.useGetJson = exports.removeStandardEntityFields = exports.getStandardEntityFields = exports.useGetVersionsInfinite = exports.useGetVersions = exports.useUpdateEntity = exports.useGetEntity = void 0; var tslib_1 = require("tslib"); var lodash_es_1 = require("lodash-es"); var react_1 = require("react"); var react_query_1 = require("react-query"); var __1 = require("../.."); var SynapseContext_1 = require("../../SynapseContext"); var synapseTypes_1 = require("../../synapseTypes"); function useGetEntity(entityId, versionNumber, options) { var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; return (0, react_query_1.useQuery)(['entity', entityId, 'entity', versionNumber], function () { return __1.SynapseClient.getEntity(accessToken, entityId, versionNumber === null || versionNumber === void 0 ? void 0 : versionNumber.toString()); }, options); } exports.useGetEntity = useGetEntity; function useUpdateEntity(options) { var _this = this; var queryClient = (0, react_query_1.useQueryClient)(); var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; return (0, react_query_1.useMutation)(function (entity) { return __1.SynapseClient.updateEntity(entity, accessToken); }, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { onSuccess: function (updatedEntity, variables, ctx) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, queryClient.invalidateQueries(['entity', updatedEntity.id, 'entity', undefined], { exact: false, })]; case 1: _a.sent(); queryClient.setQueryData(['entity', updatedEntity.id, 'entity', undefined], updatedEntity); if (!(options === null || options === void 0 ? void 0 : options.onSuccess)) return [3 /*break*/, 3]; return [4 /*yield*/, options.onSuccess(updatedEntity, variables, ctx)]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); } })); } exports.useUpdateEntity = useUpdateEntity; function useGetVersions(entityId, offset, limit, options) { if (offset === void 0) { offset = 0; } if (limit === void 0) { limit = 200; } var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; return (0, react_query_1.useQuery)(['entity', entityId, 'versions', { offset: offset, limit: limit }], function () { return __1.SynapseClient.getEntityVersions(entityId, accessToken, offset, limit); }, options); } exports.useGetVersions = useGetVersions; function useGetVersionsInfinite(entityId, options) { var _this = this; var LIMIT = 200; var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; return (0, react_query_1.useInfiniteQuery)(['entity', entityId, 'versions', 'infinite'], function (context) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, __1.SynapseClient.getEntityVersions(entityId, accessToken, context.pageParam, LIMIT)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); }, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { getNextPageParam: function (lastPage, pages) { if (lastPage.results.length > 0) return pages.length * LIMIT; //set the new offset to (page * limit) else return undefined; } })); } exports.useGetVersionsInfinite = useGetVersionsInfinite; function getStandardEntityFields(json) { return (0, lodash_es_1.pick)(json, synapseTypes_1.entityJsonKeys); } exports.getStandardEntityFields = getStandardEntityFields; function removeStandardEntityFields(json) { return (0, lodash_es_1.omit)(json, synapseTypes_1.entityJsonKeys); } exports.removeStandardEntityFields = removeStandardEntityFields; /** * In addition to the standard objects returned by {@link useQuery}, this hook also returns the following objects: * - `entityMetadata`: the standard entity fields contained in the JSON response * - `annotations`: the annotations contained in the JSON response * * The union of these objects would be equal in value to the `data` object. */ function useGetJson(entityId, options) { var _a = (0, react_1.useState)(), entityMetadata = _a[0], setEntityMetadata = _a[1]; var _b = (0, react_1.useState)(), annotations = _b[0], setAnnotations = _b[1]; var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; var query = (0, react_query_1.useQuery)([accessToken, 'entity', entityId, 'json'], function () { return __1.SynapseClient.getEntityJson(entityId, accessToken); }, options); // Separate the standard fields and annotations. (0, react_1.useEffect)(function () { if (query.data) { setEntityMetadata(getStandardEntityFields(query.data)); setAnnotations(removeStandardEntityFields(query.data)); } }, [query.data]); return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, query), { entityMetadata: entityMetadata, annotations: annotations }); } exports.useGetJson = useGetJson; function useUpdateViaJson(options) { var _this = this; var queryClient = (0, react_query_1.useQueryClient)(); var accessToken = (0, SynapseContext_1.useSynapseContext)().accessToken; return (0, react_query_1.useMutation)(function (json) { var entityId = json.id; return __1.SynapseClient.updateEntityJson(entityId, json, accessToken); }, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, options), { onSuccess: function (data, variables, ctx) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () { var entityId; return (0, tslib_1.__generator)(this, function (_a) { switch (_a.label) { case 0: entityId = data.id; return [4 /*yield*/, queryClient.invalidateQueries([accessToken, 'entity', entityId], { exact: false, })]; case 1: _a.sent(); queryClient.setQueryData([accessToken, 'entity', entityId, 'json'], data); if (!(options === null || options === void 0 ? void 0 : options.onSuccess)) return [3 /*break*/, 3]; return [4 /*yield*/, options.onSuccess(data, variables, ctx)]; case 2: _a.sent(); _a.label = 3; case 3: return [2 /*return*/]; } }); }); } })); } exports.useUpdateViaJson = useUpdateViaJson; //# sourceMappingURL=useEntity.js.map