app.vue 2.6 KB
Newer Older
P
Phil Hughes 已提交
1 2 3 4 5 6 7
<script>
import Visibility from 'visibilityjs';
import Poll from '../../lib/utils/poll';
import Service from '../services/index';
import Store from '../stores';
import titleComponent from './title.vue';
import descriptionComponent from './description.vue';
8
import editedComponent from './edited.vue';
P
Phil Hughes 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

export default {
  props: {
    endpoint: {
      required: true,
      type: String,
    },
    canUpdate: {
      required: true,
      type: Boolean,
    },
    issuableRef: {
      type: String,
      required: true,
    },
    initialTitle: {
      type: String,
      required: true,
    },
    initialDescriptionHtml: {
      type: String,
      required: false,
      default: '',
    },
    initialDescriptionText: {
      type: String,
      required: false,
      default: '',
    },
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    updatedAt: {
      type: String,
      required: false,
      default: '',
    },
    updatedByName: {
      type: String,
      required: false,
      default: '',
    },
    updatedByPath: {
      type: String,
      required: false,
      default: '',
    },
P
Phil Hughes 已提交
53 54 55
  },
  data() {
    const store = new Store({
56
      titleHtml: this.initialTitle,
P
Phil Hughes 已提交
57 58
      descriptionHtml: this.initialDescriptionHtml,
      descriptionText: this.initialDescriptionText,
59 60 61
      updatedAt: this.updatedAt,
      updatedByName: this.updatedByName,
      updatedByPath: this.updatedByPath,
P
Phil Hughes 已提交
62 63 64 65 66 67 68 69 70 71
    });

    return {
      store,
      state: store.state,
    };
  },
  components: {
    descriptionComponent,
    titleComponent,
72
    editedComponent,
P
Phil Hughes 已提交
73
  },
74 75 76 77 78
  computed: {
    hasUpdated() {
      return !!this.state.updatedAt;
    },
  },
P
Phil Hughes 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  created() {
    const resource = new Service(this.endpoint);
    const poll = new Poll({
      resource,
      method: 'getData',
      successCallback: (res) => {
        this.store.updateState(res.json());
      },
      errorCallback(err) {
        throw new Error(err);
      },
    });

    if (!Visibility.hidden()) {
      poll.makeRequest();
    }

    Visibility.change(() => {
      if (!Visibility.hidden()) {
        poll.restart();
      } else {
        poll.stop();
      }
    });
  },
};
</script>

<template>
  <div>
    <title-component
      :issuable-ref="issuableRef"
      :title-html="state.titleHtml"
      :title-text="state.titleText" />
    <description-component
114
      v-if="state.descriptionHtml"
P
Phil Hughes 已提交
115 116 117 118
      :can-update="canUpdate"
      :description-html="state.descriptionHtml"
      :description-text="state.descriptionText"
      :task-status="state.taskStatus" />
119
    <edited-component
120
      v-if="hasUpdated"
121 122 123 124
      :updated-at="state.updatedAt"
      :updated-by-name="state.updatedByName"
      :updated-by-path="state.updatedByPath"
    />
P
Phil Hughes 已提交
125 126
  </div>
</template>