product.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <main>
  3. <div class="container-fluid">
  4. <div class="col-sm3"></div>
  5. <div class="col-sm6">
  6. <div class="a-spacing-top-medium"></div>
  7. <h2 style="text-align: center">Add a new Product</h2>
  8. <form action="">
  9. <!-- Category DropDown -->
  10. <div class="a-spacing-top-medium">
  11. <label for="">Category</label>
  12. <select v-model="categoryID" class="a-select-option">
  13. <option v-for="category in categories" :key="category._id" :value="category._id">{{ category.type }}</option>
  14. </select>
  15. </div>
  16. <!-- Owner DropDown -->
  17. <div class="a-spacing-top-medium">
  18. <label for="">Owner</label>
  19. <select v-model="ownerID" class="a-select-option" >
  20. <option v-for="owner in owners" :key="owner._id" :value="owner._id">{{ owner.name }}</option>
  21. </select>
  22. </div>
  23. <!-- Title input-->
  24. <div class="a-spacing-top-medium">
  25. <label for="" style="margin-bottom: 0px;">Title</label>
  26. <input v-model="title" type="text" class="a-input-text" style="width: 100%"/>
  27. </div>
  28. <!-- stockQuantity input-->
  29. <div class="a-spacing-top-medium">
  30. <label for="" style="margin-bottom: 0px;">stockQuantity</label>
  31. <input v-model="stockQuantity" type="number" class="a-input-text" style="width: 100%"/>
  32. </div>
  33. <!-- Price input-->
  34. <div class="a-spacing-top-medium">
  35. <label for="" style="margin-bottom: 0px;">Price</label>
  36. <input v-model="price" type="number" class="a-input-text" style="width: 100%"/>
  37. </div>
  38. <!-- Description input-->
  39. <div class="a-spacing-top-medium">
  40. <label for="" style="margin-bottom: 0px;">Description</label>
  41. <textarea v-model="description" placeholder="Provide details product description" style="width: 100%"></textarea>
  42. </div>
  43. <!-- Photo input-->
  44. <div class="a-spacing-top-medium">
  45. <label for="" style="margin-bottom: 0px;">Photo</label>
  46. <div class="a-row a-spacing-top-medium">
  47. <label class="choosefile-button">
  48. <i class="fa fa-plus"></i>
  49. <input @change="onFileSelected" type="file" />
  50. <p style="margin-top: -70px">{{ fileName }}</p>
  51. </label>
  52. </div>
  53. </div>
  54. <hr/>
  55. <!--Button-->
  56. <div class="a-spacing-top-large">
  57. <span class="a-button-register">
  58. <span @click="onAddProduct" class="a-button-text">Add Product</span>
  59. </span>
  60. </div>
  61. </form>
  62. </div>
  63. <div class="col-sm3"></div>
  64. </div>
  65. </main>
  66. </template>
  67. <script>
  68. export default {
  69. async asyncData( { $axios }) {
  70. try {
  71. let categories = $axios.$get('http://localhost:3000/api/categories')
  72. let owners = $axios.$get('http://localhost:3000/api/owners')
  73. const [catResponse, ownerResponse] = await Promise.all([categories, owners])
  74. console.log(catResponse)
  75. return {
  76. categories: catResponse.categories,
  77. owners: ownerResponse.owners
  78. }
  79. } catch (err) {
  80. console.log(err)
  81. }
  82. },
  83. data () {
  84. return {
  85. categoryID: null,
  86. ownerID: null,
  87. title: '',
  88. price: 0,
  89. description: '',
  90. selectedFile: null,
  91. fileName: '',
  92. stockQuantity: 1
  93. }
  94. },
  95. methods: {
  96. onFileSelected (event) {
  97. this.selectedFile = event.target.files[0]
  98. console.log(this.selectedFile)
  99. this.fileName = event.target.files[0].name
  100. },
  101. async onAddProduct () {
  102. let data = new FormData()
  103. data.append('title', this.title)
  104. data.append('price', this.price)
  105. data.append('stockQuantity', this.stockQuantity)
  106. data.append('description', this.description)
  107. data.append('ownerID', this.ownerID)
  108. data.append('categoryID', this.categoryID)
  109. data.append('photo', this.selectedFile, this.selectedFile.name)
  110. let result = await this.$axios.$post('http://localhost:3000/api/products', data)
  111. this.$router.push('/')
  112. }
  113. }
  114. }
  115. </script>