|
| 1 | +<!-- This example requires Tailwind CSS v2.0+ --> |
| 2 | +<template> |
| 3 | + <div id="product-form"> |
| 4 | + <div class="flex items-center justify-between mb-3"> |
| 5 | + <h1 class="text-3xl font-semibold">{{ product.id ? `Update product: "${product.title}"` : 'Create new Product' }}</h1> |
| 6 | + </div> |
| 7 | + <Spinner v-if="loading" |
| 8 | + class="p-5 z-[100] absolute left-0 top-0 bg-white right-0 bottom-0 flex items-center justify-center"/> |
| 9 | + <form @submit.prevent="onSubmit"> |
| 10 | + <div class="bg-white px-4 pt-5 pb-4 z-50"> |
| 11 | + <CustomInput class="mb-2" v-model="product.title" label="Product Title"/> |
| 12 | + <CustomInput type="file" class="mb-2" label="Product Image" @change="file => product.image = file"/> |
| 13 | + <CustomInput type="richtext" class="mb-2" v-model="product.description" label="Description"/> |
| 14 | + <CustomInput type="number" class="mb-2" v-model="product.price" label="Price" prepend="$"/> |
| 15 | + <CustomInput type="checkbox" class="mb-2" v-model="product.published" label="Published"/> |
| 16 | + |
| 17 | + |
| 18 | + </div> |
| 19 | + <footer class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse"> |
| 20 | + <button type="submit" |
| 21 | + class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm |
| 22 | + text-white bg-indigo-600 hover:bg-indigo-700 focus:ring-indigo-500"> |
| 23 | + Save |
| 24 | + </button> |
| 25 | + <button type="button" |
| 26 | + @click="onSubmit($event, true)" |
| 27 | + class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm |
| 28 | + text-white bg-indigo-600 hover:bg-indigo-700 focus:ring-indigo-500"> |
| 29 | + Save and Close |
| 30 | + </button> |
| 31 | + <router-link :to="{name: 'app.products'}" |
| 32 | + class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm"> |
| 33 | + Cancel |
| 34 | + </router-link> |
| 35 | + </footer> |
| 36 | + </form> |
| 37 | + </div> |
| 38 | +</template> |
| 39 | + |
| 40 | +<script setup> |
| 41 | +import {computed, onMounted, ref} from 'vue' |
| 42 | +import CustomInput from "../../components/core/CustomInput.vue"; |
| 43 | +import store from "../../store/index.js"; |
| 44 | +import Spinner from "../../components/core/Spinner.vue"; |
| 45 | +import {useRoute, useRouter} from "vue-router"; |
| 46 | +
|
| 47 | +const product = ref({ |
| 48 | + id: null, |
| 49 | + title: null, |
| 50 | + image: null, |
| 51 | + description: '', |
| 52 | + price: null, |
| 53 | + published: null |
| 54 | +}) |
| 55 | +
|
| 56 | +const loading = ref(false) |
| 57 | +
|
| 58 | +const router = useRouter(); |
| 59 | +const route = useRoute(); |
| 60 | +
|
| 61 | +onMounted(() => { |
| 62 | + if (route.params.id) { |
| 63 | + loading.value = true |
| 64 | + store.dispatch('getProduct', route.params.id) |
| 65 | + .then(({data}) => { |
| 66 | + loading.value = false |
| 67 | + product.value = data |
| 68 | + }) |
| 69 | + } |
| 70 | +}) |
| 71 | +
|
| 72 | +function onSubmit(event, close = false) { |
| 73 | + loading.value = true |
| 74 | + if (product.value.id) { |
| 75 | + store.dispatch('updateProduct', product.value) |
| 76 | + .then(response => { |
| 77 | + loading.value = false; |
| 78 | + if (response.status === 200) { |
| 79 | + store.commit('showToast', 'Product was successfully updated') |
| 80 | + store.dispatch('getProducts') |
| 81 | + if (close) { |
| 82 | + router.push({name: 'app.products'}) |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + } else { |
| 87 | + store.dispatch('createProduct', product.value) |
| 88 | + .then(response => { |
| 89 | + loading.value = false; |
| 90 | + if (response.status === 201) { |
| 91 | + store.commit('showToast', 'Product was successfully created') |
| 92 | + store.dispatch('getProducts') |
| 93 | + if (close) { |
| 94 | + router.push({name: 'app.products'}) |
| 95 | + } |
| 96 | + } |
| 97 | + }) |
| 98 | + .catch(err => { |
| 99 | + loading.value = false; |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
| 103 | +</script> |
| 104 | +<style> |
| 105 | +#product-form { |
| 106 | + position: relative; |
| 107 | +} |
| 108 | +</style> |
0 commit comments