|
1 | 1 | <template> |
2 | | - <div class="container py-5"> |
3 | | - <div class="row"> |
4 | | - <div class="col-lg-12"> |
5 | | - <div class="mb-3"> |
6 | | - <label for="name" class="form-label">Name:</label> |
7 | | - <input type="text" v-model="name" name="name" class="form-control" id="name" placeholder="Category Name" autocomplete="off"> |
8 | | - <input type="button" class="btn btn-primary mt-3" value="Add Category" @click="createCategory()"> |
9 | | - </div> |
| 2 | + <div class="container py-5"> |
| 3 | + <div class="row"> |
| 4 | + <div class="col-lg-12"> |
| 5 | + <div class="mb-3"> |
| 6 | + <form v-on:submit.prevent="submitForm"> |
| 7 | + <label for="name" class="form-label">Name:</label> |
| 8 | + <input |
| 9 | + type="text" |
| 10 | + v-model="form.name" |
| 11 | + name="name" |
| 12 | + class="form-control" |
| 13 | + id="name" |
| 14 | + placeholder="Category Name" |
| 15 | + autocomplete="off" |
| 16 | + /> |
| 17 | + <hr /> |
| 18 | + <p class="form-label">Photo:</p> |
| 19 | + <div class="api"> |
| 20 | + <div class="api__overlay"> |
| 21 | + <div class="api-file"> |
| 22 | + <label class="api-file__label"> |
| 23 | + <input |
| 24 | + type="file" |
| 25 | + accept="image/*" |
| 26 | + name="photo" |
| 27 | + @change="onHandleUpload($event)" |
| 28 | + class="api-file__input" |
| 29 | + /> |
| 30 | + <span class="api-file__btn"> |
| 31 | + <span class="api-file__text">Upload</span> |
| 32 | + </span> |
| 33 | + </label> |
| 34 | + </div> |
| 35 | + </div> |
10 | 36 | </div> |
| 37 | + |
| 38 | + <hr /> |
| 39 | + |
| 40 | + <button |
| 41 | + class="btn btn-primary" |
| 42 | + type="button" |
| 43 | + :disabled="isSubmitted" |
| 44 | + @click="submitForm" |
| 45 | + > |
| 46 | + <span |
| 47 | + v-if="isSubmitted" |
| 48 | + class="spinner-border spinner-border-sm" |
| 49 | + role="status" |
| 50 | + aria-hidden="true" |
| 51 | + ></span> |
| 52 | + <span class="visually-hidden">{{ |
| 53 | + isSubmitted ? 'Loading' : 'Submit' |
| 54 | + }}</span> |
| 55 | + </button> |
| 56 | + </form> |
11 | 57 | </div> |
| 58 | + </div> |
12 | 59 | </div> |
| 60 | + </div> |
13 | 61 | </template> |
14 | 62 |
|
15 | 63 | <script> |
| 64 | +import { onSingleFileUpload, uploadProgress } from '../utils'; |
| 65 | +
|
16 | 66 | export default { |
17 | | - name: 'CategoriesCreate', |
18 | | - data: function (){ |
19 | | - return { |
20 | | - name: '' |
21 | | - } |
| 67 | + name: 'CategoriesCreate', |
| 68 | + data: function () { |
| 69 | + return { |
| 70 | + isSubmitted: false, // To check whether the form is in the submitted |
| 71 | + form: { |
| 72 | + name: '', |
| 73 | + photo: '' |
| 74 | + }, |
| 75 | + selectedFile: null |
| 76 | + }; |
| 77 | + }, |
| 78 | + methods: { |
| 79 | + onHandleUpload: function (event) { |
| 80 | + const file = onSingleFileUpload(event); |
| 81 | + if (file) { |
| 82 | + this.selectedFile = file; |
| 83 | + } |
22 | 84 | }, |
23 | | - methods: { |
24 | | - createCategory: function (){ |
25 | | - console.log('Creating Category...'); |
26 | | - axios.post('/api/categories', { |
27 | | - name: this.name |
28 | | - }).then( function(response) { |
29 | | - console.log('New Category ID', response.data.data.id); |
30 | | - }).catch(function(err) { |
31 | | - console.log(err); |
32 | | - console.log('Category Route error Promise has been rejected!', err.response.data.errors); |
33 | | - }); |
| 85 | + submitForm: function () { |
| 86 | + const self = this; |
| 87 | +
|
| 88 | + // Check if the string is not empty |
| 89 | + if (typeof self.form.name === 'string' && self.form.name.length > 0) { |
| 90 | + self.isSubmitted = true; |
| 91 | +
|
| 92 | + const formData = new FormData(); |
| 93 | +
|
| 94 | + // Append formData fields |
| 95 | + formData.append('name', self.form.name); |
| 96 | + if (self.selectedFile) { |
| 97 | + formData.append('photo', self.selectedFile); |
34 | 98 | } |
| 99 | +
|
| 100 | + axios |
| 101 | + .post(`/api/categories`, formData, { |
| 102 | + headers: { 'Content-Type': 'multipart/form-data' }, |
| 103 | + onUploadProgress: uploadProgress |
| 104 | + }) |
| 105 | + .then(function (response) { |
| 106 | + console.log('response', response.data); |
| 107 | + self.isSubmitted = false; |
| 108 | + self.form.name = ''; |
| 109 | + self.form.photo = ''; |
| 110 | + alert('Data is successfully updated'); |
| 111 | + }) |
| 112 | + .catch(function (_) { |
| 113 | + self.isSubmitted = false; |
| 114 | + self.form.name = ''; |
| 115 | + self.form.photo = ''; |
| 116 | + alert('Oops! Something went wrong during posting the form. 🥺'); |
| 117 | + }); |
| 118 | + } else { |
| 119 | + alert( |
| 120 | + `Sorry We can't submit the form. The category 'name' is required. 🥺` |
| 121 | + ); |
| 122 | + } |
35 | 123 | } |
36 | | -} |
| 124 | + } |
| 125 | +}; |
37 | 126 | </script> |
0 commit comments