Refactored VendorInfo component to use useCallback for fetchVendorDetails, improving performance and preventing unnecessary re-renders. Enhanced DashboardTable with a scroll handler for dynamic loading of data when scrolling up and down in card view.

This commit is contained in:
Tom Butcher 2025-06-28 13:44:44 +01:00
parent 859ae656d6
commit 4545550ab8
2 changed files with 45 additions and 5 deletions

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect, useCallback } from 'react'
import { useLocation } from 'react-router-dom' import { useLocation } from 'react-router-dom'
import axios from 'axios' import axios from 'axios'
import { import {
@ -61,7 +61,7 @@ const VendorInfo = () => {
if (vendorId) { if (vendorId) {
fetchVendorDetails() fetchVendorDetails()
} }
}, [vendorId]) }, [vendorId, fetchVendorDetails])
useEffect(() => { useEffect(() => {
if (vendorData) { if (vendorData) {
@ -76,7 +76,7 @@ const VendorInfo = () => {
} }
}, [vendorData, form]) }, [vendorData, form])
const fetchVendorDetails = async () => { const fetchVendorDetails = useCallback(async () => {
try { try {
setFetchLoading(true) setFetchLoading(true)
const response = await axios.get( const response = await axios.get(
@ -96,7 +96,7 @@ const VendorInfo = () => {
} finally { } finally {
setFetchLoading(false) setFetchLoading(false)
} }
} }, [messageApi, vendorId])
const startEditing = () => { const startEditing = () => {
updateCollapseState('info', true) updateCollapseState('info', true)
@ -137,7 +137,6 @@ const VendorInfo = () => {
if (err.errorFields) { if (err.errorFields) {
return return
} }
console.error('Failed to update vendor information:', err)
messageApi.error('Failed to update vendor information') messageApi.error('Failed to update vendor information')
} finally { } finally {
fetchVendorDetails() fetchVendorDetails()

View File

@ -304,11 +304,52 @@ const DashboardTable = forwardRef(
const tableData = pages.flatMap((page) => page.items) const tableData = pages.flatMap((page) => page.items)
// Card view rendering // Card view rendering
const cardsContainerRef = useRef(null)
// Card view scroll handler
useEffect(() => {
if (!cards) return
const container = cardsContainerRef.current
if (!container) return
const handleCardsScroll = (e) => {
const { scrollTop, scrollHeight, clientHeight } = e.target
const lowestPage = Math.min(...pages.map((p) => p.pageNum))
const prevPage = lowestPage - 1
// Load more data when scrolling down
if (
scrollHeight - scrollTop - clientHeight < 100 &&
hasMore &&
!lazyLoading
) {
setTimeout(() => {
e.target.scrollTop = scrollHeight / 2
}, 0)
setLazyLoading(true)
loadNextPage()
}
// Load previous data when scrolling up
if (scrollTop < 100 && prevPage > 0 && !lazyLoading) {
setTimeout(() => {
e.target.scrollTop = scrollHeight / 2
}, 0)
setLazyLoading(true)
loadPreviousPage()
}
}
container.addEventListener('scroll', handleCardsScroll)
return () => container.removeEventListener('scroll', handleCardsScroll)
}, [cards, pages, hasMore, lazyLoading, loadNextPage, loadPreviousPage])
const renderCards = () => { const renderCards = () => {
return ( return (
<Row <Row
gutter={[16, 16]} gutter={[16, 16]}
style={{ overflowY: 'auto', maxHeight: adjustedScrollHeight }} style={{ overflowY: 'auto', maxHeight: adjustedScrollHeight }}
ref={cardsContainerRef}
> >
{tableData.map((record) => { {tableData.map((record) => {
// Special case for columns[0] if needed // Special case for columns[0] if needed