diff --git a/src/components/FormLoadModal.vue b/src/components/FormLoadModal.vue index d46d6b1..4224ade 100644 --- a/src/components/FormLoadModal.vue +++ b/src/components/FormLoadModal.vue @@ -9,9 +9,12 @@ import Products from './ui/Products.vue'; import { GoogleMap, Marker, Polyline } from "vue3-google-map"; import useDirectionsRender from '../composables/useDirectionRender'; + import { useAuthStore } from '../stores/auth'; + import { useRouter } from 'vue-router'; + const router = useRouter() const loadStore = useLoadsStore(); - + const auth = useAuthStore(); const windowWidth = ref(window.innerWidth); const zoom = ref(6); const heightMap = ref(768); @@ -20,6 +23,7 @@ const startLocation = ref(null); const endLocation = ref(null); const { geocodeAddress } = useDirectionsRender(); + const clearLoad = () => { loadStore.currentLoad = null; @@ -44,15 +48,16 @@ zoom.value = 4; heightMap.value = 420; } + formLoad.owner = auth.user?.first_name + ' ' + auth.user?.last_name; //origin_formatted_address if(loadStore.currentLoad){ formLoad.price = loadStore.currentLoad.actual_cost; formLoad.segment = loadStore.currentLoad.categories.map(m =>{ - return { name: m.name }; + return m; }); startLocation.value = loadStore.currentLoad.origin_formatted_address; endLocation.value = loadStore.currentLoad.destination_formatted_address; - formLoad.terms = {name: loadStore.currentLoad.product.name}; + formLoad.terms = loadStore.currentLoad.product; formLoad.owner = loadStore.currentLoad.posted_by_name; formLoad.notes = loadStore.currentLoad.notes; formLoad.weight = loadStore.currentLoad.weight; @@ -60,39 +65,41 @@ formLoad.dateDownload = loadStore.currentLoad.est_unloading_date.substring(0, 10); formLoad.truckType = {meta_value: loadStore.currentLoad.truck_type}; - origin.locationNameOrigin = loadStore.currentLoad.origin.company_name; - origin.addressOrigin = loadStore.currentLoad.origin.street_address1; - origin.stateOrigin = { state_name: loadStore.currentLoad.origin.state }; - origin.cityOrigin = { city_name: loadStore.currentLoad.origin.city }; - origin.countryOrigin = loadStore.currentLoad.origin.country; - origin.postalCodeOrigin = loadStore.currentLoad.origin.zipcode; - origin.refOrigin = loadStore.currentLoad.origin.landmark; + origin.locationName = loadStore.currentLoad.origin.company_name; + origin.address = loadStore.currentLoad.origin.street_address1; + origin.state = { state_name: loadStore.currentLoad.origin.state }; + origin.city = { city_name: loadStore.currentLoad.origin.city }; + origin.country = loadStore.currentLoad.origin.country; + origin.postalCode = loadStore.currentLoad.origin.zipcode; + origin.ref = loadStore.currentLoad.origin.landmark; - destination.locationNameDestination = loadStore.currentLoad.destination.company_name; - destination.addressDestination = loadStore.currentLoad.destination.street_address1; - destination.stateDestination = { state_name: loadStore.currentLoad.destination.state }; - destination.cityDestination = { city_name: loadStore.currentLoad.destination.city }; - destination.countryDestination = loadStore.currentLoad.destination.country; - destination.postalCodeDestination = loadStore.currentLoad.destination.zipcode; - destination.refDestination = loadStore.currentLoad.destination.landmark; + destination.locationName = loadStore.currentLoad.destination.company_name; + destination.address = loadStore.currentLoad.destination.street_address1; + destination.state = { state_name: loadStore.currentLoad.destination.state }; + destination.city = { city_name: loadStore.currentLoad.destination.city }; + destination.country = loadStore.currentLoad.destination.country; + destination.postalCode = loadStore.currentLoad.destination.zipcode; + destination.ref = loadStore.currentLoad.destination.landmark; getCoordsMap(); + } - watch(origin, async() => { - startLocation.value = origin.addressOrigin + ', ' + origin.cityOrigin.city_name + ', ' + origin.stateOrigin.state_name + ', ' + origin.countryOrigin + ', ' +origin.postalCodeOrigin; - + watch(origin, async() => { + if(origin.address && origin.city && origin.state) { + startLocation.value = origin.address + ', ' + origin.city.city_name + ', ' + origin.state.state_name + ', ' + origin.country + ', ' +origin.postalCode; originCoords.value = await geocodeAddress(startLocation.value); console.log('Origin:') console.log(originCoords.value) - }) - - watch(destination, async() => { - endLocation.value = destination.addressDestination + ', ' + destination.cityDestination.city_name + ', ' + destination.stateDestination.state_name + ', ' + destination.countryDestination + ', ' +destination.postalCodeDestination; + } + }) + watch(destination, async() => { + if(destination.address && destination.city && destination.state) { + endLocation.value = destination.address + ', ' + destination.city.city_name + ', ' + destination.state.state_name + ', ' + destination.country + ', ' +destination.postalCode; destinationCoords.value = await geocodeAddress(endLocation.value); console.log('Destination:') console.log(destinationCoords.value); - }) - } + } + }) }) const getCoordsMap = async() => { @@ -114,29 +121,72 @@ }); const origin = reactive({ - locationNameOrigin: '', - addressOrigin: '', - stateOrigin: '', - cityOrigin: '', - countryOrigin: '', - postalCodeOrigin: '', - refOrigin: '', + locationName: '', + address: '', + state: '', + city: '', + country: '', + postalCode: '', + ref: '', }); const destination = reactive({ - locationNameDestination: '', - addressDestination: '', - stateDestination: '', - cityDestination: '', - countryDestination: '', - postalCodeDestination: '', - refDestination: '', + locationName: '', + address: '', + state: '', + city: '', + country: '', + postalCode: '', + ref: '', }); - const handleSave = () => { - console.log(formLoad); - console.log(origin) - console.log(destination) + const handleSave = async() => { + let loadData = { + actual_cost: formLoad.price, + truck_type: formLoad.truckType.meta_value || null, + est_loading_date : formLoad.dateLoad, + est_unloading_date : formLoad.dateDownload, + notes : formLoad.notes, + weight : formLoad.weight, + product: formLoad.terms || null, + categories: formLoad.segment || null, + origin:{ + company_name : origin.locationName, + street_address1 : origin.address, + state : origin.state.state_name, + city : origin.city.city_name, + country : origin.country, + landmark : origin.ref, + zipcode : origin.postalCode, + lat : originCoords.value.lat, + lng : originCoords.value.lng + }, + destination:{ + company_name : destination.locationName, + street_address1 : destination.address, + state : destination.state.state_name, + city : destination.city.city_name, + country : destination.country, + landmark : destination.ref, + zipcode : destination.postalCode, + lat : destinationCoords.value.lat, + lng : destinationCoords.value.lng + }, + company: auth.user.company, + posted_by: auth.user._id, + posted_by_name: formLoad.owner + }; + + console.log('loadData: ', loadData); + if(loadStore.currentLoad){ + const resp = await loadStore.updateLoad(loadStore.currentLoad._id, loadData); + const index = loadStore.loads.findIndex((load) => load._id === resp._id); + loadStore.loads[index] = resp; + document.getElementById('btnCloseFormLoadModal').click(); + } else{ + const resp = await loadStore.saveLoad(loadData); + loadStore.loads.push(resp); + } } @@ -219,6 +269,7 @@ label="Publicación hecha por*" type="text" :filled="false" + :readonly="true" name="owner" v-model:field="formLoad.owner" /> @@ -232,25 +283,25 @@ type="text" :filled="false" name="name-location-origin" - v-model:field="origin.locationNameOrigin" + v-model:field="origin.locationName" />
@@ -282,25 +333,25 @@ type="text" :filled="false" name="name-location-destination" - v-model:field="destination.locationNameDestination" + v-model:field="destination.locationName" />
diff --git a/src/components/ProposalsModal.vue b/src/components/ProposalsModal.vue index d5414c4..bca4cdd 100644 --- a/src/components/ProposalsModal.vue +++ b/src/components/ProposalsModal.vue @@ -1,9 +1,9 @@