Skip to content

Commit 92b2932

Browse files
author
RobJellinghaus
committed
Pagination works!
1 parent 3747522 commit 92b2932

2 files changed

Lines changed: 89 additions & 14 deletions

File tree

frontend/src/containers/SupplierList.tsx

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ const SupplierListContent = ({
117117
}) => {
118118
const [showCreateForm, setShowCreateForm] = useState<boolean>(false);
119119
const [isImportPending, setIsImportPending] = useState<boolean>(false);
120+
const [currentPage, setCurrentPage] = useState<number>(0);
121+
const [pageSize] = useState<number>(10);
120122
const fileInputRef = useRef<HTMLInputElement>(null);
121123
const [createForm, setCreateForm] = useState<CreateSupplierInput>({
122124
name: '',
@@ -139,6 +141,8 @@ const SupplierListContent = ({
139141

140142
const suppliers = data.suppliers?.items || [];
141143
const totalItems = data.suppliers?.totalItems || 0;
144+
const numPages = data.suppliers?.numPages || 1;
145+
const currentPageFromData = data.suppliers?.page || 0;
142146

143147
const handleCreateSupplier = () => {
144148
if (createForm.name.trim() && createForm.contactEmail.trim()) {
@@ -163,9 +167,10 @@ const SupplierListContent = ({
163167
website: ''
164168
});
165169
setShowCreateForm(false);
166-
// Force network fetch to bypass cache
170+
// Force network fetch to bypass cache and reset to first page
171+
setCurrentPage(0);
167172
loadQuery(
168-
{ page: 0, pageSize: 10 },
173+
{ page: 0, pageSize },
169174
{ fetchPolicy: 'network-only' }
170175
);
171176
},
@@ -183,8 +188,17 @@ const SupplierListContent = ({
183188
onCompleted: (response) => {
184189
if (response.deleteSupplier) {
185190
// Force network fetch to bypass cache and show updated list
191+
// Check if current page would be empty after deletion, go to previous page
192+
const remainingItems = totalItems - 1;
193+
const maxPage = Math.max(0, Math.ceil(remainingItems / pageSize) - 1);
194+
const targetPage = Math.min(currentPage, maxPage);
195+
196+
if (targetPage !== currentPage) {
197+
setCurrentPage(targetPage);
198+
}
199+
186200
loadQuery(
187-
{ page: 0, pageSize: 10 },
201+
{ page: targetPage, pageSize },
188202
{ fetchPolicy: 'network-only' }
189203
);
190204
}
@@ -336,9 +350,10 @@ const SupplierListContent = ({
336350
if (fileInputRef.current) {
337351
fileInputRef.current.value = '';
338352
}
339-
// Refresh the list
353+
// Refresh the list and reset to first page
354+
setCurrentPage(0);
340355
loadQuery(
341-
{ page: 0, pageSize: 10 },
356+
{ page: 0, pageSize },
342357
{ fetchPolicy: 'network-only' }
343358
);
344359
},
@@ -355,6 +370,28 @@ const SupplierListContent = ({
355370
}
356371
};
357372

373+
const handlePreviousPage = () => {
374+
if (currentPage > 0) {
375+
const newPage = currentPage - 1;
376+
setCurrentPage(newPage);
377+
loadQuery(
378+
{ page: newPage, pageSize },
379+
{ fetchPolicy: 'network-only' }
380+
);
381+
}
382+
};
383+
384+
const handleNextPage = () => {
385+
if (currentPage < numPages - 1) {
386+
const newPage = currentPage + 1;
387+
setCurrentPage(newPage);
388+
loadQuery(
389+
{ page: newPage, pageSize },
390+
{ fetchPolicy: 'network-only' }
391+
);
392+
}
393+
};
394+
358395
return (
359396
<div style={{ display: 'flex', flexFlow: 'column' }}>
360397
<h1>Suppliers</h1>
@@ -545,13 +582,30 @@ const SupplierListContent = ({
545582
))}
546583

547584
<div className="Form">
548-
<div style={{ display: 'flex' }}>
549-
<button disabled={true}>{`<< (Coming Soon)`}</button>
550-
<span style={{ flex: 1, textAlign: 'center' }}>
551-
Page {(data.suppliers?.page || 0) + 1} of {data.suppliers?.numPages || 1}
585+
<div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
586+
<button
587+
onClick={handlePreviousPage}
588+
disabled={currentPage === 0}
589+
style={{ minWidth: '100px' }}
590+
>
591+
Previous
592+
</button>
593+
<span style={{ flex: 1, textAlign: 'center', fontWeight: '500' }}>
594+
Page {currentPage + 1} of {numPages}
552595
</span>
553-
<button disabled={true}>{`>> (Coming Soon)`}</button>
596+
<button
597+
onClick={handleNextPage}
598+
disabled={currentPage >= numPages - 1}
599+
style={{ minWidth: '100px' }}
600+
>
601+
Next
602+
</button>
554603
</div>
604+
{totalItems > 0 && (
605+
<div style={{ textAlign: 'center', fontSize: '14px', color: '#666', marginTop: '8px' }}>
606+
Showing {Math.min(currentPage * pageSize + 1, totalItems)} - {Math.min((currentPage + 1) * pageSize, totalItems)} of {totalItems} suppliers
607+
</div>
608+
)}
555609
</div>
556610
</div>
557611
);

tests/suppliers.spec.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,12 @@ test.describe('Supplier Tests', () => {
318318
await expect(page.locator('text=Edit Test Corp')).toBeVisible();
319319
});
320320

321-
test('should show disabled pagination controls', async ({ page }) => {
321+
test('should show functional pagination controls', async ({ page }) => {
322322
await page.click('a:has-text("Suppliers")');
323323

324-
// Check that pagination buttons are disabled
325-
await expect(page.locator('button:has-text("<< (Coming Soon)")')).toBeDisabled();
326-
await expect(page.locator('button:has-text(">> (Coming Soon)")')).toBeDisabled();
324+
// Check that pagination buttons are present and Previous is disabled on first page
325+
await expect(page.locator('button:has-text("Previous")')).toBeDisabled();
326+
await expect(page.locator('button:has-text("Next")')).toBeVisible();
327327

328328
// Check page display
329329
await expect(page.locator('text=Page 1 of 1')).toBeVisible();
@@ -425,4 +425,25 @@ test.describe('Supplier Tests', () => {
425425
await expect(fileInput).toHaveCSS('display', 'none');
426426
});
427427

428+
test('should show working pagination controls', async ({ page }) => {
429+
await page.click('a:has-text("Suppliers")');
430+
431+
// Verify pagination controls are visible
432+
await expect(page.locator('button:has-text("Previous")')).toBeVisible();
433+
await expect(page.locator('button:has-text("Next")')).toBeVisible();
434+
435+
// On first page, Previous should be disabled
436+
await expect(page.locator('button:has-text("Previous")')).toBeDisabled();
437+
438+
// Check page display shows current page
439+
await expect(page.locator('text=Page 1 of')).toBeVisible();
440+
441+
// If there are suppliers, verify the item count display
442+
const supplierCount = await page.locator('a:has-text("delete")').count();
443+
if (supplierCount > 0) {
444+
await expect(page.locator('text=Showing')).toBeVisible();
445+
await expect(page.locator('text=suppliers')).toBeVisible();
446+
}
447+
});
448+
428449
});

0 commit comments

Comments
 (0)