summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/packages_and_registries/settings/group/components/dependency_proxy_settings.vue
blob: a5189201112b56c3eef8cb79c37621b827259241 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<script>
import { GlToggle, GlSprintf, GlLink } from '@gitlab/ui';
import { s__ } from '~/locale';
import SettingsBlock from '~/vue_shared/components/settings/settings_block.vue';
import SettingsTitles from '~/packages_and_registries/settings/group/components/settings_titles.vue';
import updateDependencyProxySettings from '~/packages_and_registries/settings/group/graphql/mutations/update_dependency_proxy_settings.mutation.graphql';
import updateDependencyProxyImageTtlGroupPolicy from '~/packages_and_registries/settings/group/graphql/mutations/update_dependency_proxy_image_ttl_group_policy.mutation.graphql';
import { updateGroupPackageSettings } from '~/packages_and_registries/settings/group/graphql/utils/cache_update';
import {
  updateGroupDependencyProxySettingsOptimisticResponse,
  updateDependencyProxyImageTtlGroupPolicyOptimisticResponse,
} from '~/packages_and_registries/settings/group/graphql/utils/optimistic_responses';

import {
  DEPENDENCY_PROXY_HEADER,
  DEPENDENCY_PROXY_DOCS_PATH,
} from '~/packages_and_registries/settings/group/constants';

export default {
  name: 'DependencyProxySettings',
  components: {
    GlToggle,
    GlSprintf,
    GlLink,
    SettingsBlock,
    SettingsTitles,
  },
  i18n: {
    DEPENDENCY_PROXY_HEADER,
    enabledProxyLabel: s__('DependencyProxy|Enable Dependency Proxy'),
    enabledProxyHelpText: s__(
      'DependencyProxy|To see the image prefix and what is in the cache, visit the %{linkStart}Dependency Proxy%{linkEnd}',
    ),
    storageSettingsTitle: s__('DependencyProxy|Storage settings'),
    ttlPolicyEnabledLabel: s__('DependencyProxy|Clear the Dependency Proxy cache automatically'),
    ttlPolicyEnabledHelpText: s__(
      'DependencyProxy|When enabled, images older than 90 days will be removed from the cache.',
    ),
  },
  links: {
    DEPENDENCY_PROXY_DOCS_PATH,
  },
  inject: ['defaultExpanded', 'groupPath', 'groupDependencyProxyPath'],
  props: {
    dependencyProxySettings: {
      type: Object,
      required: true,
    },
    dependencyProxyImageTtlPolicy: {
      type: Object,
      required: true,
    },
    isLoading: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  computed: {
    enabled: {
      get() {
        return this.dependencyProxySettings.enabled;
      },
      set(enabled) {
        this.updateSettings({ enabled });
      },
    },
    ttlEnabled: {
      get() {
        return this.dependencyProxyImageTtlPolicy.enabled;
      },
      set(enabled) {
        const payload = {
          enabled,
          ttl: 90, // hardocded TTL for the MVC version
        };
        this.updateDependencyProxyImageTtlGroupPolicy(payload);
      },
    },
    helpText() {
      return this.enabled ? this.$options.i18n.enabledProxyHelpText : '';
    },
  },
  methods: {
    mutationVariables(payload) {
      return {
        input: {
          groupPath: this.groupPath,
          ...payload,
        },
      };
    },
    async executeMutation(config, resource) {
      try {
        const { data } = await this.$apollo.mutate(config);
        if (data[resource]?.errors.length > 0) {
          throw new Error();
        } else {
          this.$emit('success');
        }
      } catch {
        this.$emit('error');
      }
    },
    async updateSettings(payload) {
      const apolloConfig = {
        mutation: updateDependencyProxySettings,
        variables: this.mutationVariables(payload),
        update: updateGroupPackageSettings(this.groupPath),
        optimisticResponse: updateGroupDependencyProxySettingsOptimisticResponse({
          ...this.dependencyProxySettings,
          ...payload,
        }),
      };

      this.executeMutation(apolloConfig, 'updateDependencyProxySettings');
    },
    async updateDependencyProxyImageTtlGroupPolicy(payload) {
      const apolloConfig = {
        mutation: updateDependencyProxyImageTtlGroupPolicy,
        variables: this.mutationVariables(payload),
        update: updateGroupPackageSettings(this.groupPath),
        optimisticResponse: updateDependencyProxyImageTtlGroupPolicyOptimisticResponse({
          ...this.dependencyProxyImageTtlPolicy,
          ...payload,
        }),
      };

      this.executeMutation(apolloConfig, 'updateDependencyProxyImageTtlGroupPolicy');
    },
  },
};
</script>

<template>
  <settings-block
    :default-expanded="defaultExpanded"
    data-qa-selector="dependency_proxy_settings_content"
  >
    <template #title> {{ $options.i18n.DEPENDENCY_PROXY_HEADER }} </template>
    <template #default>
      <div>
        <gl-toggle
          v-model="enabled"
          :disabled="isLoading"
          :label="$options.i18n.enabledProxyLabel"
          :help="helpText"
          data-qa-selector="dependency_proxy_setting_toggle"
          data-testid="dependency-proxy-setting-toggle"
        >
          <template #help>
            <span class="gl-overflow-break-word gl-max-w-100vw gl-display-inline-block">
              <gl-sprintf :message="$options.i18n.enabledProxyHelpText">
                <template #link="{ content }">
                  <gl-link data-testid="toggle-help-link" :href="groupDependencyProxyPath">{{
                    content
                  }}</gl-link>
                </template>
              </gl-sprintf>
            </span>
          </template>
        </gl-toggle>

        <settings-titles :title="$options.i18n.storageSettingsTitle" class="gl-my-6" />
        <gl-toggle
          v-model="ttlEnabled"
          :disabled="isLoading"
          :label="$options.i18n.ttlPolicyEnabledLabel"
          :help="$options.i18n.ttlPolicyEnabledHelpText"
          data-testid="dependency-proxy-ttl-policies-toggle"
        />
      </div>
    </template>
  </settings-block>
</template>