|
6 | 6 | jsx: (
|
7 | 7 | <div>
|
8 | 8 | {(() => {
|
9 |
| - const { env, Children, useAction, useGetAll } = B; |
| 9 | + const { env, Children, Action, useGetAll } = B; |
10 | 10 |
|
11 | 11 | const {
|
12 | 12 | actionId,
|
|
16 | 16 | formSuccessMessage,
|
17 | 17 | redirect,
|
18 | 18 | showError,
|
| 19 | + showSuccess, |
19 | 20 | } = options;
|
20 |
| - |
21 | 21 | const formRef = React.createRef();
|
22 | 22 |
|
23 | 23 | const displayError = showError === 'built-in';
|
| 24 | + const displaySuccess = showSuccess === 'built-in'; |
24 | 25 | const empty = children.length === 0;
|
25 | 26 | const isDev = B.env === 'dev';
|
26 | 27 | const isPristine = empty && isDev;
|
|
31 | 32 | const [isInvalid, setIsInvalid] = useState(false);
|
32 | 33 | const location = isDev ? {} : useLocation();
|
33 | 34 |
|
34 |
| - const [callAction, { data, loading, error }] = useAction(actionId, { |
35 |
| - onCompleted({ actionb5 }) { |
36 |
| - if (actionb5) { |
37 |
| - B.triggerEvent('onSuccess', actionb5); |
38 |
| - } else { |
39 |
| - B.triggerEvent('onNoResults'); |
40 |
| - } |
41 |
| - if (hasRedirect) { |
42 |
| - if (redirectTo === location.pathname) { |
43 |
| - history.go(0); |
44 |
| - } else { |
45 |
| - history.push(redirectTo); |
46 |
| - } |
47 |
| - } |
48 |
| - }, |
49 |
| - onError(err) { |
50 |
| - if (err && !displayError) { |
51 |
| - B.triggerEvent('onError', formErrorMessage || err.message); |
52 |
| - } |
53 |
| - }, |
54 |
| - }); |
55 |
| - |
56 | 35 | const { loading: isFetching, data: models, error: err } =
|
57 | 36 | model &&
|
58 | 37 | useGetAll(model, {
|
|
61 | 40 | take: 1,
|
62 | 41 | });
|
63 | 42 |
|
64 |
| - if (loading) { |
65 |
| - B.triggerEvent('onLoad', loading); |
66 |
| - } |
67 |
| - |
68 | 43 | const mounted = useRef(true);
|
69 | 44 | useEffect(() => {
|
70 | 45 | if (!mounted.current && isFetching) {
|
71 |
| - B.triggerEvent('onLoad', isFetching); |
| 46 | + B.triggerEvent('onDataLoad', isFetching); |
72 | 47 | }
|
73 | 48 | mounted.current = false;
|
74 | 49 | }, [isFetching]);
|
75 | 50 |
|
76 | 51 | if (err && !displayError) {
|
77 |
| - B.triggerEvent('onError', formErrorMessage || err.message); |
| 52 | + B.triggerEvent('onDataError', formErrorMessage || err.message); |
78 | 53 | }
|
79 | 54 |
|
80 | 55 | const item = models && models.results[0];
|
81 | 56 |
|
82 | 57 | if (item) {
|
83 | 58 | if (item.id) {
|
84 |
| - B.triggerEvent('onSuccess', item); |
| 59 | + B.triggerEvent('onDataSuccess', item); |
85 | 60 | } else {
|
86 |
| - B.triggerEvent('onNoResults'); |
| 61 | + B.triggerEvent('onDataNoResults'); |
87 | 62 | }
|
88 | 63 | }
|
89 | 64 |
|
|
94 | 69 | }
|
95 | 70 | };
|
96 | 71 |
|
97 |
| - const handleSubmit = evt => { |
| 72 | + const handleSubmit = (evt, callAction) => { |
98 | 73 | evt.preventDefault();
|
99 | 74 | setIsInvalid(false);
|
100 | 75 | B.triggerEvent('onSubmit');
|
|
107 | 82 | callAction({ variables: { input: values } });
|
108 | 83 | };
|
109 | 84 |
|
110 |
| - const renderContent = () => { |
| 85 | + const renderContent = loading => { |
111 | 86 | if (!model || isDev) {
|
112 | 87 | return <Children loading={loading}>{children}</Children>;
|
113 | 88 | }
|
|
121 | 96 | );
|
122 | 97 | };
|
123 | 98 |
|
| 99 | + const trigger = (data, loading, error) => { |
| 100 | + if (data) { |
| 101 | + B.triggerEvent('onActionSuccess', data); |
| 102 | + |
| 103 | + if (hasRedirect) { |
| 104 | + if (redirectTo === location.pathname) { |
| 105 | + history.go(0); |
| 106 | + } else { |
| 107 | + history.push(redirectTo); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (loading) { |
| 113 | + B.triggerEvent('onActionLoad', loading); |
| 114 | + } |
| 115 | + |
| 116 | + if (error) { |
| 117 | + B.triggerEvent('onActionError', formErrorMessage || error.message); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
124 | 121 | return (
|
125 |
| - <> |
126 |
| - <div className={classes.messageContainer}> |
127 |
| - {error && displayError && ( |
128 |
| - <span className={classes.error}>{formErrorMessage}</span> |
129 |
| - )} |
130 |
| - {data && ( |
131 |
| - <span className={classes.success}>{formSuccessMessage}</span> |
132 |
| - )} |
133 |
| - </div> |
134 |
| - |
135 |
| - <form |
136 |
| - onInvalid={handleInvalid} |
137 |
| - onSubmit={handleSubmit} |
138 |
| - ref={formRef} |
139 |
| - className={[ |
140 |
| - empty && classes.empty, |
141 |
| - isPristine && classes.pristine, |
142 |
| - ].join(' ')} |
143 |
| - > |
144 |
| - {isPristine && <span>form</span>} |
145 |
| - {renderContent()} |
146 |
| - </form> |
147 |
| - </> |
| 122 | + <Action actionId={actionId}> |
| 123 | + {(callAction, { data, loading, error }) => ( |
| 124 | + <> |
| 125 | + {trigger(data, loading, error)} |
| 126 | + <div className={classes.messageContainer}> |
| 127 | + {error && displayError && ( |
| 128 | + <span className={classes.error}>{formErrorMessage}</span> |
| 129 | + )} |
| 130 | + {data && displaySuccess && ( |
| 131 | + <span className={classes.success}> |
| 132 | + {formSuccessMessage} |
| 133 | + </span> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + |
| 137 | + <form |
| 138 | + onInvalid={handleInvalid} |
| 139 | + onSubmit={evt => handleSubmit(evt, callAction)} |
| 140 | + ref={formRef} |
| 141 | + className={[ |
| 142 | + empty && classes.empty, |
| 143 | + isPristine && classes.pristine, |
| 144 | + ].join(' ')} |
| 145 | + > |
| 146 | + {isPristine && <span>form</span>} |
| 147 | + {renderContent(loading)} |
| 148 | + </form> |
| 149 | + </> |
| 150 | + )} |
| 151 | + </Action> |
148 | 152 | );
|
149 | 153 | })()}
|
150 | 154 | </div>
|
|
0 commit comments