All files / app/assets/javascripts/members/components/modals leave_modal.vue

93.33% Statements 14/15
100% Branches 5/5
100% Functions 8/8
93.33% Lines 14/15

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    13x                                                                                               11x       11x     11x               4x 1x     3x 3x           11x 4x     7x               11x     11x         1x                                                                    
<script>
import { GlModal, GlForm, GlSprintf, GlTooltipDirective } from '@gitlab/ui';
// eslint-disable-next-line no-restricted-imports
import { mapState } from 'vuex';
import csrf from '~/lib/utils/csrf';
import { __, s__, sprintf } from '~/locale';
import UserDeletionObstaclesList from '~/vue_shared/components/user_deletion_obstacles/user_deletion_obstacles_list.vue';
import { parseUserDeletionObstacles } from '~/vue_shared/components/user_deletion_obstacles/utils';
import {
  LEAVE_MODAL_ID,
  MEMBER_MODEL_TYPE_GROUP_MEMBER,
  MEMBER_MODEL_TYPE_PROJECT_MEMBER,
} from '../../constants';
 
export default {
  name: 'LeaveModal',
  actionCancel: {
    text: __('Cancel'),
  },
  csrf,
  modalId: LEAVE_MODAL_ID,
  i18n: {
    title: s__('Members|Leave "%{source}"'),
    body: s__('Members|Are you sure you want to leave "%{source}"?'),
    preventedTitle: s__('Members|Cannot leave "%{source}"'),
    preventedBodyProjectMemberModelType: s__(
      'Members|You cannot remove yourself from a personal project.',
    ),
    preventedBodyGroupMemberModelType: s__(
      'Members|A group must have at least one owner. To leave this group, assign a new owner.',
    ),
  },
  components: { GlModal, GlForm, GlSprintf, UserDeletionObstaclesList },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  inject: ['namespace'],
  props: {
    member: {
      type: Object,
      required: true,
    },
    permissions: {
      type: Object,
      required: true,
    },
  },
  computed: {
    ...mapState({
      memberPath(state) {
        return state[this.namespace].memberPath;
      },
    }),
    leavePath() {
      return this.memberPath.replace(/:id$/, 'leave');
    },
    modalTitle() {
      return sprintf(
        this.permissions.canRemoveBlockedByLastOwner
          ? this.$options.i18n.preventedTitle
          : this.$options.i18n.title,
        { source: this.member.source.fullName },
      );
    },
    preventedModalBody() {
      if (this.member.type === MEMBER_MODEL_TYPE_PROJECT_MEMBER) {
        return this.$options.i18n.preventedBodyProjectMemberModelType;
      }
 
      if (this.member.type === MEMBER_MODEL_TYPE_GROUP_MEMBER) {
        return this.$options.i18n.preventedBodyGroupMemberModelType;
      }
 
      return null;
    },
    actionPrimary() {
      if (this.permissions.canRemoveBlockedByLastOwner) {
        return null;
      }
 
      return {
        text: __('Leave'),
        attributes: {
          variant: 'danger',
        },
      };
    },
    obstacles() {
      return parseUserDeletionObstacles(this.member.user);
    },
    hasObstaclesToUserDeletion() {
      return this.obstacles?.length;
    },
  },
  methods: {
    handlePrimary() {
      this.$refs.form.$el.submit();
    },
  },
};
</script>
 
<template>
  <gl-modal
    v-bind="$attrs"
    :modal-id="$options.modalId"
    :title="modalTitle"
    :action-primary="actionPrimary"
    :action-cancel="$options.actionCancel"
    @primary="handlePrimary"
  >
    <gl-form ref="form" :action="leavePath" method="post">
      <p>
        <template v-if="permissions.canRemoveBlockedByLastOwner">{{ preventedModalBody }}</template>
        <gl-sprintf v-else :message="$options.i18n.body">
          <template #source>{{ member.source.fullName }}</template>
        </gl-sprintf>
      </p>
 
      <user-deletion-obstacles-list
        v-if="hasObstaclesToUserDeletion"
        :obstacles="obstacles"
        :is-current-user="true"
      />
 
      <input type="hidden" name="_method" value="delete" />
      <input :value="$options.csrf.token" type="hidden" name="authenticity_token" />
    </gl-form>
  </gl-modal>
</template>