|
@@ -7,6 +7,7 @@
|
|
|
let products = [];
|
|
let products = [];
|
|
|
let categories = [];
|
|
let categories = [];
|
|
|
|
|
|
|
|
|
|
+ let noCategory = false;
|
|
|
let isAddingProduct = false;
|
|
let isAddingProduct = false;
|
|
|
let editingProductId = null;
|
|
let editingProductId = null;
|
|
|
let productFormData = {
|
|
let productFormData = {
|
|
@@ -15,6 +16,7 @@
|
|
|
price: 0,
|
|
price: 0,
|
|
|
sendToKitchen: false
|
|
sendToKitchen: false
|
|
|
};
|
|
};
|
|
|
|
|
+ let associatedProduct = false;
|
|
|
|
|
|
|
|
let isAddingCategory = false;
|
|
let isAddingCategory = false;
|
|
|
let newCategoryName = '';
|
|
let newCategoryName = '';
|
|
@@ -43,26 +45,45 @@
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (editingProductId) {
|
|
|
|
|
- const index = products.findIndex((p) => p.id === editingProductId);
|
|
|
|
|
- if (index !== -1) {
|
|
|
|
|
- products[index] = {
|
|
|
|
|
- ...products[index],
|
|
|
|
|
- ...productFormData
|
|
|
|
|
- };
|
|
|
|
|
- products = products; // Força reatividade
|
|
|
|
|
- console.log('Produto atualizado com sucesso!');
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- const newProduct = {
|
|
|
|
|
- id: Date.now(),
|
|
|
|
|
- ...productFormData
|
|
|
|
|
- };
|
|
|
|
|
- products = [...products, newProduct];
|
|
|
|
|
- console.log('Produto criado com sucesso!');
|
|
|
|
|
|
|
+ const categoryObj = categories.find((c) => c.name === productFormData.category);
|
|
|
|
|
+ if (!categoryObj) {
|
|
|
|
|
+ console.error('Categoria inválida');
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- resetProductForm();
|
|
|
|
|
|
|
+ const payload = {
|
|
|
|
|
+ product_name: productFormData.name,
|
|
|
|
|
+ product_price: Number(productFormData.price),
|
|
|
|
|
+ category_id: categoryObj.id,
|
|
|
|
|
+ company_id: 1
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const requestOptions = {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify(payload),
|
|
|
|
|
+ redirect: 'follow'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/product/create', requestOptions)
|
|
|
|
|
+ .then((response) => response.json())
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ const newProduct = {
|
|
|
|
|
+ id: res.data?.product_id || Date.now(),
|
|
|
|
|
+ ...productFormData
|
|
|
|
|
+ };
|
|
|
|
|
+ products = [...products, newProduct];
|
|
|
|
|
+ console.log('Produto criado com sucesso!');
|
|
|
|
|
+ resetProductForm();
|
|
|
|
|
+ location.reload();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao criar produto:', res.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((error) => {
|
|
|
|
|
+ console.error('Erro na requisição:', error);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function handleEditProduct(product) {
|
|
function handleEditProduct(product) {
|
|
@@ -88,46 +109,137 @@
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- categories = [...categories, { name: newCategoryName }];
|
|
|
|
|
- console.log('Categoria criada com sucesso');
|
|
|
|
|
- newCategoryName = '';
|
|
|
|
|
- isAddingCategory = false;
|
|
|
|
|
|
|
+ const requestOptions = {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ category_name: newCategoryName,
|
|
|
|
|
+ category_is_kitchen: false,
|
|
|
|
|
+ company_id: 1
|
|
|
|
|
+ }),
|
|
|
|
|
+ redirect: 'follow'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/category/create', requestOptions)
|
|
|
|
|
+ .then((response) => response.json())
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ categories = [...categories, { name: newCategoryName }];
|
|
|
|
|
+ console.log('Categoria criada com sucesso');
|
|
|
|
|
+ newCategoryName = '';
|
|
|
|
|
+ isAddingCategory = false;
|
|
|
|
|
+ location.reload();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao criar categoria:', res.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((error) => {
|
|
|
|
|
+ console.error('Erro na requisição:', error);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function handleDeleteCategory(name) {
|
|
function handleDeleteCategory(name) {
|
|
|
- categories = categories.filter((c) => c.name !== name);
|
|
|
|
|
- products = products.filter((p) => p.category !== name);
|
|
|
|
|
- if (selectedCategoryFilter === name) selectedCategoryFilter = '';
|
|
|
|
|
|
|
+ const hasAssociatedProducts = products.some((p) => p.category === name);
|
|
|
|
|
+
|
|
|
|
|
+ if (hasAssociatedProducts) {
|
|
|
|
|
+ associatedProduct = true;
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/category/delete', {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ category_name: name,
|
|
|
|
|
+ company_id: 1
|
|
|
|
|
+ }),
|
|
|
|
|
+ redirect: 'follow'
|
|
|
|
|
+ })
|
|
|
|
|
+ .then((response) => response.json())
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ categories = categories.filter((c) => c.name !== name);
|
|
|
|
|
+ if (selectedCategoryFilter === name) selectedCategoryFilter = '';
|
|
|
|
|
+ console.log('Categoria deletada com sucesso!');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao deletar categoria:', res.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((error) => {
|
|
|
|
|
+ console.error('Erro na requisição:', error);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function handleDeleteProduct(id) {
|
|
|
|
|
- products = products.filter((p) => p.id !== id);
|
|
|
|
|
|
|
+ function handleDeleteProduct(productName) {
|
|
|
|
|
+ const requestOptions = {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ product_name: productName,
|
|
|
|
|
+ company_id: 1
|
|
|
|
|
+ }),
|
|
|
|
|
+ redirect: 'follow'
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/product/delete', requestOptions)
|
|
|
|
|
+ .then((response) => response.json())
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ products = products.filter((p) => p.name !== productName);
|
|
|
|
|
+ console.log('Produto deletado com sucesso!');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao deletar produto:', res.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((error) => {
|
|
|
|
|
+ console.error('Erro na requisição:', error);
|
|
|
|
|
+ });
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
onMount(() => {
|
|
|
- const requestOptions = { method: 'GET', redirect: 'follow' };
|
|
|
|
|
|
|
+ const companyId = 1;
|
|
|
|
|
+
|
|
|
|
|
+ const requestOptions = {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
+ redirect: 'follow',
|
|
|
|
|
+ body: JSON.stringify({ company_id: companyId })
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
- fetch('https://fakestoreapi.com/users', requestOptions)
|
|
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/category/get', requestOptions)
|
|
|
.then((response) => response.json())
|
|
.then((response) => response.json())
|
|
|
- .then((data) => {
|
|
|
|
|
- categories = data.map((categorie) => ({
|
|
|
|
|
- name: categorie.username
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ categories = res.data.map((categorie) => ({
|
|
|
|
|
+ id: categorie.category_id,
|
|
|
|
|
+ name: categorie.category_name,
|
|
|
|
|
+ isKitchen: categorie.category_is_kitchen === 1
|
|
|
|
|
+ }));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao carregar categorias:', res.msg);
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
- .catch((error) => console.error(error));
|
|
|
|
|
|
|
+ .catch((error) => console.error('Erro ao buscar categorias:', error));
|
|
|
|
|
|
|
|
- fetch('https://fakestoreapi.com/users', requestOptions)
|
|
|
|
|
|
|
+ fetch('https://dev2.mixtech.dev.br/product/get', requestOptions)
|
|
|
.then((response) => response.json())
|
|
.then((response) => response.json())
|
|
|
- .then((data) => {
|
|
|
|
|
- products = data.map((item, index) => ({
|
|
|
|
|
- id: Date.now() + index,
|
|
|
|
|
- name: item.address.street,
|
|
|
|
|
- category: item.username,
|
|
|
|
|
- price: Math.floor(Math.random() * 50 + 10),
|
|
|
|
|
- sendToKitchen: Math.random() > 0.5
|
|
|
|
|
- }));
|
|
|
|
|
|
|
+ .then((res) => {
|
|
|
|
|
+ if (res.status === 'ok') {
|
|
|
|
|
+ products = res.data.map((item) => {
|
|
|
|
|
+ const category = categories.find((c) => c.id === item.category_id);
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: item.product_id,
|
|
|
|
|
+ name: item.product_name,
|
|
|
|
|
+ category: category?.name || 'Sem categoria',
|
|
|
|
|
+ price: Number(item.product_price),
|
|
|
|
|
+ sendToKitchen: category?.isKitchen || false
|
|
|
|
|
+ };
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.error('Erro ao carregar produtos:', res.msg);
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
- .catch((error) => console.error(error));
|
|
|
|
|
|
|
+ .catch((error) => console.error('Erro ao buscar produtos:', error));
|
|
|
});
|
|
});
|
|
|
</script>
|
|
</script>
|
|
|
|
|
|
|
@@ -207,6 +319,13 @@
|
|
|
</button>
|
|
</button>
|
|
|
</div>
|
|
</div>
|
|
|
{/if}
|
|
{/if}
|
|
|
|
|
+ {#if associatedProduct}
|
|
|
|
|
+ <div class="p-4">
|
|
|
|
|
+ <p class="w-full rounded-md bg-yellow-600 p-1 text-sm">
|
|
|
|
|
+ Delete os produtos associados a esta categoria
|
|
|
|
|
+ </p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {/if}
|
|
|
{/if}
|
|
{/if}
|
|
|
</div>
|
|
</div>
|
|
|
{/if}
|
|
{/if}
|
|
@@ -216,11 +335,21 @@
|
|
|
<div class="overflow-hidden rounded-lg bg-gray-800 shadow-lg lg:col-span-2">
|
|
<div class="overflow-hidden rounded-lg bg-gray-800 shadow-lg lg:col-span-2">
|
|
|
<div class="flex items-center justify-between bg-gray-700 p-4">
|
|
<div class="flex items-center justify-between bg-gray-700 p-4">
|
|
|
<h2 class="text-lg font-semibold">Produtos</h2>
|
|
<h2 class="text-lg font-semibold">Produtos</h2>
|
|
|
|
|
+ {#if noCategory}
|
|
|
|
|
+ <div>
|
|
|
|
|
+ <p class="w-full rounded-md bg-yellow-600 p-1 text-sm">Primeiro crie uma categoria</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ {/if}
|
|
|
<button
|
|
<button
|
|
|
on:click={() => {
|
|
on:click={() => {
|
|
|
- if (!categories.length) return;
|
|
|
|
|
- resetProductForm(); // já atribui a categoria
|
|
|
|
|
- isAddingProduct = true;
|
|
|
|
|
|
|
+ if (!categories.length) {
|
|
|
|
|
+ noCategory = true;
|
|
|
|
|
+ return;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ noCategory = false;
|
|
|
|
|
+ resetProductForm();
|
|
|
|
|
+ isAddingProduct = true;
|
|
|
|
|
+ }
|
|
|
}}
|
|
}}
|
|
|
class="rounded-lg bg-emerald-600 p-1.5 hover:bg-emerald-700"
|
|
class="rounded-lg bg-emerald-600 p-1.5 hover:bg-emerald-700"
|
|
|
>
|
|
>
|
|
@@ -309,7 +438,7 @@
|
|
|
</p>
|
|
</p>
|
|
|
</div>
|
|
</div>
|
|
|
<button
|
|
<button
|
|
|
- on:click|stopPropagation={() => handleDeleteProduct(product.id)}
|
|
|
|
|
|
|
+ on:click|stopPropagation={() => handleDeleteProduct(product.name)}
|
|
|
class="rounded-lg p-1.5 text-red-400 hover:bg-red-900/20"
|
|
class="rounded-lg p-1.5 text-red-400 hover:bg-red-900/20"
|
|
|
>
|
|
>
|
|
|
<img src={trash_icon} alt="Deletar" class="h-4 w-4" />
|
|
<img src={trash_icon} alt="Deletar" class="h-4 w-4" />
|