Major Refactor
This commit is contained in:
@@ -1,280 +1,186 @@
|
||||
<template>
|
||||
<Header />
|
||||
<div class="flex">
|
||||
<div class="">
|
||||
<SideBar />
|
||||
</div>
|
||||
<div class=" w-full px-10">
|
||||
<div class="text-sm breadcrumbs">
|
||||
<ul>
|
||||
<li>
|
||||
<router-link :to="{ name: 'home' }">
|
||||
Home
|
||||
</router-link>
|
||||
</li>
|
||||
<li>
|
||||
<router-link :to="{ name: 'customer' }">
|
||||
Customers
|
||||
</router-link>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="flex">
|
||||
<div class="w-full px-4 md:px-10 py-4">
|
||||
<!-- Breadcrumbs & Title -->
|
||||
<div class="text-sm breadcrumbs">
|
||||
<ul>
|
||||
<li><router-link :to="{ name: 'home' }">Home</router-link></li>
|
||||
<li><router-link :to="{ name: 'customer' }">Customers</router-link></li>
|
||||
<li>Edit Tank Details</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mt-4">
|
||||
<h1 v-if="customer.id" class="text-3xl font-bold">
|
||||
Tank for: {{ customer.customer_first_name }} {{ customer.customer_last_name }}
|
||||
</h1>
|
||||
<router-link v-if="customer.id" :to="{ name: 'customerProfile', params: { id: customer.id } }" class="btn btn-secondary btn-sm mt-2 sm:mt-0">
|
||||
Back to Profile
|
||||
</router-link>
|
||||
</div>
|
||||
|
||||
<!-- Main Form Card -->
|
||||
<div class="bg-neutral rounded-lg p-6 mt-6">
|
||||
<form @submit.prevent="onSubmit" class="space-y-6">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4">
|
||||
|
||||
<!-- Inspection Date -->
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">Last Inspection Date</span></label>
|
||||
<input v-model="TankForm.last_tank_inspection" type="date" class="input input-bordered input-sm w-full" />
|
||||
</div>
|
||||
|
||||
|
||||
<div class="grid grid-cols-1 rounded-md p-6 ">
|
||||
<div class="text-[24px]">
|
||||
Customer: {{ customer.customer_first_name }} {{ customer.customer_last_name }} | {{ customer.account_number }}
|
||||
</div>
|
||||
|
||||
<form class="rounded-md px-8 pt-6 pb-8 mb-4 w-full" enctype="multipart/form-data"
|
||||
@submit.prevent="onSubmit">
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-white text-sm font-bold mb-2">Inspection Date </label>
|
||||
<input v-model="CreateTankForm.basicInfo.last_tank_inspection"
|
||||
class="input input-bordered input-sm w-full max-w-xs" id="title" type="date"
|
||||
min="2023-01-01" max="2030-01-01" />
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-white text-sm font-bold mb-2">Good Or bad Tank</label>
|
||||
<label>
|
||||
<input type="radio" name="goodtank1" value="true" class="radio"
|
||||
v-model="CreateTankForm.basicInfo.tank_status" />
|
||||
Good
|
||||
</label>
|
||||
<label>
|
||||
<input type="radio" name="goodtank2" value="false" class="radio"
|
||||
v-model="CreateTankForm.basicInfo.tank_status" />
|
||||
Bad
|
||||
</label>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-white text-sm font-bold mb-2">Tank Size</label>
|
||||
<input v-model="CreateTankForm.basicInfo.tank_size"
|
||||
class="input input-bordered input-sm w-full max-w-xs" id="title" type="text"
|
||||
placeholder="Gallon size of tank" />
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-white text-sm font-bold mb-2">Inside or Outside</label>
|
||||
<label>
|
||||
<input type="radio" name="insideoutside1" value="true" class="radio"
|
||||
v-model="CreateTankForm.basicInfo.outside_or_inside" />
|
||||
Inside
|
||||
</label>
|
||||
<label>
|
||||
|
||||
<input type="radio" name="insideoutside2" value="false" class="radio"
|
||||
v-model="CreateTankForm.basicInfo.outside_or_inside" />
|
||||
Outside
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="block text-white text-sm font-bold mb-2">Fill Location</label>
|
||||
<input v-model="CreateTankForm.basicInfo.fill_location"
|
||||
class="input input-bordered input-sm w-full max-w-xs" id="title" type="text"
|
||||
placeholder="Fill Location" />
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<div class="col-span-12 md:col-span-12 flex mt-5 mb-5">
|
||||
<button class="btn btn-accent btn-sm">
|
||||
Save Changes
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<!-- Tank Size -->
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">Tank Size (Gallons)</span></label>
|
||||
<input v-model="TankForm.tank_size" type="number" placeholder="e.g., 275" class="input input-bordered input-sm w-full" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tank Status -->
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">Tank Status</span></label>
|
||||
<div class="flex items-center gap-6 bg-base-100 p-2 rounded-lg">
|
||||
<label class="label cursor-pointer gap-2">
|
||||
<span class="label-text">Good</span>
|
||||
<input type="radio" v-model="TankForm.tank_status" :value="true" class="radio radio-primary" />
|
||||
</label>
|
||||
<label class="label cursor-pointer gap-2">
|
||||
<span class="label-text">Bad</span>
|
||||
<input type="radio" v-model="TankForm.tank_status" :value="false" class="radio radio-primary" />
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Tank Location -->
|
||||
<div class="form-control">
|
||||
<label class="label"><span class="label-text">Tank Location</span></label>
|
||||
<div class="flex items-center gap-6 bg-base-100 p-2 rounded-lg">
|
||||
<label class="label cursor-pointer gap-2">
|
||||
<span class="label-text">Inside</span>
|
||||
<input type="radio" v-model="TankForm.outside_or_inside" :value="true" class="radio radio-primary" />
|
||||
</label>
|
||||
<label class="label cursor-pointer gap-2">
|
||||
<span class="label-text">Outside</span>
|
||||
<input type="radio" v-model="TankForm.outside_or_inside" :value="false" class="radio radio-primary" />
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Fill Location -->
|
||||
<div class="form-control md:col-span-2">
|
||||
<label class="label"><span class="label-text">Fill Location Description</span></label>
|
||||
<input v-model="TankForm.fill_location" type="text" placeholder="e.g., Left side of house, behind shed" class="input input-bordered input-sm w-full" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- SUBMIT BUTTON -->
|
||||
<div class="pt-4">
|
||||
<button type="submit" class="btn btn-primary btn-sm">Save Changes</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Footer />
|
||||
</div>
|
||||
<Footer />
|
||||
</template>
|
||||
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue'
|
||||
import axios from 'axios'
|
||||
import authHeader from '../../../services/auth.header'
|
||||
import Header from '../../../layouts/headers/headerauth.vue'
|
||||
import SideBar from '../../../layouts/sidebar/sidebar.vue'
|
||||
import Footer from '../../../layouts/footers/footer.vue'
|
||||
|
||||
// Interface for our flat form model
|
||||
interface TankFormData {
|
||||
last_tank_inspection: string | null;
|
||||
tank_status: boolean;
|
||||
outside_or_inside: boolean;
|
||||
tank_size: number;
|
||||
fill_location: string;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: 'TankEdit',
|
||||
|
||||
components: {
|
||||
Header,
|
||||
SideBar,
|
||||
Footer,
|
||||
},
|
||||
|
||||
data() {
|
||||
return {
|
||||
user: {
|
||||
id: '',
|
||||
},
|
||||
customer: {
|
||||
id: 0,
|
||||
user_id: 0,
|
||||
customer_first_name: '',
|
||||
customer_last_name: '',
|
||||
customer_town: '',
|
||||
customer_address: '',
|
||||
customer_state: 0,
|
||||
customer_zip: '',
|
||||
customer_apt: '',
|
||||
customer_home_type: 0,
|
||||
customer_phone_number: '',
|
||||
account_number: '',
|
||||
},
|
||||
tank: {
|
||||
customer_id: 0,
|
||||
last_tank_inspection: true,
|
||||
tank_status: true,
|
||||
outside_or_inside: true,
|
||||
tank_size: 0,
|
||||
|
||||
},
|
||||
|
||||
CreateTankForm: {
|
||||
basicInfo: {
|
||||
last_tank_inspection: null,
|
||||
tank_status: true,
|
||||
outside_or_inside: true,
|
||||
tank_size: 0,
|
||||
fill_location: 0,
|
||||
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
created() {
|
||||
this.userStatus()
|
||||
},
|
||||
watch: {
|
||||
$route() {
|
||||
this.getCustomer(this.$route.params.id);
|
||||
this.getCustomerDescription(this.$route.params.id);
|
||||
this.getTank(this.$route.params.id);
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
this.getCustomer(this.$route.params.id);
|
||||
this.getCustomerDescription(this.$route.params.id);
|
||||
this.getTank(this.$route.params.id);
|
||||
},
|
||||
methods: {
|
||||
userStatus() {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
headers: authHeader(),
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.user = response.data.user;
|
||||
this.user.id = response.data.user.id;
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
this.user.id = '';
|
||||
})
|
||||
},
|
||||
getCustomer(userid: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/customer/' + userid;
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
headers: authHeader(),
|
||||
}).then((response: any) => {
|
||||
this.customer = response.data
|
||||
})
|
||||
},
|
||||
getCustomerDescription(userid: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + '/customer/description/' + userid;
|
||||
axios({
|
||||
method: 'get',
|
||||
url: path,
|
||||
headers: authHeader(),
|
||||
}).then((response: any) => {
|
||||
|
||||
this.CreateTankForm.basicInfo.fill_location = response.data.fill_location;
|
||||
name: 'TankEdit',
|
||||
components: {
|
||||
Footer,
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
user: null as any,
|
||||
customer: {} as any,
|
||||
// --- REFACTORED: Simplified, flat form object ---
|
||||
TankForm: {
|
||||
last_tank_inspection: null,
|
||||
tank_status: true,
|
||||
outside_or_inside: true,
|
||||
tank_size: 0,
|
||||
fill_location: '',
|
||||
} as TankFormData,
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.userStatus();
|
||||
const customerId = this.$route.params.id;
|
||||
this.getCustomer(customerId);
|
||||
this.getCustomerDescription(customerId);
|
||||
this.getTank(customerId);
|
||||
},
|
||||
methods: {
|
||||
userStatus() {
|
||||
const path = import.meta.env.VITE_BASE_URL + '/auth/whoami';
|
||||
axios.get(path, { withCredentials: true, headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.user = response.data.user;
|
||||
}
|
||||
})
|
||||
},
|
||||
getTank(customer_id: any) {
|
||||
let path = import.meta.env.VITE_BASE_URL + "/customer/tank/" + customer_id;
|
||||
axios({
|
||||
method: "get",
|
||||
url: path,
|
||||
withCredentials: true,
|
||||
headers: authHeader(),
|
||||
})
|
||||
.then((response: any) => {
|
||||
this.tank = response.data
|
||||
|
||||
this.CreateTankForm.basicInfo.last_tank_inspection = response.data.last_tank_inspection;
|
||||
this.CreateTankForm.basicInfo.tank_status = response.data.tank_status;
|
||||
this.CreateTankForm.basicInfo.outside_or_inside = response.data.outside_or_inside;
|
||||
this.CreateTankForm.basicInfo.tank_size = response.data.tank_size;
|
||||
|
||||
console.log(this.CreateTankForm.basicInfo.outside_or_inside)
|
||||
console.log(this.CreateTankForm.basicInfo.tank_status)
|
||||
})
|
||||
},
|
||||
editTank(payload: {
|
||||
|
||||
last_tank_inspection: any;
|
||||
tank_status: boolean;
|
||||
outside_or_inside: boolean;
|
||||
tank_size: number;
|
||||
fill_location: number;
|
||||
}) {
|
||||
|
||||
let path = import.meta.env.VITE_BASE_URL + "/customer/edit/tank/" + this.$route.params.id;
|
||||
axios({
|
||||
method: "put",
|
||||
url: path,
|
||||
data: payload,
|
||||
withCredentials: true,
|
||||
headers: authHeader(),
|
||||
})
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.$router.push({ name: "customerProfile", params: { id: this.tank.customer_id } });
|
||||
}
|
||||
if (response.data.error) {
|
||||
this.$router.push("/");
|
||||
}
|
||||
})
|
||||
},
|
||||
onSubmit() {
|
||||
console.log(this.CreateTankForm.basicInfo.outside_or_inside)
|
||||
console.log(this.CreateTankForm.basicInfo.tank_status)
|
||||
let payload = {
|
||||
last_tank_inspection: this.CreateTankForm.basicInfo.last_tank_inspection,
|
||||
tank_status: this.CreateTankForm.basicInfo.tank_status,
|
||||
outside_or_inside: this.CreateTankForm.basicInfo.outside_or_inside,
|
||||
tank_size: this.CreateTankForm.basicInfo.tank_size,
|
||||
fill_location: this.CreateTankForm.basicInfo.fill_location,
|
||||
};
|
||||
this.editTank(payload);
|
||||
},
|
||||
.catch(() => { this.user = null; });
|
||||
},
|
||||
getCustomer(userid: any) {
|
||||
const path = `${import.meta.env.VITE_BASE_URL}/customer/${userid}`;
|
||||
axios.get(path, { headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
this.customer = response.data;
|
||||
});
|
||||
},
|
||||
getCustomerDescription(userid: any) {
|
||||
const path = `${import.meta.env.VITE_BASE_URL}/customer/description/${userid}`;
|
||||
axios.get(path, { headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
// Only update fill_location if the response has it
|
||||
if (response.data && response.data.fill_location) {
|
||||
this.TankForm.fill_location = response.data.fill_location;
|
||||
}
|
||||
});
|
||||
},
|
||||
getTank(customer_id: any) {
|
||||
const path = `${import.meta.env.VITE_BASE_URL}/customer/tank/${customer_id}`;
|
||||
axios.get(path, { withCredentials: true, headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
if (response.data) {
|
||||
// Update the form model with data from the tank endpoint
|
||||
this.TankForm.last_tank_inspection = response.data.last_tank_inspection;
|
||||
this.TankForm.tank_status = response.data.tank_status;
|
||||
this.TankForm.outside_or_inside = response.data.outside_or_inside;
|
||||
this.TankForm.tank_size = response.data.tank_size;
|
||||
}
|
||||
});
|
||||
},
|
||||
editTank(payload: TankFormData) {
|
||||
const path = `${import.meta.env.VITE_BASE_URL}/customer/edit/tank/${this.$route.params.id}`;
|
||||
axios.put(path, payload, { withCredentials: true, headers: authHeader() })
|
||||
.then((response: any) => {
|
||||
if (response.data.ok) {
|
||||
this.$router.push({ name: "customerProfile", params: { id: this.customer.id } });
|
||||
} else {
|
||||
console.error("Failed to edit tank:", response.data.error);
|
||||
}
|
||||
});
|
||||
},
|
||||
onSubmit() {
|
||||
// The payload is simply the entire form object now
|
||||
this.editTank(this.TankForm);
|
||||
},
|
||||
},
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped></style>
|
||||
</script>
|
||||
Reference in New Issue
Block a user