Skip to content

Commit

Permalink
Merge pull request #5 from Greeshmabk/deleteEDitBranch
Browse files Browse the repository at this point in the history
Delete e dit branch
  • Loading branch information
Greeshmabk authored Sep 5, 2024
2 parents 6f5dd73 + 5b62ecb commit 9096dee
Show file tree
Hide file tree
Showing 5 changed files with 317 additions and 100 deletions.
113 changes: 14 additions & 99 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,121 +1,36 @@
<script >
import AddProduct from './components/addProduct.vue'
import { library } from '@fortawesome/fontawesome-svg-core';
import { faTrash , faEdit } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import ProductList from './components/productList.vue'
import './assets/tailwind.css';
library.add(faTrash , faEdit);
import ShoppingList from './components/shoppingList.vue';
export default{
name: 'App',
components:{
AddProduct,
FontAwesomeIcon,
},
data() {
return {
shoppingList: this.fetchData(),
editShow:false,
editItem:''
};
},
methods: {
fetchData() {
fetch('https://shoppinglist.cosmos.cboxlab.com/api/v1/shopping-list', {
method: "GET",
})
.then((response) => {
response.json().then((data) => {
this.shoppingList=data;
});
})
.catch((err) => {
console.error(err);
});
},
async deleteData(id){
try {
const response = await fetch(`https://shoppinglist.cosmos.cboxlab.com/api/v1/shopping-list/${id}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(`Product with ID ${id} deleted.`);
this.fetchData()
} catch (error) {
console.error('There was an error deleting the product:', error);
}
},
async editData(id,productName){
try {
const response = await fetch(`https://shoppinglist.cosmos.cboxlab.com/api/v1/shopping-list/${id}`, {
method: 'POST',
body: JSON.stringify({
name: productName
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(`Product with ID ${id} edited .`);
this. fetchData()
} catch (error) {
console.error('There was an error editing the product:', error);
}
}
ProductList,
ShoppingList
},
}
</script>

<template>
<div class="shoppingList container mx-auto px-8 py-8">
<div class=" pb-8 flex">
<h1 class="text-3xl font-bold underline headerPart"><b>Shopping List</b></h1>

</div>

<div>
<AddProduct></AddProduct>
<div class="row">
<table class="min-w-full bg-white">

<tbody>
<tr v-for="item in shoppingList.items" :key="index">
<td class="border px-4 py-2">{{ item.name }}</td>

<button @click="editShow=true , editItem=item.id" class="px-2 py-1 bg-yellow-500 text-white rounded mr-2">
<font-awesome-icon :icon="['fas', 'edit']" />
</button>
<button @click="deleteData(item.id)" class="px-2 py-1 bg-red-600 text-white rounded">
<font-awesome-icon :icon="['fas', 'trash']" />

</button>

</tr>
</tbody>
</table>

<form v-if="editShow">
<label class="">Product Name:</label>
<input class="editProduct" type="text" v-model="product">
<button @click="editData(editItem,product)">Save</button>
</form>

</div>
<div class=" shoppingList ">
<h1 class="text-3xl font-bold underline mx-auto headerPart"><b>Product-Shopping List</b></h1>

<div class=" container flex justify-center px-8 py-8 ">
<ProductList></ProductList>
<ShoppingList></ShoppingList>
</div>
</div>
</div>
</template>

<style scoped>
.shoppingList{
background-color: white;
background-color: #FFE0BD;
color: black;
min-width:1200px;
}
.headerPart {
line-height: 2.5;
Expand Down
2 changes: 1 addition & 1 deletion src/assets/main.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@import './base.css';

#app {
max-width: 1280px;

margin: 0 auto;
padding: 2rem;
font-weight: normal;
Expand Down
63 changes: 63 additions & 0 deletions src/components/addInventory.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<script>
export default{
name: 'Add Product',
components:{},
data(){
return{
show: false,
formattedDate: '',
expiryDate:''
}
},
methods:{
async addData(productName,storageLocation,quantity,unit,date){
this.expiryDate = new Date(date).toISOString();
try {
const response = await fetch('https://shoppinglist.cosmos.cboxlab.com/api/v1/inventory', {
method: 'POST',
body: JSON.stringify({
name: productName,
quntity:quantity,
storageLocation:storageLocation,
unit:unit,
expiry: this.expiryDate
})
});
console.log('Product saved:', response.data);
} catch (error) {
console.error('There was an error saving thstorageLocatione product:', error);
}
this.show= false;
}
}
}
</script>
<template>
<div>
<div class="button-container py-4">
<button class="dark:bg-slate-900 dark:text-white " @click="show=true">Add Product</button>
</div>
<div v-if="show" class="modal-overlay" @click.self="close">
<div class="modal-content">
<form>
<label class="">Product Name:</label>
<input type="text" v-model="product">
<label class="">Storage Location:</label>
<input type="text" v-model="storageLocation">
<label class="">Quantity:</label>
<input type="number" v-model="quantity">
<label class="">Unit:</label>
<input type="text" v-model="unit">
<label class="">Expiry Date:</label>
<input type="date" v-model="date">
<button @click="addData(product,storageLocation,quantity,unit,date)">Save</button>
</form>
</div>
</div>
</div>
</template>
129 changes: 129 additions & 0 deletions src/components/productList.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<script>
import AddInventory from './addInventory.vue'
import '../assets/tailwind.css';
import { library } from '@fortawesome/fontawesome-svg-core';
import { faTrash , faEdit } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
library.add(faTrash , faEdit);
export default{
name: 'productList',
components:{
AddInventory,
FontAwesomeIcon
},
data(){
return{
shoppingList: this.fetchData(),
editShow:false,
editItem:{}
};
},
methods:{
fetchData() {
fetch('https://shoppinglist.cosmos.cboxlab.com/api/v1/inventory', {
method: "GET",
})
.then((response) => {
response.json().then((data) => {
this.shoppingList=data.items;
});
})
.catch((err) => {
console.error(err);
});
},
async deleteData(id){
try {
const response = await fetch(`https://shoppinglist.cosmos.cboxlab.com/api/v1/inventory/${id}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(`Product with ID ${id} deleted.`);
this.fetchData()
} catch (error) {
console.error('There was an error deleting the product:', error);
}
},
async editData(editItemDetails){
try {
const response = await fetch(`https://shoppinglist.cosmos.cboxlab.com/api/v1/inventory/${editItemDetails.id}`, {
method: 'POST',
body: JSON.stringify({
name: editItemDetails.name,
quntity:editItemDetails.quntity,
storageLocation:editItemDetails.storageLocation,
unit:editItemDetails.unit,
expiry:editItemDetails.expiry
})
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
console.log(`Product with ID ${id} edited .`);
this. fetchData()
} catch (error) {
console.error('There was an error editing the product:', error);
}
this.editItem={};
}
}
}
</script>

<template>
<div>
<div class=" pb-8 flex">
<h1 class="text-3xl font-bold underline mx-auto headerPart"><b>Product List</b></h1>
</div>
<div>
<AddInventory></AddInventory>
<div class="row">
<table v-if="!editShow">
<thead>
<th>
<td class="border px-4 py-2 mx-auto">Item Name</td>
<td class="border px-4 py-2 mx-auto">Expiry date</td>
<td class="border px-4 py-2 mx-auto">Storage Location</td>
<td class="border px-4 py-2 mx-auto">Quantity</td>
</th>
</thead>
<tbody>
<tr v-for="item in shoppingList" :key="index">
<td class="border px-4 py-2 mx-auto">{{ item.name }}</td>
<td class="border px-4 py-2 mx-auto">{{ item.expiry }}</td>
<td class="border px-4 py-2 mx-auto">{{ item.storageLocation }}</td>
<td class="border px-4 py-2 mx-auto" >{{ item.quntity+ item.unit}}</td>
<button @click="editShow=true , editItem=item" class="px-2 py-1 bg-yellow-500 text-white rounded mr-2">
<font-awesome-icon :icon="['fas', 'edit']" />
</button>
<button @click="deleteData(item.id)" class="px-2 py-1 bg-red-600 text-white rounded">
<font-awesome-icon :icon="['fas', 'trash']" />

</button>
</tr>
</tbody>
</table>
<form v-if="editShow">
<label class="">Product Name:</label>
<input type="text" v-model="editItem.name">
<label class="">Storage Location:</label>
<input type="text" v-model="editItem.storageLocation ">
<label class="">Quantity:</label>
<input type="number" v-model="editItem.quntity">
<label class="">Unit:</label>
<input type="text" v-model="editItem.unit">
<label class="">Expiry Date:</label>
{{ editItem.expiry }}
<input type="date" v-model="editItem.expiry">
<button @click="editData(editItem)">Save</button>
</form>
</div>
</div>
</div>
</template>
Loading

0 comments on commit 9096dee

Please sign in to comment.