summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pipeline_schedules/components/pipeline_schedules.vue
blob: 4a08a82275a33d3333b2f45a6149d99b306abe35 (plain)
1
2
3
4
5
6
7
8
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<script>
import { GlAlert, GlLoadingIcon, GlModal } from '@gitlab/ui';
import { s__, __ } from '~/locale';
import deletePipelineScheduleMutation from '../graphql/mutations/delete_pipeline_schedule.mutation.graphql';
import getPipelineSchedulesQuery from '../graphql/queries/get_pipeline_schedules.query.graphql';
import PipelineSchedulesTable from './table/pipeline_schedules_table.vue';

export default {
  i18n: {
    schedulesFetchError: s__('PipelineSchedules|There was a problem fetching pipeline schedules.'),
    scheduleDeleteError: s__(
      'PipelineSchedules|There was a problem deleting the pipeline schedule.',
    ),
  },
  modal: {
    id: 'delete-pipeline-schedule-modal',
    deleteConfirmation: s__(
      'PipelineSchedules|Are you sure you want to delete this pipeline schedule?',
    ),
    actionPrimary: {
      text: s__('PipelineSchedules|Delete pipeline schedule'),
      attributes: [{ variant: 'danger' }],
    },
    actionCancel: {
      text: __('Cancel'),
      attributes: [],
    },
  },
  components: {
    GlAlert,
    GlLoadingIcon,
    GlModal,
    PipelineSchedulesTable,
  },
  inject: {
    fullPath: {
      default: '',
    },
  },
  apollo: {
    schedules: {
      query: getPipelineSchedulesQuery,
      variables() {
        return {
          projectPath: this.fullPath,
        };
      },
      update({ project }) {
        return project?.pipelineSchedules?.nodes || [];
      },
      error() {
        this.reportError(this.$options.i18n.schedulesFetchError);
      },
    },
  },
  data() {
    return {
      schedules: [],
      hasError: false,
      errorMessage: '',
      scheduleToDeleteId: null,
      showModal: false,
    };
  },
  computed: {
    isLoading() {
      return this.$apollo.queries.schedules.loading;
    },
  },
  methods: {
    reportError(error) {
      this.hasError = true;
      this.errorMessage = error;
    },
    showDeleteModal(id) {
      this.showModal = true;
      this.scheduleToDeleteId = id;
    },
    hideModal() {
      this.showModal = false;
      this.scheduleToDeleteId = null;
    },
    async deleteSchedule() {
      try {
        const {
          data: {
            pipelineScheduleDelete: { errors },
          },
        } = await this.$apollo.mutate({
          mutation: deletePipelineScheduleMutation,
          variables: { id: this.scheduleToDeleteId },
        });

        if (errors.length > 0) {
          throw new Error();
        } else {
          this.$apollo.queries.schedules.refetch();
        }
      } catch {
        this.reportError(this.$options.i18n.scheduleDeleteError);
      }
    },
  },
};
</script>

<template>
  <div>
    <gl-alert v-if="hasError" class="gl-mb-2" variant="danger" @dismiss="hasError = false">
      {{ errorMessage }}
    </gl-alert>

    <gl-loading-icon v-if="isLoading" size="lg" />

    <!-- Tabs will be addressed in #371989 -->

    <template v-else>
      <pipeline-schedules-table :schedules="schedules" @showDeleteModal="showDeleteModal" />

      <gl-modal
        :visible="showModal"
        :title="$options.modal.actionPrimary.text"
        :modal-id="$options.modal.id"
        :action-primary="$options.modal.actionPrimary"
        :action-cancel="$options.modal.actionCancel"
        size="sm"
        @primary="deleteSchedule"
        @hide="hideModal"
      >
        {{ $options.modal.deleteConfirmation }}
      </gl-modal>
    </template>
  </div>
</template>