Generado repositorio para consultar la api #86 #87
| @@ -310,27 +310,15 @@ | ||||
|      async submit() { | ||||
|        this.$refs.purchase.validate(); | ||||
|          if (this.valid) { | ||||
|            try { | ||||
|                const response = await fetch('/don_confiao/api/sales/', { | ||||
|                    method: 'POST', | ||||
|                    headers: { | ||||
|                        'Content-Type': 'application/json', | ||||
|                    }, | ||||
|                    body: JSON.stringify(this.purchase), | ||||
|                }); | ||||
|                if (response.ok) { | ||||
|                    const data = await response.json(); | ||||
|            this.api.createPurchase(this.purchase) | ||||
|                .then(data => { | ||||
|                  console.log('Compra enviada:', data); | ||||
|                  this.$router.push({ | ||||
|                    path: "/summary_purchase", | ||||
|                    query : {id: parseInt(data.id)} | ||||
|                  }); | ||||
|                } else { | ||||
|                    console.error('Error al enviar la compra:', response.statusText); | ||||
|                } | ||||
|            } catch (error) { | ||||
|                console.error('Error de red:', error); | ||||
|            } | ||||
|                }) | ||||
|            .catch(error => console.error('Error al enviarl la compra:', error)); | ||||
|        } else { | ||||
|          this.show_alert_purchase = true; | ||||
|            setTimeout(() => { | ||||
|   | ||||
| @@ -14,6 +14,10 @@ class Api { | ||||
|   getPaymentMethods() { | ||||
|     return this.apiImplementation.getPaymentMethods(); | ||||
|   } | ||||
|  | ||||
|   createPurchase(purchase) { | ||||
|     return this.apiImplementation.createPurchase(purchase); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default Api; | ||||
|   | ||||
| @@ -37,6 +37,31 @@ 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)); | ||||
|     }); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default DjangoApi; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user