Welcome to mirror list, hosted at ThFree Co, Russian Federation.

protected_tag_edit.vue « protected_tags « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5ec88c171ce239f80963ce943132ddafebab008 (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
<script>
import AccessDropdown from '~/projects/settings/components/access_dropdown.vue';
import { createAlert } from '~/alert';
import axios from '~/lib/utils/axios_utils';
import { s__ } from '~/locale';
import { ACCESS_LEVELS, LEVEL_TYPES } from './constants';

export const i18n = {
  failureMessage: s__('ProjectSettings|Failed to update tag!'),
};

export default {
  i18n,
  ACCESS_LEVELS,
  name: 'ProtectedTagEdit',
  components: {
    AccessDropdown,
  },
  props: {
    url: {
      type: String,
      required: true,
    },
    accessLevelsData: {
      type: Array,
      required: true,
    },
    hasLicense: {
      required: false,
      type: Boolean,
      default: true,
    },
    preselectedItems: {
      type: Array,
      required: false,
      default: () => [],
    },
    searchEnabled: {
      type: Boolean,
      required: false,
      default: true,
    },
  },
  data() {
    return {
      selected: this.preselectedItems,
    };
  },
  methods: {
    hasChanges(permissions) {
      return permissions.some(({ id, _destroy }) => id === undefined || _destroy);
    },
    updatePermissions(permissions) {
      if (!this.hasChanges(permissions)) return;
      axios
        .patch(this.url, {
          protected_tag: {
            [`${ACCESS_LEVELS.CREATE}_attributes`]: permissions,
          },
        })
        .then(this.setSelected)
        .catch(() => {
          createAlert({
            message: i18n.failureMessage,
            parent: this.parentContainer,
          });
        });
    },
    setSelected({ data }) {
      if (!data) return;
      this.selected = data[ACCESS_LEVELS.CREATE].map(
        ({ id, user_id: userId, group_id: groupId, access_level: accessLevel }) => {
          if (userId) {
            return {
              id,
              user_id: userId,
              type: LEVEL_TYPES.USER,
            };
          }

          if (groupId) {
            return {
              id,
              group_id: groupId,
              type: LEVEL_TYPES.GROUP,
            };
          }

          return {
            id,
            access_level: accessLevel,
            type: LEVEL_TYPES.ROLE,
          };
        },
      );
    },
  },
};
</script>

<template>
  <access-dropdown
    toggle-class="js-allowed-to-create gl-max-w-34"
    test-id="allowed-to-create-dropdown"
    :has-license="hasLicense"
    :access-level="$options.ACCESS_LEVELS.CREATE"
    :access-levels-data="accessLevelsData"
    :preselected-items="selected"
    :search-enabled="searchEnabled"
    groups-with-project-access
    :block="true"
    @hidden="updatePermissions"
  />
</template>