All files / app/assets/javascripts/deploy_freeze/components deploy_freeze_modal.vue

91.3% Statements 21/23
100% Branches 15/15
84.62% Functions 11/13
91.3% Lines 21/23

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 164 165 166 167 168  2x 2x 2x 2x 2x 2x                                                                                   17x                           17x     17x     17x     17x       17x             11x     11x                     34x 2x   32x     2x 1x   1x             57x                                                                                                              
<script>
import { GlFormGroup, GlFormInput, GlModal, GlSprintf, GlLink } from '@gitlab/ui';
import { isValidCron } from 'cron-validator';
import { mapActions, mapState } from 'vuex';
import { __ } from '~/locale';
import TimezoneDropdown from '~/vue_shared/components/timezone_dropdown.vue';
import { mapComputed } from '~/vuex_shared/bindings';
 
export default {
  components: {
    GlFormGroup,
    GlFormInput,
    GlModal,
    GlSprintf,
    GlLink,
    TimezoneDropdown,
  },
  modalOptions: {
    ref: 'modal',
    modalId: 'deploy-freeze-modal',
    actionCancel: {
      text: __('Cancel'),
    },
    static: true,
    lazy: true,
  },
  translations: {
    cronPlaceholder: '* * * * *',
    cronSyntaxInstructions: __(
      'Define a custom deploy freeze pattern with %{cronSyntaxStart}cron syntax%{cronSyntaxEnd}',
    ),
    addTitle: __('Add deploy freeze'),
    editTitle: __('Edit deploy freeze'),
  },
  computed: {
    ...mapState([
      'projectId',
      'selectedId',
      'selectedTimezone',
      'timezoneData',
      'freezeStartCron',
      'freezeEndCron',
    ]),
    ...mapComputed([
      { key: 'freezeStartCron', updateFn: 'setFreezeStartCron' },
      { key: 'freezeEndCron', updateFn: 'setFreezeEndCron' },
    ]),
    addDeployFreezeButton() {
      return {
        text: this.isEditing ? __('Save deploy freeze') : __('Add deploy freeze'),
        attributes: [
          { variant: 'confirm' },
          {
            disabled:
              !isValidCron(this.freezeStartCron) ||
              !isValidCron(this.freezeEndCron) ||
              !this.selectedTimezone,
          },
        ],
      };
    },
    invalidFreezeStartCron() {
      return this.invalidCronMessage(this.freezeStartCronState);
    },
    freezeStartCronState() {
      return Boolean(!this.freezeStartCron || isValidCron(this.freezeStartCron));
    },
    invalidFreezeEndCron() {
      return this.invalidCronMessage(this.freezeEndCronState);
    },
    freezeEndCronState() {
      return Boolean(!this.freezeEndCron || isValidCron(this.freezeEndCron));
    },
    timezone: {
      get() {
        return this.selectedTimezone;
      },
      set(selectedTimezone) {
        this.setSelectedTimezone(selectedTimezone);
      },
    },
    isEditing() {
      return Boolean(this.selectedId);
    },
    modalTitle() {
      return this.isEditing
        ? this.$options.translations.editTitle
        : this.$options.translations.addTitle;
    },
  },
  methods: {
    ...mapActions(['addFreezePeriod', 'updateFreezePeriod', 'setSelectedTimezone', 'resetModal']),
    resetModalHandler() {
      this.resetModal();
    },
    invalidCronMessage(validCronState) {
      if (!validCronState) {
        return __('This Cron pattern is invalid');
      }
      return '';
    },
    submit() {
      if (this.isEditing) {
        this.updateFreezePeriod();
      } else {
        this.addFreezePeriod();
      }
    },
  },
};
</script>
 
<template>
  <gl-modal
    v-bind="$options.modalOptions"
    :title="modalTitle"
    :action-primary="addDeployFreezeButton"
    @primary="submit"
    @canceled="resetModalHandler"
  >
    <p>
      <gl-sprintf :message="$options.translations.cronSyntaxInstructions">
        <template #cronSyntax="{ content }">
          <gl-link href="https://crontab.guru/" target="_blank">{{ content }}</gl-link>
        </template>
      </gl-sprintf>
    </p>
 
    <gl-form-group
      :label="__('Freeze start')"
      label-for="deploy-freeze-start"
      :invalid-feedback="invalidFreezeStartCron"
      :state="freezeStartCronState"
    >
      <gl-form-input
        id="deploy-freeze-start"
        v-model="freezeStartCron"
        class="gl-font-monospace!"
        data-qa-selector="deploy_freeze_start_field"
        :placeholder="this.$options.translations.cronPlaceholder"
        :state="freezeStartCronState"
        trim
      />
    </gl-form-group>
 
    <gl-form-group
      :label="__('Freeze end')"
      label-for="deploy-freeze-end"
      :invalid-feedback="invalidFreezeEndCron"
      :state="freezeEndCronState"
    >
      <gl-form-input
        id="deploy-freeze-end"
        v-model="freezeEndCron"
        class="gl-font-monospace!"
        data-qa-selector="deploy_freeze_end_field"
        :placeholder="this.$options.translations.cronPlaceholder"
        :state="freezeEndCronState"
        trim
      />
    </gl-form-group>
 
    <gl-form-group :label="__('Cron time zone')" label-for="cron-time-zone-dropdown">
      <timezone-dropdown v-model="timezone" :timezone-data="timezoneData" />
    </gl-form-group>
  </gl-modal>
</template>