Skip to content

Commit

Permalink
fix router, premove products, add AddProduct, EditProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 28, 2023
1 parent 26ea163 commit e9283a8
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 44 deletions.
2 changes: 1 addition & 1 deletion ShoppingCart/Frondend/ecommerce-ui/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<router-link to="/admin/category">Category</router-link> |
<router-link to="/admin/category/add">Add</router-link> |
<router-link to="/admin/wishlist">Wishlist</router-link> |
<router-link to="/product">Product</router-link>
<router-link to="/admin/product">Product</router-link>
</nav>
<router-view/>
</div>
Expand Down
23 changes: 12 additions & 11 deletions ShoppingCart/Frondend/ecommerce-ui/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import EditCategory from "../views/Category/EditCategory";
import Category from "../views/Category/Category";
import Wishlist from "../views/Product/Wishlist.vue";
import ListProducts from '../views/Category/ListProducts.vue'

import AddProduct from '../views/Product/AddProduct.vue'
import EditProduct from '../views/Product/EditProduct.vue'
import Product from '../views/Product/Product.vue'
import ShowDetails from '../views/Product/ShowDetails.vue'

Expand Down Expand Up @@ -70,16 +71,16 @@ const routes = [
name: 'AdminProduct',
component: Product
},
// {
// path: '/admin/product/add',
// name: 'AddProduct',
// component: AddProduct
// },
// {
// path: '/admin/product/:id',
// name: 'EditProduct',
// component: EditProduct,
// },
{
path: '/admin/product/add',
name: 'AddProduct',
component: AddProduct
},
{
path: '/admin/product/:id',
name: 'EditProduct',
component: EditProduct,
},
{
path : '/product/show/:id',
name : 'ShowDetails',
Expand Down
129 changes: 129 additions & 0 deletions ShoppingCart/Frondend/ecommerce-ui/src/views/Product/AddProduct.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<template>
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h4 class="pt-3">Add new Product</h4>
</div>
</div>

<div class="row">
<div class="col-3"></div>
<div class="col-md-6 px-5 px-md-0">
<form>
<div class="form-group">
<label>Category</label>
<select class="form-control" v-model="categoryId" required>
<option
v-for="category of categories"
:key="category.id"
:value="category.id"
>
{{ category.categoryName }}
</option>
</select>
</div>
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" v-model="name" required />
</div>
<div class="form-group">
<label>Description</label>
<input
type="text"
class="form-control"
v-model="description"
required
/>
</div>
<div class="form-group">
<label>ImageURL</label>
<input
type="url"
class="form-control"
v-model="imageURL"
required
/>
</div>
<div class="form-group">
<label>Price</label>
<input
type="number"
class="form-control"
v-model="price"
required
/>
</div>
<button type="button" class="btn btn-primary" @click="addProduct">
Submit
</button>
</form>
</div>
<div class="col-3"></div>
</div>
</div>
</template>

<script>
import swal from "sweetalert";
import axios from "axios";
export default {
data() {
return {
id: null,
categoryId: null,
name: null,
description: null,
imageURL: null,
price: null,
};
},
props: ["baseURL", "products", "categories"],
methods: {
async addProduct() {
const newProduct = {
id: this.id,
categoryId: this.categoryId,
name: this.name,
description: this.description,
imageURL: this.imageURL,
price: this.price,
};
await axios({
method: "post",
url: "http://localhost:9999/" + "product/add",
data: JSON.stringify(newProduct),
headers: {
"Content-Type": "application/json",
},
})
.then((res) => {
//sending the event to parent to handle
console.log(res)
this.$emit("fetchData");
this.$router.push({ name: "AdminProduct" });
swal({
text: "Product Added Successfully!",
icon: "success",
closeOnClickOutside: false,
});
})
.catch((err) => console.log(err));
},
},
mounted() {
if (!localStorage.getItem("token")) {
this.$router.push({ name: "Signin" });
}
},
};
</script>

<style scoped>
h4 {
font-family: "Roboto", sans-serif;
color: #484848;
font-weight: 700;
}
</style>
118 changes: 118 additions & 0 deletions ShoppingCart/Frondend/ecommerce-ui/src/views/Product/EditProduct.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<template>
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h4 class="pt-3">Edit Product</h4>
</div>
</div>

<div class="row">
<div class="col-3"></div>
<div class="col-md-6 px-5 px-md-0">
<form v-if="product">
<div class="form-group">
<label>Category</label>
<select class="form-control" v-model="product.categoryId" required>
<option
v-for="category of categories"
:key="category.id"
:value="category.id"
>
{{ category.categoryName }}
</option>
</select>
</div>
<div class="form-group">
<label>Name</label>
<input
type="text"
class="form-control"
v-model="product.name"
required
/>
</div>
<div class="form-group">
<label>Description</label>
<input
type="text"
class="form-control"
v-model="product.description"
required
/>
</div>
<div class="form-group">
<label>ImageURL</label>
<input
type="url"
class="form-control"
v-model="product.imageURL"
required
/>
</div>
<div class="form-group">
<label>Price</label>
<input
type="number"
class="form-control"
v-model="product.price"
required
/>
</div>
<button type="button" class="btn btn-primary" @click="editProduct">
Submit
</button>
</form>
</div>
<div class="col-3"></div>
</div>
</div>
</template>

<script>
import swal from "sweetalert";
import axios from "axios";
export default {
data() {
return {
product: null,
};
},
props: ["baseURL", "products", "categories"],
methods: {
async editProduct() {
axios
//post(this.baseURL + "product/update/" + this.id, this.product)
.post("localhost:9999" + "product/update/" + this.id, this.product)
.then((res) => {
console.log(res)
//sending the event to parent to handle
this.$emit("fetchData");
this.$router.push({ name: "AdminProduct" });
swal({
text: "Product Updated Successfully!",
icon: "success",
closeOnClickOutside: false,
});
})
.catch((err) => console.log("err", err));
},
},
mounted() {
// if (!localStorage.getItem("token")) {
// this.$router.push({ name: "Signin" });
// return;
// }
this.id = this.$route.params.id;
this.product = this.products.find((product) => product.id == this.id);
},
};
</script>

<style scoped>
h4 {
font-family: "Roboto", sans-serif;
color: #484848;
font-weight: 700;
}
</style>
10 changes: 5 additions & 5 deletions ShoppingCart/Frondend/ecommerce-ui/src/views/Product/Product.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
name: 'Product',
components : {ProductBox},
props : [ "baseURL" , "products" ],
mounted(){
if (this.$route.name=='AdminProduct' && !localStorage.getItem('token')) {
this.$router.push({name : 'Signin'});
}
}
// mounted(){
// if (this.$route.name=='AdminProduct' && !localStorage.getItem('token')) {
// this.$router.push({name : 'Signin'});
// }
// }
}
</script>

Expand Down
27 changes: 0 additions & 27 deletions ShoppingCart/Frondend/ecommerce-ui/src/views/Product/Products.vue

This file was deleted.

0 comments on commit e9283a8

Please sign in to comment.