From 937c3a8fe55b3f77b04f974a6017affe76151915 Mon Sep 17 00:00:00 2001 From: Alexandro Uc Santos Date: Tue, 9 Jan 2024 18:49:51 -0600 Subject: [PATCH] add: crud locations --- src/assets/main.css | 6 + src/components/CardBudget.vue | 4 +- src/components/CardLocation.vue | 116 ++++++++++++ src/components/CreateLocationModal.vue | 249 +++++++++++++++++++++++++ src/components/FormLoadModal.vue | 62 ++++++ src/services/company.js | 44 +++++ src/stores/company.js | 66 ++++++- src/views/LocationsView.vue | 133 +++++++++++++ 8 files changed, 678 insertions(+), 2 deletions(-) create mode 100644 src/components/CardLocation.vue create mode 100644 src/components/CreateLocationModal.vue diff --git a/src/assets/main.css b/src/assets/main.css index 2ed5f38..1d9bec4 100644 --- a/src/assets/main.css +++ b/src/assets/main.css @@ -67,6 +67,12 @@ body { transition: background-color 300ms ease; } +.error-msg { + color: red; + font-size: 12px; + font-weight: 300; +} + .btn-primary-sm { background-color: #FBBA33; padding: 8px 16px; diff --git a/src/components/CardBudget.vue b/src/components/CardBudget.vue index ca680e6..ae796ba 100644 --- a/src/components/CardBudget.vue +++ b/src/components/CardBudget.vue @@ -1,5 +1,6 @@ + + + + \ No newline at end of file diff --git a/src/components/CreateLocationModal.vue b/src/components/CreateLocationModal.vue new file mode 100644 index 0000000..18a3bc5 --- /dev/null +++ b/src/components/CreateLocationModal.vue @@ -0,0 +1,249 @@ + + + + + \ No newline at end of file diff --git a/src/components/FormLoadModal.vue b/src/components/FormLoadModal.vue index 1289d8e..5112474 100644 --- a/src/components/FormLoadModal.vue +++ b/src/components/FormLoadModal.vue @@ -13,10 +13,12 @@ import { useAuthStore } from '../stores/auth'; import Swal from 'sweetalert2'; import { useNotificationsStore } from '../stores/notifications'; + import { useCompanyStore } from '../stores/company'; const loadStore = useLoadsStore(); const notyStore = useNotificationsStore(); const auth = useAuthStore(); + const companyStore = useCompanyStore() const windowWidth = ref(window.innerWidth); const zoom = ref(6); const heightMap = ref(768); @@ -25,9 +27,16 @@ const startLocation = ref(null); const endLocation = ref(null); const isLoading = ref(false); + const loadingLocations = ref(false); const submited = ref(false); const { geocodeAddress } = useDirectionsRender(); const formRef = ref(null); + const filterQueryVehicles = ref([]); + const checkLocationLoad = ref(false); + const checkLocationDownload = ref(false); + const locationLoadSelected = ref(null) + const locationDownloadSelected = ref(null) + const errors = ref({ segment: null, product: null, @@ -62,6 +71,9 @@ zoom.value = 4; heightMap.value = 420; } + if(companyStore.locations.length <= 0) { + getLocations(); + } formLoad.owner = auth.user?.first_name + ' ' + auth.user?.last_name; //origin_formatted_address if(loadStore.currentLoad){ @@ -117,6 +129,13 @@ }) }) + const getLocations = async() => { + loadingLocations.value = true; + filterQueryVehicles.value.company = "company="+ localStorage.getItem('id'); + await companyStore.getLocationsCompany(filterQueryVehicles.value, false) + loadingLocations.value = false; + } + const getCoordsMap = async() => { if(loadStore.currentLoad.origin_formatted_address) { originCoords.value = await geocodeAddress(loadStore.currentLoad.origin_formatted_address); @@ -397,6 +416,25 @@

Dirección de origen

+ +
+ + +
+
+ + +

Dirección de destino

+
+ + +
+
+ + +
{ } } +export const getLocations = async(filter) => { + try { + const endpoint = `/branches/${filter}`; + const {data} = await api.get(endpoint); + return data; + } catch (error) { + console.log(error); + return null; + } +} + +export const createLocation = async(formData) => { + try { + const endpoint = `/branches`; + const {data} = await api.post(endpoint, formData); + return data; + } catch (error) { + console.log(error); + return null; + } +} + +export const updateLocation = async(id, formData) => { + try { + const endpoint = `/branches/${id}`; + const {data} = await api.patch(endpoint, formData); + return data; + } catch (error) { + console.log(error); + return null; + } +} + +export const deleteLocation = async(id) => { + try { + const endpoint = `/branches/${id}`; + const {data} = await api.delete(endpoint); + return data; + } catch (error) { + console.log(error); + return null; + } +} + // export const editCompany = async(companyId, formData) => { // try { diff --git a/src/stores/company.js b/src/stores/company.js index 7b4adb1..f29939c 100644 --- a/src/stores/company.js +++ b/src/stores/company.js @@ -1,6 +1,6 @@ import { defineStore } from "pinia"; import { ref } from "vue"; -import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget, getUsers, updateUser, createUser, deleteUser } from "../services/company"; +import { getBudgets, getCompany, updateBudget, updateCompany, deleteBudget, createBudget, getUsers, updateUser, createUser, deleteUser, getLocations, createLocation, updateLocation, deleteLocation } from "../services/company"; import api from "../lib/axios"; export const useCompanyStore = defineStore('company', () => { @@ -8,6 +8,7 @@ export const useCompanyStore = defineStore('company', () => { const company = ref(null) const users = ref([]); const budgets = ref([]); + const locations = ref([]); const proposals = ref([]) const loading = ref(false); @@ -196,6 +197,64 @@ export const useCompanyStore = defineStore('company', () => { } } + const getLocationsCompany = async(filterQuery, reload = false) => { + let filterArr = Object.values(filterQuery); + + let cleanfilterArr = filterArr.filter(n=>n); + var filterStr = ""; + if(cleanfilterArr.length > 0){ + filterStr ="?"+cleanfilterArr.join("&"); + } + if(locations.value.length <= 0 || reload === true) { + const resp = await getLocations(filterStr); + console.log(resp); + if(resp !== null && resp.total > 0) { + locations.value = resp.data; + } else { + locations.value = []; + } + } + } + + const createLocationCompany = async(formData, localData) => { + const data = await createLocation(formData); + if(data) { + locations.value.push({ + ...data, + ...localData + }); + return 'success'; + } else { + return 'Algo salio mal, intente más tarde'; + } + } + + const updateLocationCompany = async(id, formData, localData) => { + const data = await updateLocation(id, formData); + if(data) { + const index = locations.value.findIndex((loc) => loc._id === id); + locations.value[index] = { + ...locations.value[index], + ...data, + ...localData + }; + return 'success'; + } else { + return 'Algo salio mal, intente más tarde'; + } + } + + const deleteLocationCompany = async(id) => { + const data = await deleteLocation(id); + if(data) { + locations.value = locations.value.filter(loc => loc._id !== id); + + return 'success'; + } else { + return 'Algo salio mal, intente más tarde'; + } + } + return { getCompanyData, @@ -209,8 +268,13 @@ export const useCompanyStore = defineStore('company', () => { updateBudgetCompany, createBudgetCompany, deleteBudgetCompany, + getLocationsCompany, + createLocationCompany, + updateLocationCompany, + deleteLocationCompany, budgets, users, + locations, clear, $reset, loading, diff --git a/src/views/LocationsView.vue b/src/views/LocationsView.vue index 5d66486..5fb5675 100644 --- a/src/views/LocationsView.vue +++ b/src/views/LocationsView.vue @@ -1,13 +1,146 @@ \ No newline at end of file