1<template>
2  <b-modal
3    id="modal-default-gateway"
4    ref="modal"
5    :title="$t('pageNetwork.modal.editIPv6DefaultGatewayTitle')"
6    @hidden="resetForm"
7  >
8    <b-form id="gateway-settings" @submit.prevent="handleSubmit">
9      <b-row>
10        <b-col sm="6">
11          <b-form-group
12            :label="$t('pageNetwork.gateway')"
13            label-for="defaultGateway"
14          >
15            <b-form-input
16              id="defaultGateway"
17              v-model.trim="form.defaultGateway"
18              data-test-id="network-input-gateway"
19              type="text"
20              :state="getValidationState($v.form.defaultGateway)"
21              @change="$v.form.defaultGateway.$touch()"
22            />
23            <b-form-invalid-feedback role="alert">
24              <div v-if="!$v.form.defaultGateway.required">
25                {{ $t('global.form.fieldRequired') }}
26              </div>
27              <div v-if="!$v.form.defaultGateway.validateGateway">
28                {{ $t('global.form.invalidFormat') }}
29              </div>
30            </b-form-invalid-feedback>
31          </b-form-group>
32        </b-col>
33      </b-row>
34    </b-form>
35    <template #modal-footer="{ cancel }">
36      <b-button variant="secondary" @click="cancel()">
37        {{ $t('global.action.cancel') }}
38      </b-button>
39      <b-button
40        form="gateway-settings"
41        type="submit"
42        variant="primary"
43        @click="onOk"
44      >
45        {{ $t('global.action.add') }}
46      </b-button>
47    </template>
48  </b-modal>
49</template>
50
51<script>
52import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
53import { required, helpers } from 'vuelidate/lib/validators';
54
55const validateGateway = helpers.regex(
56  'validateGateway',
57  /^((?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}|(?:[a-fA-F0-9]{1,4}:){1,7}:|(?:[a-fA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}|(?:[a-fA-F0-9]{1,4}:){1,5}(?::[a-fA-F0-9]{1,4}){1,2}|(?:[a-fA-F0-9]{1,4}:){1,4}(?::[a-fA-F0-9]{1,4}){1,3}|(?:[a-fA-F0-9]{1,4}:){1,3}(?::[a-fA-F0-9]{1,4}){1,4}|(?:[a-fA-F0-9]{1,4}:){1,2}(?::[a-fA-F0-9]{1,4}){1,5}|[a-fA-F0-9]{1,4}:(?::[a-fA-F0-9]{1,4}){1,6}|:(?::[a-fA-F0-9]{1,4}){1,7}|::|(?:[a-fA-F0-9]{1,4}:){6}(?:[0-9]{1,3}\.){3}[0-9]{1,3}|::(?:[a-fA-F0-9]{1,4}:){0,5}(?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[a-fA-F0-9]{1,4}:){1,5}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[a-fA-F0-9]{1,4}:){1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[a-fA-F0-9]{1,4}:){1,3}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|(?:[a-fA-F0-9]{1,4}:){1,2}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|[a-fA-F0-9]{1,4}:(?:[0-9]{1,3}\.){3}[0-9]{1,3}|::(?:[0-9]{1,3}\.){3}[0-9]{1,3})$/,
58);
59
60export default {
61  mixins: [VuelidateMixin],
62  props: {
63    defaultGateway: {
64      type: String,
65      default: '',
66    },
67  },
68  data() {
69    return {
70      form: {
71        defaultGateway: '',
72      },
73    };
74  },
75  watch: {
76    defaultGateway() {
77      this.form.defaultGateway = this.defaultGateway;
78    },
79  },
80  validations() {
81    return {
82      form: {
83        defaultGateway: {
84          required,
85          validateGateway,
86        },
87      },
88    };
89  },
90  methods: {
91    handleSubmit() {
92      this.$v.$touch();
93      if (this.$v.$invalid) return;
94      this.$emit('ok', { IPv6DefaultGateway: this.form.defaultGateway });
95      this.closeModal();
96    },
97    closeModal() {
98      this.$nextTick(() => {
99        this.$refs.modal.hide();
100      });
101    },
102    resetForm() {
103      this.form.defaultGateway = this.defaultGateway;
104      this.$v.$reset();
105      this.$emit('hidden');
106    },
107    onOk(bvModalEvt) {
108      // prevent modal close
109      bvModalEvt.preventDefault();
110      this.handleSubmit();
111    },
112  },
113};
114</script>
115