|
|
|
|
|
|
|
|
import React, { useEffect } from 'react'; |
|
|
|
|
|
|
|
|
import React, { useEffect, useState } from 'react'; |
|
|
import { |
|
|
import { |
|
|
Paper, |
|
|
Paper, |
|
|
Box, |
|
|
Box, |
|
|
|
|
|
|
|
|
} from '@mui/material'; |
|
|
} from '@mui/material'; |
|
|
// import { useTranslation } from 'react-i18next'; |
|
|
// import { useTranslation } from 'react-i18next'; |
|
|
import { useDispatch, useSelector, batch } from 'react-redux'; |
|
|
import { useDispatch, useSelector, batch } from 'react-redux'; |
|
|
|
|
|
import useDebounce from '../../../hooks/useDebounceHook'; |
|
|
import { |
|
|
import { |
|
|
itemsSelector, |
|
|
itemsSelector, |
|
|
pageSelector, |
|
|
pageSelector, |
|
|
itemsPerPageSelector, |
|
|
itemsPerPageSelector, |
|
|
countSelector, |
|
|
countSelector, |
|
|
filterSelector, |
|
|
|
|
|
sortSelector, |
|
|
sortSelector, |
|
|
} from '../../../store/selectors/randomDataSelectors'; |
|
|
} from '../../../store/selectors/randomDataSelectors'; |
|
|
import { |
|
|
import { |
|
|
|
|
|
|
|
|
} from '../../../store/actions/randomData/randomDataActions'; |
|
|
} from '../../../store/actions/randomData/randomDataActions'; |
|
|
|
|
|
|
|
|
const PagingSortingFilteringExample = () => { |
|
|
const PagingSortingFilteringExample = () => { |
|
|
|
|
|
const [filterText, setFilterText] = useState(''); |
|
|
|
|
|
|
|
|
const dispatch = useDispatch(); |
|
|
const dispatch = useDispatch(); |
|
|
// const { t } = useTranslation(); |
|
|
// const { t } = useTranslation(); |
|
|
const items = useSelector(itemsSelector); |
|
|
const items = useSelector(itemsSelector); |
|
|
const currentPage = useSelector(pageSelector); |
|
|
const currentPage = useSelector(pageSelector); |
|
|
const itemsPerPage = useSelector(itemsPerPageSelector); |
|
|
const itemsPerPage = useSelector(itemsPerPageSelector); |
|
|
const totalCount = useSelector(countSelector); |
|
|
const totalCount = useSelector(countSelector); |
|
|
const filter = useSelector(filterSelector); |
|
|
|
|
|
const sort = useSelector(sortSelector) || 'name-asc'; |
|
|
const sort = useSelector(sortSelector) || 'name-asc'; |
|
|
|
|
|
|
|
|
|
|
|
// Use debounce to prevent too many rerenders |
|
|
|
|
|
const debouncedFilterText = useDebounce(filterText, 500); |
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
useEffect(() => { |
|
|
dispatch(loadData(30)); |
|
|
dispatch(loadData(30)); |
|
|
dispatch(updateSort(sort)); |
|
|
dispatch(updateSort(sort)); |
|
|
}, []); |
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
batch(() => { |
|
|
|
|
|
dispatch(updateFilter(filterText)); |
|
|
|
|
|
currentPage > 0 && dispatch(updatePage(0)); |
|
|
|
|
|
}); |
|
|
|
|
|
}, [debouncedFilterText]); |
|
|
|
|
|
|
|
|
|
|
|
const handleFilterTextChange = (event) => { |
|
|
|
|
|
const filterText = event.target.value; |
|
|
|
|
|
setFilterText(filterText); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleSortChange = (event) => { |
|
|
|
|
|
const sort = event.target.value; |
|
|
|
|
|
dispatch(updateSort(sort)); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
const handlePageChange = (event, newPage) => { |
|
|
const handlePageChange = (event, newPage) => { |
|
|
dispatch(updatePage(newPage)); |
|
|
dispatch(updatePage(newPage)); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
const handleFilterChange = (event) => { |
|
|
|
|
|
const filter = event.target.value; |
|
|
|
|
|
batch(() => { |
|
|
|
|
|
dispatch(updateFilter(filter)); |
|
|
|
|
|
currentPage > 0 && dispatch(updatePage(0)); |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleSortChange = (event) => { |
|
|
|
|
|
const sort = event.target.value; |
|
|
|
|
|
dispatch(updateSort(sort)); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<Paper |
|
|
<Paper |
|
|
|
|
|
|
|
|
variant="outlined" |
|
|
variant="outlined" |
|
|
label="Filter" |
|
|
label="Filter" |
|
|
placeholder="Filter" |
|
|
placeholder="Filter" |
|
|
value={filter} |
|
|
|
|
|
onChange={handleFilterChange} |
|
|
|
|
|
|
|
|
value={filterText} |
|
|
|
|
|
onChange={handleFilterTextChange} |
|
|
/> |
|
|
/> |
|
|
</Box> |
|
|
</Box> |
|
|
</Box> |
|
|
</Box> |