add: tracking load & clean code save load
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
import { GoogleMap, Marker, Polyline } from "vue3-google-map";
|
||||
import useDirectionsRender from '../composables/useDirectionRender';
|
||||
import { useAuthStore } from '../stores/auth';
|
||||
import Swal from 'sweetalert2';
|
||||
|
||||
const loadStore = useLoadsStore();
|
||||
const auth = useAuthStore();
|
||||
@@ -145,7 +146,7 @@
|
||||
ref: '',
|
||||
});
|
||||
|
||||
const handleSave = async() => {
|
||||
const setLoadData = () => {
|
||||
let loadData = {
|
||||
actual_cost: formLoad.price,
|
||||
truck_type: formLoad.truckType.meta_value || null,
|
||||
@@ -182,21 +183,75 @@
|
||||
posted_by_name: formLoad.owner
|
||||
};
|
||||
|
||||
console.log('loadData: ', loadData);
|
||||
return loadData;
|
||||
}
|
||||
|
||||
const updateLoad = async(loadData) => {
|
||||
isLoading.value = true;
|
||||
if(loadStore.currentLoad){
|
||||
const resp = await loadStore.updateLoad(loadStore.currentLoad._id, loadData);
|
||||
const index = loadStore.loads.findIndex((load) => load._id === resp._id);
|
||||
console.log({resp});
|
||||
isLoading.value = false;
|
||||
loadStore.loads[index] = resp;
|
||||
document.getElementById('btnCloseFormLoadModal').click();
|
||||
if(resp) {
|
||||
const index = loadStore.loads.findIndex((load) => load._id === resp._id);
|
||||
loadStore.loads[index] = {
|
||||
...loadStore.loads[index],
|
||||
resp
|
||||
};
|
||||
return 'success';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
} else{
|
||||
const resp = await loadStore.saveLoad(loadData);
|
||||
isLoading.value = false;
|
||||
loadStore.loads.unshift(resp);
|
||||
document.getElementById('btnCloseFormLoadModal').click();
|
||||
if(resp) {
|
||||
loadStore.loads.unshift(resp);
|
||||
return 'success';
|
||||
} else {
|
||||
return 'error';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleSave = async() => {
|
||||
|
||||
const loadData = setLoadData();
|
||||
|
||||
// console.log('loadData: ', loadData);
|
||||
const resp = await updateLoad(loadData);
|
||||
if(resp === 'success') {
|
||||
document.getElementById('btnCloseFormLoadModal').click();
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "No se pudo guardar carga, intente más tarde",
|
||||
icon: "error"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const handlePostLoad = async() => {
|
||||
let loadData = setLoadData();
|
||||
|
||||
loadData = {
|
||||
...loadData,
|
||||
status: "Published",
|
||||
load_status: "Published"
|
||||
}
|
||||
|
||||
const resp = await updateLoad(loadData);
|
||||
if(resp === 'success') {
|
||||
document.getElementById('btnCloseFormLoadModal').click();
|
||||
} else {
|
||||
Swal.fire({
|
||||
title: "Error!",
|
||||
text: "No se pudo publicar carga, intente más tarde",
|
||||
icon: "error"
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -430,7 +485,7 @@
|
||||
<button
|
||||
type="button"
|
||||
class="btn-primary-sm radius-sm"
|
||||
@click=""
|
||||
@click="handlePostLoad"
|
||||
>Publicar</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user