Revize 394f2d16
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/Catalog/CatalogTable.tsx | ||
---|---|---|
13 | 13 |
import { CatalogItemDto } from '../../swagger/data-contracts' |
14 | 14 |
import ShowErrorIfPresent from '../Reusables/ShowErrorIfPresent' |
15 | 15 |
import ContentLoading from '../Reusables/ContentLoading' |
16 |
import axiosInstance from '../../api/api'
|
|
17 |
|
|
18 |
const apiError =
|
|
19 |
'Error while fetching data from the server, please try again later.'
|
|
16 |
import { RootState } from '../redux/store'
|
|
17 |
import { useDispatch, useSelector } from 'react-redux' |
|
18 |
import { consumeError, setLoading } from './catalogSlice'
|
|
19 |
import { fetchItems } from './catalogThunks'
|
|
20 | 20 |
|
21 | 21 |
// Catalog table component |
22 | 22 |
const CatalogTable = () => { |
... | ... | |
28 | 28 |
rowsPerPage[0] |
29 | 29 |
) |
30 | 30 |
|
31 |
const [items, setItems] = useState<CatalogItemDto[]>([]) |
|
32 |
const [areItemsLoading, setAreItemsLoading] = useState(true) |
|
33 |
const [err, setErr] = useState<string | undefined>(undefined) |
|
31 |
// Subscribe to the store |
|
32 |
const items = useSelector((state: RootState) => state.catalog.items) |
|
33 |
const loading = useSelector((state: RootState) => state.catalog.loading) |
|
34 |
const apiError = useSelector((state: RootState) => state.catalog.error) |
|
35 |
|
|
36 |
const [displayError, setDisplayError] = useState<string | undefined>(undefined) |
|
34 | 37 |
|
35 | 38 |
// When changing rows per page set the selected number and reset to the first page |
36 | 39 |
const onRowsPerPageChange = ( |
... | ... | |
40 | 43 |
setPage(0) |
41 | 44 |
} |
42 | 45 |
|
43 |
// Use effect hook to fetch rows from the server |
|
46 |
const dispatch = useDispatch() |
|
47 |
|
|
44 | 48 |
useEffect(() => { |
45 |
// Function to fetch items from the API |
|
46 |
const fetchItems = async () => { |
|
47 |
try { |
|
48 |
const { data, status } = await axiosInstance.get( |
|
49 |
'/catalog-items' |
|
50 |
) |
|
51 |
if (status !== 200) { |
|
52 |
setErr(apiError) |
|
53 |
return |
|
54 |
} |
|
49 |
// Fetch items when the component is mounted |
|
50 |
// This will automatically search whenever the filter changes |
|
51 |
dispatch(fetchItems()) |
|
55 | 52 |
|
56 |
setItems(data) |
|
57 |
setAreItemsLoading(false) |
|
58 |
} catch (err: any) { |
|
59 |
setErr(apiError) |
|
60 |
} |
|
53 |
return () => { |
|
54 |
// Invalidate the state when unmounting so that the old list is not rerendered when the user returns to the page |
|
55 |
dispatch(setLoading()) |
|
56 |
} |
|
57 |
}, [dispatch]) |
|
58 |
|
|
59 |
// Use effect to read the error and consume it |
|
60 |
useEffect(() => { |
|
61 |
if (apiError) { |
|
62 |
setDisplayError(apiError) |
|
63 |
dispatch(consumeError()) |
|
61 | 64 |
} |
65 |
}, [apiError, dispatch]) |
|
62 | 66 |
|
63 |
fetchItems() |
|
64 |
}, []) |
|
65 | 67 |
|
66 | 68 |
// Name of columns in the header |
67 | 69 |
const columns = [ |
... | ... | |
107 | 109 |
|
108 | 110 |
return ( |
109 | 111 |
<Fragment> |
110 |
<ShowErrorIfPresent err={err} />
|
|
111 |
{areItemsLoading && !err ? <ContentLoading /> : null}
|
|
112 |
{!areItemsLoading && !err ? (
|
|
113 |
<Fragment> |
|
112 |
<ShowErrorIfPresent err={displayError} />
|
|
113 |
{loading && !displayError ? <ContentLoading /> : null}
|
|
114 |
{!loading && !displayError ? (
|
|
115 |
<Fragment>
|
|
114 | 116 |
<TableContainer> |
115 | 117 |
<Table |
116 | 118 |
stickyHeader |
Také k dispozici: Unified diff
catalog slice
re #9545