All files / ee/app/assets/javascripts/groups/settings/compliance_frameworks/components edit_form.vue

100% Statements 41/41
100% Branches 16/16
100% Functions 14/14
100% Lines 41/41

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 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  2x 2x 2x 2x   2x 2x 2x 2x 2x 2x                                                               8x                     8x           7x     2x           8x     19x     18x 72x       12x         7x   7x 3x   3x     4x   4x               5x 5x     2x 2x 2x     3x 3x   3x 3x       3x                   2x   2x 1x   1x     1x                 78x                              
<script>
import * as Sentry from '@sentry/browser';
import { convertToGraphQLId } from '~/graphql_shared/utils';
import { visitUrl } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
 
import { FETCH_ERROR, SAVE_ERROR } from '../constants';
import getComplianceFrameworkQuery from '../graphql/queries/get_compliance_framework.query.graphql';
import updateComplianceFrameworkMutation from '../graphql/queries/update_compliance_framework.mutation.graphql';
import { getSubmissionParams, initialiseFormData } from '../utils';
import FormStatus from './form_status.vue';
import SharedForm from './shared_form.vue';
 
export default {
  components: {
    FormStatus,
    SharedForm,
  },
  props: {
    graphqlFieldName: {
      type: String,
      required: true,
    },
    groupEditPath: {
      type: String,
      required: true,
    },
    groupPath: {
      type: String,
      required: true,
    },
    id: {
      type: String,
      required: false,
      default: null,
    },
    pipelineConfigurationFullPathEnabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      initErrorMessage: '',
      saveErrorMessage: '',
      formData: initialiseFormData(),
      saving: false,
    };
  },
  apollo: {
    namespace: {
      query: getComplianceFrameworkQuery,
      variables() {
        return {
          fullPath: this.groupPath,
          complianceFramework: this.graphqlId,
        };
      },
      result({ data }) {
        this.formData = this.extractComplianceFramework(data);
      },
      error(error) {
        this.setInitError(error, FETCH_ERROR);
      },
    },
  },
  computed: {
    graphqlId() {
      return convertToGraphQLId(this.graphqlFieldName, this.id);
    },
    isLoading() {
      return this.$apollo.loading || this.saving;
    },
    showForm() {
      return (
        Object.values(this.formData).filter((d) => d !== null).length > 0 && !this.initErrorMessage
      );
    },
    errorMessage() {
      return this.initErrorMessage || this.saveErrorMessage;
    },
  },
  methods: {
    extractComplianceFramework(data) {
      const complianceFrameworks = data.namespace?.complianceFrameworks?.nodes || [];
 
      if (!complianceFrameworks.length) {
        this.setInitError(new Error(FETCH_ERROR), FETCH_ERROR);
 
        return initialiseFormData();
      }
 
      const { name, description, pipelineConfigurationFullPath, color } = complianceFrameworks[0];
 
      return {
        name,
        description,
        pipelineConfigurationFullPath,
        color,
      };
    },
    setInitError(error, userFriendlyText) {
      this.initErrorMessage = userFriendlyText;
      Sentry.captureException(error);
    },
    setSavingError(error, userFriendlyText) {
      this.saving = false;
      this.saveErrorMessage = userFriendlyText;
      Sentry.captureException(error);
    },
    async onSubmit() {
      this.saving = true;
      this.saveErrorMessage = '';
 
      try {
        const params = getSubmissionParams(
          this.formData,
          this.pipelineConfigurationFullPathEnabled,
        );
        const { data } = await this.$apollo.mutate({
          mutation: updateComplianceFrameworkMutation,
          variables: {
            input: {
              id: this.graphqlId,
              params,
            },
          },
        });
 
        const [error] = data?.updateComplianceFramework?.errors || [];
 
        if (error) {
          this.setSavingError(new Error(error), error);
        } else {
          visitUrl(this.groupEditPath);
        }
      } catch (e) {
        this.setSavingError(e, SAVE_ERROR);
      }
    },
  },
  i18n: {
    submitButtonText: __('Save changes'),
  },
};
</script>
<template>
  <form-status :loading="isLoading" :error="errorMessage">
    <shared-form
      v-if="showForm"
      :group-edit-path="groupEditPath"
      :pipeline-configuration-full-path-enabled="pipelineConfigurationFullPathEnabled"
      :name.sync="formData.name"
      :description.sync="formData.description"
      :pipeline-configuration-full-path.sync="formData.pipelineConfigurationFullPath"
      :color.sync="formData.color"
      :submit-button-text="$options.i18n.submitButtonText"
      @submit="onSubmit"
    />
  </form-status>
</template>