Skip to content

Commit ee6944e

Browse files
committed
fix(src/routes/(app)/+page.server.ts): handle null values for getLastPayrollCycle to prevent errors
fix(src/routes/(app)/+page.svelte): handle null values for payroll data to prevent errors and improve UX refactor(src/routes/(app)/+page.svelte): improve the display of payroll data to enhance readability and user experience. Add conditional rendering to handle cases where no paystubs are available.
1 parent 64ca628 commit ee6944e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/routes/(app)/+page.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const load: PageServerLoad = async ({ locals }) => {
2525
const getLatestPayrollCycle = async (): Promise<
2626
SelectPayrollCycle & { paystubs: (SelectPaystub & { employee: SelectEmployee })[] }
2727
> => {
28-
return (await getLastPayrollCycle(clientId)) as SelectPayrollCycle & {
28+
return ((await getLastPayrollCycle(clientId)) || {}) as SelectPayrollCycle & {
2929
paystubs: (SelectPaystub & { employee: SelectEmployee })[];
3030
};
3131
};
@@ -39,7 +39,7 @@ export const load: PageServerLoad = async ({ locals }) => {
3939
orderBy: (pc, { desc }) => [desc(pc.paymentDate)]
4040
});
4141

42-
return data as (SelectPayrollCycle & { paystubs: SelectPaystub[] })[];
42+
return data || [] as (SelectPayrollCycle & { paystubs: SelectPaystub[] })[];
4343
};
4444

4545
return {

src/routes/(app)/+page.svelte

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
paystubs: (SelectPaystub & { employee: SelectEmployee })[];
2626
};
2727
const cycles = historyCycles as (SelectPayrollCycle & { paystubs: SelectPaystub[] })[];
28+
console.log(data);
2829
</script>
2930

3031
<svelte:head>
@@ -43,35 +44,42 @@
4344
<div class="px-4 mb-4 text-2xl font-medium">Latest Payroll</div>
4445
<div class="flex flex-col md:flex-row md:justify-around">
4546
{#if user && ['org_admin', 'super_admin'].includes(user.profile.role)}
47+
{#if cycle !== null && cycle.paystubs?.length > 0}
4648
<Card size="sm">
4749
<h5 class="mb-4 text-2xl font-medium text-gray-600 dark:text-gray-50">
48-
{toHumanDate(cycle.paymentDate)}
50+
{toHumanDate(cycle?.paymentDate)}
4951
</h5>
5052
<div class="flex items-baseline text-gray-900 dark:text-white">
5153
<ul class="my-7 space-y-4">
54+
{#if cycle.paystubs?.length > 0}
5255
<li class="flex space-x-2 rtl:space-x-reverse">
5356
<CheckCircleSolid class="w-4 h-4 text-primary-600 dark:text-primary-500" />
5457
<span class="text-xl font-extrabold tracking-tight"
5558
>{cycle.paystubs?.length} paystubs</span
5659
>
5760
</li>
61+
{/if}
62+
{#if cycle.paystubs?.length > 0}
5863
<li class="flex space-x-2 rtl:space-x-reverse">
5964
<CheckCircleSolid class="w-4 h-4 text-primary-600 dark:text-primary-500" />
6065
<span class="text-xl font-extrabold tracking-tight"
6166
>${cycle.paystubs?.reduce((acc, curr) => acc + Number(curr.netPay), 0)}</span
6267
> total net
6368
</li>
69+
{/if}
6470
</ul>
6571
</div>
6672
<div class="flex flex-col w-full md:flex-row md:justify-end md:w-auto">
67-
<Button href={'/app/payroll-cycles/' + cycle.id}>View Details</Button>
73+
<Button href={'/app/payroll-cycles/' + cycle?.id}>View Details</Button>
6874
</div>
6975
</Card>
76+
{/if}
7077
<Card size="sm">
7178
<div class="mb-4 text-xl font-medium text-gray-400 dark:text-gray-500">
7279
Top Performers
7380
</div>
7481
<div class="flex items-baseline text-gray-900 dark:text-white">
82+
{#if cycle?.paystubs?.length > 0}
7583
{#each cycle?.paystubs as paystub (paystub.id)}
7684
<div class="flex flex-col md:flex-row justify-between gap-2 w-full">
7785
<span class="text-xl font-extrabold tracking-tight"
@@ -82,6 +90,9 @@
8290
>
8391
</div>
8492
{/each}
93+
{:else}
94+
<span class="text-xl font-extrabold tracking-tight">No paystubs</span>
95+
{/if}
8596
</div>
8697
</Card>
8798
{:else}
@@ -105,6 +116,7 @@
105116
<TableHeadCell>Closed</TableHeadCell>
106117
</TableHead>
107118
<TableBody>
119+
{#if cycles.length > 0}
108120
{#each cycles as cycle (cycle.id)}
109121
<TableBodyRow>
110122
<TableBodyCell>
@@ -121,6 +133,13 @@
121133
</TableBodyCell>
122134
</TableBodyRow>
123135
{/each}
136+
{:else}
137+
<TableBodyRow>
138+
<TableBodyCell colSpan={6} class="text-center">
139+
No payroll cycles found
140+
</TableBodyCell>
141+
</TableBodyRow>
142+
{/if}
124143
</TableBody>
125144
</Table>
126145
</div>

0 commit comments

Comments
 (0)