#84 refactor(frontend): extract methods to repository.

This commit is contained in:
Mono Mono 2025-01-11 10:37:17 -05:00
parent a4048473ae
commit 6d6322b0cd
5 changed files with 116 additions and 29 deletions

View File

@ -147,6 +147,7 @@
<script> <script>
import CustomerForm from './CreateCustomerModal.vue'; import CustomerForm from './CreateCustomerModal.vue';
import CasherModal from './CasherModal.vue'; import CasherModal from './CasherModal.vue';
import { inject } from 'vue';
export default { export default {
name: 'DonConfiao', name: 'DonConfiao',
@ -159,6 +160,7 @@
}, },
data() { data() {
return { return {
api: inject('api'),
valid: false, valid: false,
form_changed: false, form_changed: false,
show_alert_lines: false, show_alert_lines: false,
@ -259,8 +261,7 @@
this.purchase.saleline_set[index].measuring_unit = selectedProduct.measuring_unit; this.purchase.saleline_set[index].measuring_unit = selectedProduct.measuring_unit;
}, },
fetchClients() { fetchClients() {
fetch('/don_confiao/api/customers/') this.api.getCustomers()
.then(response => response.json())
.then(data => { .then(data => {
this.clients = data; this.clients = data;
}) })
@ -273,8 +274,7 @@
this.purchase.customer = newCustomer.id; this.purchase.customer = newCustomer.id;
}, },
fetchProducts() { fetchProducts() {
fetch('/don_confiao/api/products/') this.api.getProducts()
.then(response => response.json())
.then(data => { .then(data => {
this.products = data; this.products = data;
}) })
@ -283,8 +283,7 @@
}); });
}, },
fetchPaymentMethods() { fetchPaymentMethods() {
fetch('/don_confiao/payment_methods/all/select_format') this.api.getPaymentMethods()
.then(response => response.json())
.then(data => { .then(data => {
this.payment_methods = data; this.payment_methods = data;
}) })
@ -347,7 +346,7 @@
}, },
}, },
mounted() { mounted() {
this.fetchClients(); // Llama a fetchClients al montar el componente this.fetchClients();
} }
}; };
</script> </script>

View File

@ -9,11 +9,17 @@ import { registerPlugins } from '@/plugins'
// Components // Components
import App from './App.vue' import App from './App.vue'
import ApiImplementation from './services/api-implementation';
// Composables // Composables
import { createApp } from 'vue' import { createApp } from 'vue'
const app = createApp(App) process.env.API_IMPLEMENTATION = 'django';
let apiImplementation = new ApiImplementation();
const api = apiImplementation.getApi();
const app = createApp(App);
app.provide('api', api);
registerPlugins(app) registerPlugins(app)

View File

@ -0,0 +1,21 @@
import DjangoApi from './django-api';
import Api from './api';
class ApiImplementation {
constructor() {
const implementation = process.env.API_IMPLEMENTATION;
let apiImplementation;
if (implementation === 'django') {
apiImplementation = new DjangoApi();
} else {
throw new Error("API implementation don't configured");
}
this.api = new Api(apiImplementation);
}
getApi() {
return this.api;
}
}
export default ApiImplementation;

View File

@ -0,0 +1,19 @@
class Api {
constructor (apiImplementation) {
this.apiImplementation = apiImplementation;
}
getCustomers() {
return this.apiImplementation.getCustomers();
}
getProducts() {
return this.apiImplementation.getProducts();
}
getPaymentMethods() {
return this.apiImplementation.getPaymentMethods();
}
}
export default Api;

View File

@ -0,0 +1,42 @@
class DjangoApi {
getCustomers() {
return new Promise((resolve, reject) => {
fetch('/don_confiao/api/customers/')
.then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
}
getProducts() {
return new Promise((resolve, reject) => {
fetch('/don_confiao/api/products/')
.then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
}
getPaymentMethods() {
return new Promise((resolve, reject) => {
fetch('/don_confiao/payment_methods/all/select_format')
.then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
}
}
export default DjangoApi;