All files / ee/app/assets/javascripts/oncall_schedules/components/rotations/components delete_rotation_modal.vue

5.55% Statements 1/18
0% Branches 0/3
0% Functions 0/11
5.88% Lines 1/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                5x                                                                                                                                                                                                                                                        
<script>
import { GlSprintf, GlModal, GlAlert } from '@gitlab/ui';
import { isEmpty } from 'lodash';
import destroyOncallRotationMutation from 'ee/oncall_schedules/graphql/mutations/destroy_oncall_rotation.mutation.graphql';
import getOncallSchedulesQuery from 'ee/oncall_schedules/graphql/queries/get_oncall_schedules.query.graphql';
import { updateStoreAfterRotationDelete } from 'ee/oncall_schedules/utils/cache_updates';
import { s__, __ } from '~/locale';
 
export const i18n = {
  deleteRotation: s__('OnCallSchedules|Delete rotation'),
  deleteRotationMessage: s__(
    'OnCallSchedules|Are you sure you want to delete the "%{deleteRotation}" rotation? This action cannot be undone.',
  ),
  cancel: __('Cancel'),
};
 
export default {
  i18n,
  components: {
    GlSprintf,
    GlModal,
    GlAlert,
  },
  inject: ['projectPath'],
  props: {
    rotation: {
      type: Object,
      required: true,
      validator: (rotation) =>
        isEmpty(rotation) || [rotation.id, rotation.name, rotation.startsAt].every(Boolean),
    },
    schedule: {
      type: Object,
      required: true,
    },
    modalId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      loading: false,
      error: '',
    };
  },
  computed: {
    primaryProps() {
      return {
        text: this.$options.i18n.deleteRotation,
        attributes: { category: 'primary', variant: 'danger', loading: this.loading },
      };
    },
    cancelProps() {
      return {
        text: this.$options.i18n.cancel,
      };
    },
    rotationDeleteModalTestId() {
      return `delete-rotation-modal-${this.rotation.id}`;
    },
  },
  methods: {
    deleteRotation() {
      const {
        projectPath,
        rotation: { id },
        schedule: { iid },
      } = this;
 
      this.loading = true;
      this.$apollo
        .mutate({
          mutation: destroyOncallRotationMutation,
          variables: {
            id,
            scheduleIid: iid,
            projectPath,
          },
          update(store, { data }) {
            updateStoreAfterRotationDelete(
              store,
              getOncallSchedulesQuery,
              { ...data, scheduleIid: iid },
              {
                projectPath,
              },
            );
          },
        })
        .then(({ data: { oncallRotationDestroy } = {} } = {}) => {
          const error = oncallRotationDestroy.errors[0];
          Iif (error) {
            throw error;
          }
          this.$emit('rotation-deleted');
          this.$refs.deleteRotationModal.hide();
        })
        .catch((error) => {
          this.error = error;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    hideErrorAlert() {
      this.error = '';
    },
  },
};
</script>
 
<template>
  <gl-modal
    ref="deleteRotationModal"
    :modal-id="modalId"
    size="sm"
    :data-testid="rotationDeleteModalTestId"
    :title="$options.i18n.deleteRotation"
    :action-primary="primaryProps"
    :action-cancel="cancelProps"
    @primary.prevent="deleteRotation"
    @cancel="$emit('set-rotation-to-update', {})"
  >
    <gl-alert v-if="error" variant="danger" class="gl-mt-n3 gl-mb-3" @dismiss="hideErrorAlert">
      {{ error || $options.i18n.errorMsg }}
    </gl-alert>
    <gl-sprintf :message="$options.i18n.deleteRotationMessage">
      <template #deleteRotation>{{ rotation.name }}</template>
    </gl-sprintf>
  </gl-modal>
</template>