clean code

This commit is contained in:
2025-07-01 15:58:21 +08:00
parent edeb0870f3
commit a825334e3a
2 changed files with 364 additions and 454 deletions

View File

@@ -26,7 +26,6 @@
approved_date?: Date;
created_at?: Date;
};
type TimesheetDisplay = {
id: number;
name: string;
@@ -50,7 +49,6 @@
remarks: string;
created_at?: Date;
};
type TimesheetsInsert = {
entered_by: string;
work_description: string;
@@ -70,6 +68,15 @@
remarks: string;
approval: boolean | null; // Allow null for new entries
};
type Villa = {
id: string;
villa_name: string;
};
type columns = {
key: string;
title: string;
};
const categoryOfWork = [
{ label: "Cleaning", value: "Cleaning" },
@@ -80,52 +87,11 @@
{ label: "Administration", value: "Administration" },
{ label: "Non Billable", value: "Non Billable" },
];
const typeOfWork = [
{ label: "Running", value: "Running" },
{ label: "Periodic", value: "Periodic" },
{ label: "Irregular", value: "Irregular" },
];
let currentUserId: string | null = null;
let currentVillaFilter: string | null = null;
let currentSearchTerm: string | null = null;
onMount(async () => {
const {
data: { user },
} = await supabase.auth.getUser();
currentUserId = user?.id ?? null;
});
type Villa = {
id: string;
villa_name: string;
};
let dataVilla: Villa[] = [];
onMount(async () => {
const { data, error } = await supabase
.from("vb_villas")
.select("id, villa_name")
.eq("villa_status", "Active");
if (error) {
console.error("Error fetching villas:", error);
} else if (data) {
dataVilla = data;
}
});
let allRows: TimesheetDisplay[] = [];
type columns = {
key: string;
title: string;
};
const columns: columns[] = [
{ key: "name", title: "Work Description" },
{ key: "staff_id", title: "Staff Name" },
@@ -142,6 +108,35 @@
{ key: "created_at", title: "Created At" },
{ key: "actions", title: "Actions" },
];
// Store for current user ID and filters
let currentUserId: string | null = null;
let currentVillaFilter: string | null = null;
let currentSearchTerm: string | null = null;
let dataVilla: Villa[] = [];
let allRows: TimesheetDisplay[] = [];
onMount(async () => {
const {
data: { user },
} = await supabase.auth.getUser();
currentUserId = user?.id ?? null;
});
onMount(async () => {
const { data, error } = await supabase
.from("vb_villas")
.select("id, villa_name")
.eq("villa_status", "Active");
if (error) {
console.error("Error fetching villas:", error);
} else if (data) {
dataVilla = data;
}
});
// Function to calculate total work hours
function calculateTotalHours() {
if (form.datetime_in && form.datetime_out) {
const start = new Date(form.datetime_in);
@@ -154,13 +149,14 @@
}
}
// Function to fetch timesheets with optional filters and sorting
async function fetchTimeSheets(
villaNameFilter: string | null = null,
searchTerm: string | null = null,
sortColumn: string | null = "created_at",
sortOrder: "asc" | "desc" = "desc",
offset: number = 0,
limit: number = 10,
limit: number = 1000,
) {
let query = supabase
.from("vb_timesheet")
@@ -269,7 +265,7 @@
// Sort the rows based on the sortColumn and sortOrder
}
let currentPage = 1;
let rowsPerPage = 5;
let rowsPerPage = 20;
$: totalPages = Math.ceil(allRows.length / rowsPerPage);
$: paginatedRows = allRows.slice(
(currentPage - 1) * rowsPerPage,