1 |
|
2 |
|
3 | import * as React from "react";
|
4 |
|
5 | type 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 |
|
17 | type OwnProps = {
|
18 | id: string;
|
19 | settings: {
|
20 | selectors: any;
|
21 | dataItemURL: string;
|
22 | endpoint: string;
|
23 | manageEntity: any;
|
24 | normalizeDataItem: any;
|
25 | }
|
26 | };
|
27 |
|
28 | import { connect } from "react-redux";
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | import { fetchItem as fetchItemAction } from "./actions";
|
58 |
|
59 | import { LargeErrorMessage, LoadingMessage } from "x25/Messages";
|
60 | import words from "./words";
|
61 |
|
62 | const
|
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 |
|
79 | const 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 |
|
116 | export default connect(mapStateToProps, mapDispatchToProps)(LoadDataItem);
|