Skip to content

Commit ae9693b

Browse files
committed
another approach to prevent flickering in subforms
1 parent 12f61fd commit ae9693b

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

src/features/payment/PaymentProvider.tsx

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,12 @@ type PaymentContextProvider = {
2424

2525
export const PaymentContext = createContext<PaymentContextProps | undefined>(undefined);
2626

27-
const EmptyPaymentProvider: React.FC<PaymentContextProvider> = ({ children }) => {
28-
const contextValue = {
29-
setLoading: () => {},
30-
performPayment: () => {},
31-
paymentError: null,
32-
};
33-
return <PaymentContext.Provider value={contextValue}>{children}</PaymentContext.Provider>;
34-
};
35-
36-
const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children }) => {
27+
export const PaymentProvider: React.FC<PaymentContextProvider> = ({ children }) => {
3728
const [loading, setLoading] = useState<boolean>(false);
3829
const partyId = useNavigationParam('partyId');
3930
const instanceGuid = useNavigationParam('instanceGuid');
40-
const paymentInfo = usePaymentInformation();
41-
const isPdf = useIsPdf();
42-
const { next, busy } = useProcessNavigation() || {};
4331
const { mutate, error } = usePerformPayActionMutation(partyId, instanceGuid);
32+
const isSubformPage = useIsSubformPage();
4433

4534
const contextValue: PaymentContextProps = useMemo(
4635
() => ({
@@ -54,6 +43,31 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
5443
[setLoading, mutate, error],
5544
);
5645

46+
// If payment failed, stop loading
47+
React.useEffect(() => {
48+
if (error) {
49+
setLoading(false);
50+
}
51+
}, [error]);
52+
53+
if (loading) {
54+
return <Loader reason='Navigating to external payment solution' />;
55+
}
56+
57+
return (
58+
<PaymentContext.Provider value={contextValue}>
59+
{children}
60+
{!isSubformPage && !error && <PaymentNavigation />}
61+
</PaymentContext.Provider>
62+
);
63+
};
64+
65+
function PaymentNavigation() {
66+
const { next, busy } = useProcessNavigation() || {};
67+
const paymentInfo = usePaymentInformation();
68+
const isPdf = useIsPdf();
69+
const { setLoading, performPayment } = usePayment();
70+
5771
const paymentDoesNotExist = paymentInfo?.status === PaymentStatus.Uninitialized;
5872
const isPaymentProcess = useIsPayment();
5973
const actionCalled = useRef(false);
@@ -64,9 +78,9 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
6478
if (isPaymentProcess && paymentDoesNotExist && !actionCalled.current && !isPdf) {
6579
actionCalled.current = true;
6680
setLoading(true);
67-
mutate();
81+
performPayment();
6882
}
69-
}, [isPaymentProcess, paymentDoesNotExist, mutate, isPdf]);
83+
}, [isPaymentProcess, paymentDoesNotExist, performPayment, setLoading, isPdf]);
7084

7185
const paymentCompleted = paymentInfo?.status === PaymentStatus.Paid || paymentInfo?.status === PaymentStatus.Skipped;
7286
const nextCalled = useRef(false);
@@ -78,29 +92,10 @@ const FunctionalPaymentProvider: React.FC<PaymentContextProvider> = ({ children
7892
setLoading(true);
7993
next({ action: 'confirm', nodeId: 'next-button' });
8094
}
81-
}, [paymentCompleted, next, busy, isPdf]);
82-
83-
useEffect(() => {
84-
if (error) {
85-
setLoading(false);
86-
}
87-
}, [error]);
88-
89-
if (loading) {
90-
return <Loader reason='Navigating to external payment solution' />;
91-
}
92-
93-
return <PaymentContext.Provider value={contextValue}>{children}</PaymentContext.Provider>;
94-
};
95+
}, [paymentCompleted, setLoading, next, busy, isPdf]);
9596

96-
export const PaymentProvider: React.FC<PaymentContextProvider> = ({ children }) => {
97-
const isSubformPage = useIsSubformPage();
98-
if (isSubformPage) {
99-
return <EmptyPaymentProvider>{children}</EmptyPaymentProvider>;
100-
}
101-
102-
return <FunctionalPaymentProvider>{children}</FunctionalPaymentProvider>;
103-
};
97+
return null;
98+
}
10499

105100
export const usePayment = () => {
106101
const context = useContext(PaymentContext);

0 commit comments

Comments
 (0)