Generado repositorio para consultar la api #86 #87
| @@ -187,27 +187,14 @@ | ||||
|       async submit() { | ||||
|         this.$refs.taker.validate(); | ||||
|         if (this.valid) { | ||||
|           try { | ||||
|             const response = await fetch('/don_confiao/reconciliate_jar', { | ||||
|               method: 'POST', | ||||
|               headers: { | ||||
|                 'Content-Type': 'application/json' | ||||
|               }, | ||||
|               body: JSON.stringify(this.reconciliation), | ||||
|             }); | ||||
|             if (response.ok) { | ||||
|               const data = await response.json(); | ||||
|               console.log('Cuadre enviado:', data); | ||||
|               this.$router.push({path: "/"}); | ||||
|             } else { | ||||
|               console.error('Error al enviar el cuadre', response.statusText); | ||||
|  | ||||
|             } | ||||
|           } catch (error) { | ||||
|             console.error('Error de red:', error); | ||||
|           } | ||||
|           this.api.createReconciliationJar(this.reconciliation) | ||||
|               .then(data => { | ||||
|                 console.log('Cuadre enviado:', data); | ||||
|                 this.$router.push({path: "/"}); | ||||
|               }) | ||||
|               .catch(error => console.error('Error:', error)); | ||||
|         } | ||||
|       }, | ||||
|       } | ||||
|     }, | ||||
|   } | ||||
| </script> | ||||
|   | ||||
| @@ -26,6 +26,10 @@ class Api { | ||||
|   createPurchase(purchase) { | ||||
|     return this.apiImplementation.createPurchase(purchase); | ||||
|   } | ||||
|  | ||||
|   createReconciliationJar(reconciliation) { | ||||
|     return this.apiImplementation.createReconciliationJar(reconciliation); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default Api; | ||||
|   | ||||
| @@ -25,28 +25,13 @@ class DjangoApi { | ||||
|   } | ||||
|  | ||||
|   createPurchase(purchase) { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       console.log('compra a enviar:', purchase); | ||||
|       fetch('/don_confiao/api/sales/', { | ||||
|                    method: 'POST', | ||||
|                    headers: { | ||||
|                        'Content-Type': 'application/json', | ||||
|                    }, | ||||
|                    body: JSON.stringify(purchase), | ||||
|       }).then(response => { | ||||
|         if (!response.ok) { | ||||
|           reject(new Error(`Error ${response.status}: ${response.statusText}`)); | ||||
|         } else { | ||||
|           response.json().then(data => { | ||||
|             if (!data) { | ||||
|               reject(new Error('La respuesta no es un JSON válido')); | ||||
|             } else { | ||||
|               resolve(data); | ||||
|             } | ||||
|           }); | ||||
|         } | ||||
|       }).catch(error => reject(error)); | ||||
|     }); | ||||
|     const url = '/don_confiao/api/sales/'; | ||||
|     return this.postRequest(url, purchase); | ||||
|   } | ||||
|  | ||||
|   createReconciliationJar(reconciliation) { | ||||
|     const url = '/don_confiao/reconciliate_jar'; | ||||
|     return this.postRequest(url, reconciliation); | ||||
|   } | ||||
|  | ||||
|   getRequest(url) { | ||||
| @@ -60,7 +45,35 @@ class DjangoApi { | ||||
|           reject(error); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|  | ||||
|   } | ||||
|  | ||||
|   postRequest(url, content) { | ||||
|     return new Promise((resolve, reject) => { | ||||
|       fetch(url, { | ||||
|         method: 'POST', | ||||
|         headers: { | ||||
|           'Content-Type': 'application/json', | ||||
|         }, | ||||
|         body: JSON.stringify(content) | ||||
|       }) | ||||
|         .then(response => { | ||||
|           if (!response.ok) { | ||||
|             reject(new Error(`Error ${response.status}: ${response.statusText}`)); | ||||
|           } else { | ||||
|             response.json().then(data => { | ||||
|               if (!data) { | ||||
|                 reject(new Error('La respuesta no es un JSON válido')); | ||||
|               } else { | ||||
|                 resolve(data); | ||||
|               } | ||||
|             }); | ||||
|           } | ||||
|         }) | ||||
|         .catch(error => reject(error)); | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default DjangoApi; | ||||
|   export default DjangoApi; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user