UNPKG

2.49 kBJSXView Raw
1// @flow
2
3import * as React from "react";
4
5type LoadDataItemPropTypes = {
6 isFetching: bool;
7 fetched: bool;
8 id: string;
9 hasError: bool;
10 data: any;
11 children: React.Node;
12 shouldFetch: any;
13
14 fetchItem: () => void;
15};
16
17type OwnProps = {
18 id: string;
19 settings: {
20 selectors: any;
21 dataItemURL: string;
22 endpoint: string;
23 manageEntity: any;
24 normalizeDataItem: any;
25 }
26};
27
28import { connect } from "react-redux";
29
30// import {
31// useLocation,
32// useNavigate,
33// useParams,
34// } from "react-router-dom";
35
36// const withRouter = (Component) => {
37// const ComponentWithRouterProp = (props) => {
38// const location = useLocation();
39// const navigate = useNavigate();
40// const params = useParams();
41
42// return (
43// <Component
44// {...props}
45// router={{
46// location,
47// navigate,
48// params,
49// }}
50// />
51// );
52// };
53
54// return ComponentWithRouterProp;
55// };
56
57import { fetchItem as fetchItemAction } from "./actions";
58
59import { LargeErrorMessage, LoadingMessage } from "x25/Messages";
60import words from "./words";
61
62const
63 mapStateToProps = (state : any, { settings : { selectors }, id } : OwnProps) => ({
64 data : selectors.getItem(state, id),
65 hasError : selectors.getItemHasError(state, id),
66 fetched : selectors.getItemIsFetched(state, id),
67 isFetching : selectors.getIsFetchingItemInfo(state, id),
68 shouldFetch : selectors.getShouldFetchItemInfo(state, id),
69 }),
70 mapDispatchToProps = (dispatch : any, { id, settings } : OwnProps) => ({
71 fetchItem () {
72 dispatch(fetchItemAction({
73 ...settings,
74 id,
75 }));
76 },
77 });
78
79const LoadDataItem = (props : LoadDataItemPropTypes) => {
80 const { children, data, isFetching, shouldFetch, hasError, fetchItem } = props;
81
82 React.useEffect(() => {
83 if (shouldFetch) {
84 fetchItem();
85 }
86 }, [
87 shouldFetch,
88 isFetching,
89 hasError,
90 ]);
91
92 if (isFetching) {
93 return (
94 <LoadingMessage message={words.LoadingData} />
95 );
96 }
97
98 if (hasError) {
99 return (
100 <LargeErrorMessage
101 message={words.ThereWasAProblem}
102 onRetry={fetchItem}
103 />
104 );
105 }
106
107 if (data.size === 0) {
108 return null;
109 }
110
111 return (
112 children
113 );
114};
115
116export default connect(mapStateToProps, mapDispatchToProps)(LoadDataItem);