|
| 1 | +<template> |
| 2 | + <div class="col s12 m6"> |
| 3 | + <div> |
| 4 | + <div class="page-subtitle"> |
| 5 | + <h4>Редактировать</h4> |
| 6 | + </div> |
| 7 | + |
| 8 | + <form @submit.prevent="submitHandler"> |
| 9 | + <div class="input-field"> |
| 10 | + <select ref="select" v-model="current"> |
| 11 | + <option v-for="c of categories" :key="c.id" :value="c.id">{{c.title}}</option> |
| 12 | + </select> |
| 13 | + <label>Выберите категорию</label> |
| 14 | + </div> |
| 15 | + |
| 16 | + <div class="input-field"> |
| 17 | + <input |
| 18 | + id="name" |
| 19 | + type="text" |
| 20 | + v-model="title" |
| 21 | + :class="{invalid: $v.title.$dirty && !$v.title.required}" |
| 22 | + /> |
| 23 | + <label for="name">Название</label> |
| 24 | + <span |
| 25 | + v-if="$v.title.$dirty && !$v.title.required" |
| 26 | + class="helper-text invalid" |
| 27 | + >Введите название категории</span> |
| 28 | + </div> |
| 29 | + |
| 30 | + <div class="input-field"> |
| 31 | + <input |
| 32 | + id="limit" |
| 33 | + type="number" |
| 34 | + v-model.number="limit" |
| 35 | + :class="{invalid: $v.limit.$dirty && !$v.limit.minValue}" |
| 36 | + /> |
| 37 | + <label for="limit">Лимит</label> |
| 38 | + <span |
| 39 | + v-if="$v.limit.$dirty && !$v.limit.minValue" |
| 40 | + class="helper-text invalid" |
| 41 | + >Минимальная значение {{$v.limit.$params.minValue.min}}</span> |
| 42 | + </div> |
| 43 | + |
| 44 | + <button class="btn waves-effect waves-light" type="submit"> |
| 45 | + Обновить |
| 46 | + <i class="material-icons right">send</i> |
| 47 | + </button> |
| 48 | + </form> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | +</template> |
| 52 | + |
| 53 | +<script> |
| 54 | +import { required, minValue } from "vuelidate/lib/validators"; |
| 55 | +import M from "materialize-css" |
| 56 | +
|
| 57 | +export default { |
| 58 | + props: { |
| 59 | + categories: { |
| 60 | + type: Array, |
| 61 | + required: true |
| 62 | + } |
| 63 | + }, |
| 64 | + data: () => ({ |
| 65 | + select: null, |
| 66 | + title: "", |
| 67 | + limit: 100, |
| 68 | + current: null |
| 69 | + }), |
| 70 | + validations: { |
| 71 | + title: { required }, |
| 72 | + limit: { minValue: minValue(100) } |
| 73 | + }, |
| 74 | + watch: { |
| 75 | + current(catId) { |
| 76 | + const { title, limit } = this.categories.find(c => c.id === catId); |
| 77 | + this.title = title; |
| 78 | + this.limit = limit; |
| 79 | + } |
| 80 | + }, |
| 81 | + created() { |
| 82 | + const { id, title, limit } = this.categories[0]; |
| 83 | + this.current = id; |
| 84 | + this.title = title; |
| 85 | + this.limit = limit; |
| 86 | + }, |
| 87 | + methods: { |
| 88 | + async submitHandler() { |
| 89 | + if (this.$v.$invalid) { |
| 90 | + this.$v.$touch(); |
| 91 | + return; |
| 92 | + } |
| 93 | +
|
| 94 | + try { |
| 95 | + const categoryData = { |
| 96 | + id: this.current, |
| 97 | + title: this.title, |
| 98 | + limit: this.limit |
| 99 | + }; |
| 100 | + await this.$store.dispatch("updateCategory", categoryData); |
| 101 | + this.$message("Категория упешно обновлена"); |
| 102 | + this.$emit("updated", categoryData); |
| 103 | + } catch (e) {} |
| 104 | + } |
| 105 | + }, |
| 106 | + mounted() { |
| 107 | + this.select = M.FormSelect.init(this.$refs.select); |
| 108 | + M.updateTextFields(); |
| 109 | + }, |
| 110 | + destroyed() { |
| 111 | + if (this.select && this.select.destroy) { |
| 112 | + this.select.destroy(); |
| 113 | + } |
| 114 | + } |
| 115 | +}; |
| 116 | +</script> |
0 commit comments