2018-11-08 00:24:18 +03:00
|
|
|
<template>
|
|
|
|
<v-dialog
|
|
|
|
:value="value"
|
|
|
|
@input="close"
|
2019-02-16 12:05:47 +03:00
|
|
|
@keyup.esc.native="close"
|
2018-11-08 00:24:18 +03:00
|
|
|
max-width="500px"
|
|
|
|
>
|
|
|
|
<slot slot="activator" />
|
2018-12-13 15:02:37 +03:00
|
|
|
|
2018-11-08 00:24:18 +03:00
|
|
|
<v-card>
|
|
|
|
<v-card-text>
|
|
|
|
<v-form
|
|
|
|
lazy-validation
|
|
|
|
v-model="isValid"
|
2018-11-13 23:25:02 +03:00
|
|
|
@keydown.native.prevent.enter="submit"
|
2018-11-08 00:24:18 +03:00
|
|
|
>
|
2018-11-13 23:25:02 +03:00
|
|
|
<!-- v-if="value" - cause without it autofocus triggers on first modal open
|
|
|
|
https://stackoverflow.com/questions/51472947/vuetifys-autofocus-works-only-on-first-modal-open -->
|
|
|
|
<v-text-field
|
|
|
|
v-if="value"
|
|
|
|
autofocus
|
2018-11-08 00:24:18 +03:00
|
|
|
class="mb-2"
|
|
|
|
label="Item name"
|
|
|
|
:rules="itemValidationRules"
|
|
|
|
v-model="itemName"
|
|
|
|
/>
|
|
|
|
</v-form>
|
|
|
|
</v-card-text>
|
|
|
|
<v-card-actions>
|
|
|
|
<v-spacer />
|
|
|
|
<v-btn
|
|
|
|
flat
|
|
|
|
color="primary"
|
|
|
|
class="add-cat-submit"
|
|
|
|
:disabled="!isValid"
|
|
|
|
@click.native="submit"
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</v-btn>
|
|
|
|
<v-btn
|
|
|
|
flat
|
|
|
|
color="primary"
|
|
|
|
@click.native="close"
|
|
|
|
>
|
|
|
|
Cancel
|
|
|
|
</v-btn>
|
|
|
|
</v-card-actions>
|
|
|
|
</v-card>
|
|
|
|
</v-dialog>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2019-02-19 23:24:07 +03:00
|
|
|
import Vue from 'vue'
|
|
|
|
import Component from 'vue-class-component'
|
|
|
|
import { Prop, Watch } from 'vue-property-decorator'
|
2018-11-08 00:24:18 +03:00
|
|
|
|
|
|
|
@Component
|
|
|
|
export default class AddItemDialog extends Vue {
|
|
|
|
@Prop(Boolean) value!: boolean
|
2018-11-25 20:41:15 +03:00
|
|
|
@Prop(String) categoryId!: string
|
2018-11-08 00:24:18 +03:00
|
|
|
|
|
|
|
itemName: string = ''
|
|
|
|
|
2019-02-16 12:05:47 +03:00
|
|
|
itemValidationRules: Array<(x: string) => boolean | string> = [
|
2018-11-08 00:24:18 +03:00
|
|
|
(x: string) => !!x || 'Item name can not be empty'
|
|
|
|
]
|
|
|
|
|
|
|
|
isValid: boolean = false
|
2018-11-25 20:41:15 +03:00
|
|
|
|
2018-11-08 00:24:18 +03:00
|
|
|
@Watch('value')
|
2019-01-09 13:26:27 +03:00
|
|
|
onOpen () {
|
2018-11-08 00:24:18 +03:00
|
|
|
this.itemName = ''
|
2018-11-25 20:41:15 +03:00
|
|
|
}
|
2018-11-08 00:24:18 +03:00
|
|
|
|
2018-11-25 20:41:15 +03:00
|
|
|
close () {
|
2018-11-08 00:24:18 +03:00
|
|
|
this.$emit('input', false)
|
|
|
|
}
|
|
|
|
|
2018-11-25 20:41:15 +03:00
|
|
|
async submit () {
|
2019-02-16 15:15:28 +03:00
|
|
|
const createdId = await this.$store.dispatch('categoryItem/createItem', {
|
2018-11-25 20:41:15 +03:00
|
|
|
category: this.categoryId,
|
2018-11-08 00:24:18 +03:00
|
|
|
name: this.itemName
|
|
|
|
})
|
2019-02-16 15:15:28 +03:00
|
|
|
await this.$store.dispatch('category/reloadCategory')
|
2018-11-08 00:24:18 +03:00
|
|
|
this.close()
|
2019-02-16 15:15:28 +03:00
|
|
|
// nextTick to wait for item rendered in dom so router can find it and scroll to it
|
|
|
|
this.$nextTick(() => {
|
|
|
|
this.$router.push({ hash: `item-${createdId}` })
|
|
|
|
})
|
2018-11-08 00:24:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|