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