Commit b5a5e77f authored by Bui Duc Tuan's avatar Bui Duc Tuan

fix create component product

parent 2fc5a026
...@@ -29,11 +29,11 @@ ...@@ -29,11 +29,11 @@
<router-link <router-link
tag="span" tag="span"
class="icon_action icon_edit" class="icon_action icon_edit"
:to="{ name: 'editProduct', params: { id: index } }" :to="{ name: 'editProduct', params: { id: item.id } }"
> >
<ion-icon name="create-outline"></ion-icon> <ion-icon name="create-outline"></ion-icon>
</router-link> </router-link>
<span class="icon_action icon_del" @click="removeProduct(index)" <span class="icon_action icon_del" @click="removeProduct(item.id)"
><ion-icon name="trash-outline"></ion-icon ><ion-icon name="trash-outline"></ion-icon
></span> ></span>
</td> </td>
...@@ -60,22 +60,18 @@ export default { ...@@ -60,22 +60,18 @@ export default {
}; };
}, },
mounted: function () { created: function () {
if (!localStorage.getItem("products")) { let uri = `http://127.0.0.1:8000/api/products/`;
this.axios.get("http://127.0.0.1:8000/api/products/").then((response) => { this.axios.get(uri).then((response) => {
this.products = response.data this.products = response.data;
localStorage.setItem("products", JSON.stringify(this.products));
}); });
}else{
this.products = JSON.parse(localStorage.getItem("products"));
}
}, },
methods: { methods: {
removeProduct(index) { removeProduct(id) {
if (confirm("Are you sure you want to delete products?")) { if (confirm("Are you sure you want to delete products?")) {
this.products.splice(index, 1); let uri = `http://127.0.0.1:8000/api/products/${id}`;
localStorage.setItem("products", JSON.stringify(this.products)); this.axios.delete(uri);
this.$alertify.error("Delete product successfully"); this.$alertify.error("Delete product successfully");
} }
}, },
......
...@@ -43,40 +43,31 @@ Vue.use(VueAlertify); ...@@ -43,40 +43,31 @@ Vue.use(VueAlertify);
export default { export default {
data: function () { data: function () {
return { return {
name: '', name: "",
color: '', color: "",
price: '', price: "",
products: [], product: {},
}; };
}, },
created: function () {
if (localStorage.getItem("products")) {
this.products = JSON.parse(localStorage.getItem("products"));
}
},
methods: { methods: {
submit() { submit() {
if (this.name == "" || this.price == "" || this.color == "") {
if (this.name == '' || this.price == '' || this.color == '') {
alert("Please fill it out completely form!!"); alert("Please fill it out completely form!!");
return; return;
}else if(isNaN(this.price)){ } else if (isNaN(this.price)) {
alert("price only accepts numeric data!!"); alert("price only accepts numeric data!!");
return; return;
} }
this.product.name = this.name;
this.product.color = this.color;
this.product.price = this.price;
this.products.push({ let uri = `http://127.0.0.1:8000/api/products/`;
name: this.name, this.axios.post(uri, this.product)
color: this.color,
price: this.price
});
localStorage.setItem("products", JSON.stringify(this.products));
this.name = '';
this.color = '';
this.price = '';
this.$router.push({ name : 'product'}); this.$router.push({ name : 'product'});
this.$alertify.success("Add task successfully"); this.$alertify.success("Add product successfully");
}, },
}, },
}; };
......
...@@ -47,25 +47,20 @@ export default { ...@@ -47,25 +47,20 @@ export default {
name: "", name: "",
color: "", color: "",
price: "", price: "",
products: [], product: null,
}; };
}, },
created: function () { created: function () {
if (localStorage.getItem("products")) { let uri = `http://127.0.0.1:8000/api/products/${this.id}`;
this.products = JSON.parse(localStorage.getItem("products")); this.axios.get(uri).then((response) => {
} this.product = response.data;
}, this.name = this.product.name;
created: function () { this.color = this.product.color;
this.products = JSON.parse(localStorage.getItem("products")); this.price = this.product.price;
}).catch(() => {
if (this.products[this.id]) {
this.name = this.products[this.id].name;
this.color = this.products[this.id].color;
this.price = this.products[this.id].price;
} else {
this.$router.push("/product"); this.$router.push("/product");
this.$alertify.error('Product not found!'); this.$alertify.error('Product not found!');
} });
}, },
methods: { methods: {
submit() { submit() {
...@@ -77,16 +72,15 @@ export default { ...@@ -77,16 +72,15 @@ export default {
return; return;
} }
this.products[this.id].name = this.name this.product.name = this.name;
this.products[this.id].color = this.color this.product.color = this.color;
this.products[this.id].price = this.price this.product.price = this.price;
let uri = `http://127.0.0.1:8000/api/products/${this.id}`;
this.axios.put(uri, this.product);
localStorage.setItem("products", JSON.stringify(this.products));
this.name = "";
this.color = "";
this.price = "";
this.$router.push({ name: "product" }); this.$router.push({ name: "product" });
this.$alertify.warning("Add task successfully"); this.$alertify.warning("Edit product successfully");
}, },
}, },
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment