import React, { createRef } from "react"; import ShareURLComponent from "./share_url"; import _ from "underscore"; /** * Takes sequence accession as props, fetches the sequence from the server, and * displays it in a modal. */ export default class CloudShareModal extends React.Component { constructor(props) { super(props); this.state = { formState: 'input', // Possible values: 'input', 'loading', 'results', 'error' errorMessages: [], email: '', agreeToTos: false, shareableurl: '', }; this.modalRef = createRef(); } // Lifecycle methods. // handleChange = (e) => { const { name, value, type, checked } = e.target; const inputValue = type === 'checkbox' ? checked : value; this.setState({ [name]: inputValue }); } handleSubmit = (e) => { e.preventDefault(); const { email } = this.state; const regex = /\/([^/]+)(?:\/|#|\?|$)/; const match = window.location.pathname.match(regex); const jobId = match[1]; this.setState({ formState: 'loading' }); const requestData = { job_id: jobId, sender_email: email }; fetch('/cloud_share', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(requestData) }) .then(response => response.json()) .then(data => { if (data.shareable_url) { // Successful response this.setState({ formState: 'results', shareableurl: data.shareable_url }); } else if (data.errors) { // Error response with specific error messages const errorMessages = data.errors; this.setState({ formState: 'error', errorMessages }); } else { // Generic error message throw new Error('Unknown error submitting form'); } }) .catch(error => { this.setState({ formState: 'error', errorMessages: [error.message] }); }); } renderLoading() { return (

Uploading the job to SequenceServer Cloud, please wait...

); } renderResults() { const { shareableurl } = this.state; return ; } renderError() { const { errorMessages } = this.state; return ( <> { _.map( errorMessages, _.bind(function (errorMessage) { return (
{errorMessage}
); }, this) ) } {this.renderForm()} ); } renderForm() { const { email, agreeToTos } = this.state; const isSubmitDisabled = !agreeToTos; return(

By submitting this form you agree to upload this SequenceServer result set to SenquenceServer Cloud , where it will become available on the internet to everyone with the link. You also agree that your email address will be stored on SequenceServer databases as proof of authentication for support and similar purposes.

) } render() { const { formState } = this.state; let content; switch (formState) { case 'loading': content = this.renderLoading(); break; case 'results': content = this.renderResults(); break; case 'error': content = this.renderError(); break; case 'input': default: content = this.renderForm(); break; } return (

Share to SequenceServer Cloud

{content}
); } /* * Returns jQuery reference to the main modal container. */ modal() { return $(this.modalRef.current); } /** * Shows share dialogue. */ show() { this.setState({ requestCompleted: false }, () => { this.modal().modal("show"); }); } /** * Hide share dialogue. */ hide() { this.modal().modal("hide"); } }