diff --git a/src/pages/listas/indexPage.vue b/src/pages/listas/indexPage.vue index ed3114a..1d13a62 100644 --- a/src/pages/listas/indexPage.vue +++ b/src/pages/listas/indexPage.vue @@ -16,7 +16,10 @@ label="Item" @keyup.enter="cargarProducto()" :rules="[ - (val) => (val != null && val.length >= 3) || 'Mínimo 3 caracteres', + (val) => + (val != null && val.length >= 3) || + (val != null && val.length == 0) || + 'Mínimo 3 caracteres', ]" /> @@ -46,22 +49,35 @@ const producto = ref(""); const cargarProducto = async () => { if (producto.value.length >= 3) { // Busca en todos, si no está crea y añade - const result = await listaStore.pb.collection("items").getFullList(); - const busqueda = result.filter( - (element) => element.nombre == producto.value + const res = await listaStore.pb.collection("items").getFullList(); + const busqueda = res.filter( + (element) => element.nombre == producto.value.trim() ); if (busqueda.length == 0) { const data = { - nombre: producto.value, + nombre: producto.value.trim(), cantidad: 1, }; - await listaStore.pb.collection("items").create(data); - lista.value.items.push(data); + const item = await listaStore.pb.collection("items").create(data); + lista.value.items.push(item.id); await listaStore.pb .collection("lista") .update(lista.value.id, lista.value); + } else { + // Si está, comprueba que no esté en la lista y lo añade + if (lista.value.items.indexOf(busqueda[0].id) == -1) { + lista.value.items.push(busqueda[0].id); + await listaStore.pb + .collection("lista") + .update(lista.value.id, lista.value); + } } - // Si está, comprueba que no esté en la lista y lo añade + const arrayIdItem = lista.value.items; + const result = await listaStore.pb.collection("items").getFullList(); + items.value = result.filter( + (element) => arrayIdItem.indexOf(element.id) != -1 + ); + producto.value = ""; console.log(producto.value, busqueda); } };