ZmenuInstantDownload.tsx 2.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
import React from 'react';

import useApiService from 'src/shared/hooks/useApiService';

import { InstantDownloadTranscript } from 'src/shared/components/instant-download';
import { CircleLoader } from 'src/shared/components/loader/Loader';
import { TranscriptInResponse } from 'src/content/app/entity-viewer/shared/rest/rest-data-fetchers/transcriptData';

import { LoadingState } from 'src/shared/types/loading-state';

import styles from './Zmenu.scss';

type Props = {
  id: string;
};

const ZmenuInstantDownload = (props: Props) => {
34
  const genomeId = getGenomeId(props.id);
35 36 37 38 39 40 41 42 43
  const transcriptId = getStableId(props.id);
  const params = {
    endpoint: `/lookup/id/${transcriptId}?content-type=application/json;expand=1`,
    host: 'https://rest.ensembl.org'
  };
  const { loadingState, data, error } = useApiService<TranscriptInResponse>(
    params
  );

44 45 46 47
  if (
    loadingState === LoadingState.NOT_REQUESTED ||
    loadingState === LoadingState.LOADING
  ) {
48 49 50 51 52 53 54 55 56 57 58 59 60 61
    return (
      <div className={styles.zmenuInstantDowloadLoading}>
        <CircleLoader />
      </div>
    );
  }

  if (error) {
    // TODO: decide how we handle errors in this case
    return null;
  }

  return (
    <InstantDownloadTranscript
62
      genomeId={genomeId}
63 64 65 66 67 68 69
      {...preparePayload(data as TranscriptInResponse)}
      layout="vertical"
    />
  );
};

// TODO: we may want to move this to a common helper file that deals with messaging with Genome Browser
70
const getGenomeId = (id: string) => id.split(':').shift();
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
const getStableId = (id: string) => id.split(':').pop();

const preparePayload = (transcript: TranscriptInResponse) => {
  const geneId = transcript.Parent;
  const transcriptId = transcript.id;
  const so_term = transcript.biotype;

  return {
    transcript: {
      id: transcriptId,
      so_term
    },
    gene: {
      id: geneId
    }
  };
};

export default ZmenuInstantDownload;