summaryrefslogtreecommitdiff
path: root/src/views/Overview/ModalAssetTag.vue
blob: c73b5ba5e2db14dc6fdc2c9255dbaba818b243c4 (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
<template>
  <b-modal
    id="modal-asset-tag"
    ref="modal"
    :title="$t('pageOverview.modal.editAssetTag')"
    @hidden="resetForm"
  >
    <b-form id="asset-settings" @submit.prevent="handleSubmit">
      <b-row>
        <b-col sm="8">
          <b-form-group
            :label="$t('pageOverview.assetTag')"
            label-for="asset-tag"
          >
            <b-form-input
              id="asset-tag"
              v-model="form.assetTag"
              type="text"
              :state="getValidationState(v$.form.assetTag)"
              @input="v$.form.assetTag.$touch()"
            />
            <b-form-invalid-feedback role="alert">
              <template v-if="v$.form.assetTag.required.$invalid">
                {{ $t('global.form.fieldRequired') }}
              </template>
            </b-form-invalid-feedback>
          </b-form-group>
        </b-col>
      </b-row>
    </b-form>
    <template #footer="{ cancel }">
      <b-button variant="secondary" @click="cancel()">
        {{ $t('global.action.cancel') }}
      </b-button>
      <b-button
        form="asset-settings"
        type="submit"
        variant="primary"
        @click="onOk"
      >
        {{ $t('global.action.save') }}
      </b-button>
    </template>
  </b-modal>
</template>

<script>
import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
import { useVuelidate } from '@vuelidate/core';
import { required } from '@vuelidate/validators';

export default {
  mixins: [VuelidateMixin],
  props: {
    modelValue: {
      type: Boolean,
      default: false,
    },
    tag: {
      type: String,
      default: '',
    },
  },
  emits: ['ok', 'hidden', 'update:modelValue'],
  setup() {
    return {
      v$: useVuelidate(),
    };
  },
  data() {
    return {
      form: {
        assetTag: this.tag ? this.tag : '',
      },
    };
  },
  watch: {
    tag() {
      this.form.assetTag = this.tag;
    },
    modelValue: {
      handler(newValue) {
        if (newValue) {
          this.$nextTick(() => {
            this.$refs.modal?.show();
          });
        }
      },
      immediate: true,
    },
  },
  validations() {
    return {
      form: {
        assetTag: {
          required,
        },
      },
    };
  },
  methods: {
    handleSubmit() {
      this.v$.$touch();
      if (this.v$.$invalid) return;
      this.$emit('ok', { AssetTag: this.form.assetTag });
      this.closeModal();
    },
    closeModal() {
      this.$nextTick(() => {
        this.$refs.modal.hide();
      });
    },
    resetForm() {
      this.form.assetTag = this.tag;
      this.v$.$reset();
      this.$emit('update:modelValue', false);
      this.$emit('hidden');
    },
    onOk(bvModalEvt) {
      // prevent modal close
      bvModalEvt.preventDefault();
      this.handleSubmit();
    },
  },
};
</script>