UNPKG

synapse-react-client

Version:

[![npm version](https://badge.fury.io/js/synapse-react-client.svg)](https://badge.fury.io/js/synapse-react-client) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettie

252 lines (251 loc) 7.31 kB
import { jsxs as l, jsx as c, Fragment as u } from "react/jsx-runtime"; import * as n from "../../synapse-client/SynapseClient.js"; import { getFileHandleContent as f } from "../../synapse-client/SynapseClient.js"; import "@sage-bionetworks/synapse-client/generated/models/ErrorResponseCode"; import "@sage-bionetworks/synapse-client/generated/models/TwoFactorAuthErrorResponse"; import "@sage-bionetworks/synapse-client/util/SynapseClientError"; import "@sage-bionetworks/synapse-types"; import "../../utils/functions/EntityTypeUtils.js"; import "../../utils/SynapseConstants.js"; import "lodash-es"; import "@sage-bionetworks/synapse-client/util/synapseClientFetch"; import { SynapseContext as g } from "../../utils/context/SynapseContext.js"; import y from "@rjsf/core"; import S from "@rjsf/validator-ajv8"; import { Component as E, createRef as F } from "react"; class v extends E { static contextType = g; formRef; constructor(e) { super(e), this.formRef = F(), this.state = { isLoading: !0, successfullyUploaded: !1 }; } componentDidMount() { this.refresh(); } submitForm = () => { this.formRef.current.submit(); }; refresh = () => { if (this.context.isAuthenticated && this.context.accessToken) { const e = [ n.getUserProfile(this.context.accessToken), n.getEntity( this.context.accessToken, this.props.formSchemaEntityId ), n.getEntity( this.context.accessToken, this.props.formUiSchemaEntityId ) ]; Promise.all(e).then((t) => { const i = t[0]; this.getTargetContainer(i, this.context.accessToken).then( // @ts-ignore (s) => { const a = t[1], r = t[2]; this.getSchemaFileContent( s, a, r ); } ); }).catch((t) => { this.onError(t); }); } }; getSchemaFileContent = (e, t, i) => { const s = [ n.getFileResult( t, this.context.accessToken, !0, !0 ), n.getFileResult( i, this.context.accessToken, !0, !0 ) ]; Promise.all(s).then((a) => { try { const r = a.map((o) => f(o.fileHandle, o.preSignedURL)); Promise.all(r).then((o) => { const h = JSON.parse(o[0]), m = JSON.parse(o[1]); this.getExistingFileData( e, h, m ); }).catch((o) => { console.log("getSchemaFileContent: Error getting form content", o); }); } catch (r) { console.log("getSchemaFileContent: Error getting schema content", r); } }).catch((a) => { this.onError(a); }); }; getExistingFileData = (e, t, i) => { const a = { entityName: `${t.title}.json`, parentId: e }; let r, o; n.lookupChildEntity( a, this.context.accessToken ).then((h) => n.getEntity( this.context.accessToken, h.id ).then((m) => (o = m, this.props.initFormData ? n.getFileResult( o, this.context.accessToken, !0, !0 ).then(async (p) => { try { const d = await f( p.fileHandle, p.preSignedURL ); r = JSON.parse(d); } catch (d) { console.log("getExistingFileData: Error setting form data", d); } }) : Promise.resolve()))).finally(() => { this.setState({ formData: r, currentFileEntity: o, formSchema: t, formUiSchema: i }); }); }; getTargetContainer = async (e, t) => { const i = { entityName: e.ownerId, parentId: this.props.parentContainerId }; try { const s = await n.lookupChildEntity( i, t ); return console.log( `EntityForm uploading to https://www.synapse.org/Synapse:${s.id}` ), this.setState({ userprofile: e, containerId: s.id, isLoading: !1 }), s.id; } catch (s) { return s.status === 404 ? this.onError( new Error( "Your folder has not yet been set up, please retry in a few minutes." ) ) : this.onError(s); } }; finishedProcessing = () => { this.setState({ isLoading: !1, successfullyUploaded: !0 }); }; onError = (e) => { this.setState({ error: e, isLoading: !1, successfullyUploaded: !1 }); }; onSubmit = ({ formData: e }) => { this.setState({ isLoading: !0, successfullyUploaded: !1 }); const t = new Blob([JSON.stringify(e)], { type: "text/json" }); this.createEntityFile(t); }; createEntityFile = (e) => { const t = `${this.state.formSchema.title}.json`; n.uploadFile( this.context.accessToken, t, e ).then((i) => { const s = i.fileHandleId; if (this.state.currentFileEntity) { const r = { ...this.state.currentFileEntity, dataFileHandleId: s }; return this.setState({ currentFileEntity: r }), n.updateEntity( r, this.context.accessToken ); } const a = { parentId: this.state.containerId, name: t, concreteType: "org.sagebionetworks.repo.model.FileEntity", dataFileHandleId: s }; return n.createEntity( a, this.context.accessToken ); }).then((i) => { this.finishedProcessing(), this.props.synIdCallback && this.props.synIdCallback(i.id); }).catch((i) => { this.onError(i); }); }; render() { return /* @__PURE__ */ l("div", { children: [ this.state.error && /* @__PURE__ */ l("div", { className: "panel panel-danger errors", children: [ /* @__PURE__ */ c("div", { className: "panel-heading", children: /* @__PURE__ */ c("h3", { className: "panel-title", children: "Error" }) }), /* @__PURE__ */ c("ul", { className: "list-group", children: /* @__PURE__ */ l("li", { className: "list-group-item text-danger", children: [ this.state.error.name, " ", this.state.error.reason, this.state.error.message ] }) }) ] }), this.context.accessToken && !this.state.isLoading && !this.state.successfullyUploaded && this.state.formSchema && this.state.formUiSchema && !this.state.error && /* @__PURE__ */ c( y, { validator: S, formData: this.state.formData, schema: this.state.formSchema, uiSchema: this.state.formUiSchema, onSubmit: this.onSubmit, ref: this.formRef, children: /* @__PURE__ */ c("div", { style: { display: "none" }, children: /* @__PURE__ */ c("button", { type: "submit", className: "btn btn-info", children: "Submit" }) }) } ), !this.state.error && this.context.accessToken && this.state.isLoading && /* @__PURE__ */ l(u, { children: [ /* @__PURE__ */ c("span", { children: "Saving…" }), /* @__PURE__ */ c("span", { style: { marginLeft: "2px" }, className: "spinner" }) ] }) ] }); } } export { v as EntityForm, v as default }; //# sourceMappingURL=EntityForm.js.map