Skip to content
Snippets Groups Projects
fetchProtein.vue 2.97 KiB
Newer Older
<template>
moon's avatar
moon committed
    <slot
      :response="response"
      :loading="loading"
      :fetchError="fetchError"
moon's avatar
moon committed
    />
</template>

<script>
  // require modules
  /* eslint-disable no-unused-vars */
  const _ = require('lodash');
  let host = require('../js/const').host;
  const apikey = require('../js/const').apikey;
moon's avatar
moon committed
  name: 'FetchProtein',
moon's avatar
moon committed
  props: ['protein'],
  data() {
    return {
      response: null,
      loading: false,
moon's avatar
moon committed
      isDev: process.env.NODE_ENV === 'development',
moon's avatar
moon committed
      return this.$store.getters['User/jwt'];
moon's avatar
moon committed
  mounted() {
    const vm = this;
    // /* eslint-disable no-console */
    // console.log(vm.locus);
    vm.getItems();
  },
moon's avatar
moon committed
        const vm = this
moon's avatar
moon committed
        // /* eslint-disable no-console */
        // console.log(vm.incDesc);
moon's avatar
moon committed
        vm.loading = true
moon's avatar
moon committed
        if(vm.isDev) {
          host = require('../js/const').devHost;
        }
moon's avatar
moon committed
        let url = `${host}/proteins/${vm.protein}`;

        if (vm.protein && !_.isEmpty(vm.protein)) {
          try{
          const res = await fetch(url, {
            method: 'GET',
            mode: 'cors',
            cache: 'no-cache',
            headers: {
              Authorization: `Bearer ${apikey}`
            }
          })
moon's avatar
moon committed
          // console.log('protein res', res);
          if(!res.ok){
            vm.loading = false;
            vm.response = ''
            vm.fetchError = `${res.status} Internal Error, please write a mail to DDCode Admin.` 
            return;
          }
          vm.fetchError= '';
          const response = await res.json();
             setTimeout(() => {
              vm.loading = false;
              vm.response = response;
              // console.log(response);
              // if (response && !_.isEmpty(response)) {
              //   this.response = _.flatMap(response, (n) => [{ text: n.name, style: 'background-color: ' + n.color }]);
              // }
            }, 10);
          //   .then(response => response.json())
          //   .then((response) => {
          //     // /* eslint-disable no-console */
          //     // console.log(response);

          //   setTimeout(() => {
          //     vm.loading = false;
          //     vm.response = response;

          //     // console.log(response);
          //     // if (response && !_.isEmpty(response)) {
          //     //   this.response = _.flatMap(response, (n) => [{ text: n.name, style: 'background-color: ' + n.color }]);
          //     // }
          //   }, 10);
          // });
          }catch(err){
            vm.loading = false;
            vm.response = ''
            vm.fetchError = `${err.message} Internal Error, please write a mail to DDCode Admin.`
          }
          
moon's avatar
moon committed
        vm.response = ''
</script>

<style scoped>
moon's avatar
moon committed
    .mainContent {
        padding: 20px;
    }
</style>