#84 refactor(frontend): send purchase moved to repository.

This commit is contained in:
Mono Mono 2025-01-11 11:34:37 -05:00
parent 6d6322b0cd
commit fcb83d05fb
3 changed files with 39 additions and 22 deletions

View File

@ -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(() => {

View File

@ -14,6 +14,10 @@ class Api {
getPaymentMethods() {
return this.apiImplementation.getPaymentMethods();
}
createPurchase(purchase) {
return this.apiImplementation.createPurchase(purchase);
}
}
export default Api;

View File

@ -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;