move frontend to upper dir.
This commit is contained in:
		
							
								
								
									
										21
									
								
								src/services/api-implementation.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/services/api-implementation.js
									
									
									
									
									
										Normal 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; | ||||
							
								
								
									
										51
									
								
								src/services/api.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/services/api.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| class Api { | ||||
|   constructor (apiImplementation) { | ||||
|     this.apiImplementation = apiImplementation; | ||||
|   } | ||||
|  | ||||
|   getCustomers() { | ||||
|     return this.apiImplementation.getCustomers(); | ||||
|   } | ||||
|  | ||||
|   getProducts() { | ||||
|     return this.apiImplementation.getProducts(); | ||||
|   } | ||||
|  | ||||
|   getPaymentMethods() { | ||||
|     return this.apiImplementation.getPaymentMethods(); | ||||
|   } | ||||
|  | ||||
|   getSummaryPurchase(purchaseId) { | ||||
|     return this.apiImplementation.getSummaryPurchase(purchaseId); | ||||
|   } | ||||
|  | ||||
|   getPurchasesForReconciliation() { | ||||
|     return this.apiImplementation.getPurchasesForReconciliation(); | ||||
|   } | ||||
|  | ||||
|   getListReconcliations(page=1, itemsPerPage=10) { | ||||
|     return this.apiImplementation.getListReconcliations(page, itemsPerPage); | ||||
|   } | ||||
|  | ||||
|   getReconciliation(reconciliationId) { | ||||
|     return this.apiImplementation.getReconciliation(reconciliationId); | ||||
|   } | ||||
|  | ||||
|   isValidAdminCode(code) { | ||||
|     return this.apiImplementation.isValidAdminCode(code); | ||||
|   } | ||||
|  | ||||
|   createPurchase(purchase) { | ||||
|     return this.apiImplementation.createPurchase(purchase); | ||||
|   } | ||||
|  | ||||
|   createReconciliationJar(reconciliation) { | ||||
|     return this.apiImplementation.createReconciliationJar(reconciliation); | ||||
|   } | ||||
|  | ||||
|   createCustomer(customer) { | ||||
|     return this.apiImplementation.createCustomer(customer); | ||||
|   } | ||||
| } | ||||
|  | ||||
| export default Api; | ||||
							
								
								
									
										97
									
								
								src/services/django-api.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								src/services/django-api.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,97 @@ | ||||
| class DjangoApi { | ||||
|   getCustomers() { | ||||
|     const url = '/don_confiao/api/customers/'; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getProducts() { | ||||
|     const url = '/don_confiao/api/products/'; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getPaymentMethods() { | ||||
|     const url = '/don_confiao/payment_methods/all/select_format'; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getSummaryPurchase(purchaseId) { | ||||
|     const url = `/don_confiao/resumen_compra_json/${purchaseId}`; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getPurchasesForReconciliation() { | ||||
|     const url = '/don_confiao/purchases/for_reconciliation'; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getListReconcliations(page, itemsPerPage) { | ||||
|     const url = `/don_confiao/api/reconciliate_jar/?page=${page}&page_size=${itemsPerPage}`; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   getReconciliation(reconciliationId) { | ||||
|     const url = `/don_confiao/api/reconciliate_jar/${reconciliationId}/`; | ||||
|     return this.getRequest(url); | ||||
|   } | ||||
|  | ||||
|   isValidAdminCode(code) { | ||||
|     const url = `/don_confiao/api/admin_code/validate/${code}` | ||||
|     return this.getRequest(url) | ||||
|     } | ||||
|  | ||||
|   createPurchase(purchase) { | ||||
|     const url = '/don_confiao/api/sales/'; | ||||
|     return this.postRequest(url, purchase); | ||||
|   } | ||||
|  | ||||
|   createReconciliationJar(reconciliation) { | ||||
|     const url = '/don_confiao/reconciliate_jar'; | ||||
|     return this.postRequest(url, reconciliation); | ||||
|   } | ||||
|  | ||||
|   createCustomer(customer) { | ||||
|     const url = '/don_confiao/api/customers/'; | ||||
|     return this.postRequest(url, customer); | ||||
|   } | ||||
|  | ||||
|   getRequest(url) { | ||||
|     return new Promise ((resolve, reject) => { | ||||
|       fetch(url) | ||||
|         .then(response => response.json()) | ||||
|         .then(data => { | ||||
|           resolve(data); | ||||
|         }) | ||||
|         .catch(error => { | ||||
|           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; | ||||
		Reference in New Issue
	
	Block a user