forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithPolicyAndFullscreenLoading.tsx
More file actions
64 lines (54 loc) · 2.67 KB
/
Copy pathwithPolicyAndFullscreenLoading.tsx
File metadata and controls
64 lines (54 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
58
59
60
61
62
63
64
import isEmpty from 'lodash/isEmpty';
import type {ComponentType, ForwardedRef, RefAttributes} from 'react';
import React, {forwardRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import useReportDataLoading from '@hooks/useReportDataLoading';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList} from '@src/types/onyx';
import type {WithPolicyOnyxProps, WithPolicyProps} from './withPolicy';
import withPolicy, {policyDefaultProps} from './withPolicy';
type WithPolicyAndFullscreenLoadingOnyxProps = {
/** Indicated whether the report data is loading */
isLoadingReportData: OnyxEntry<boolean>;
/** Personal details of all users */
personalDetails: OnyxEntry<PersonalDetailsList>;
};
type WithPolicyAndFullscreenLoadingProps = WithPolicyProps & WithPolicyAndFullscreenLoadingOnyxProps;
type ComponentWithPolicyAndFullscreenLoading<TProps extends WithPolicyAndFullscreenLoadingProps, TRef> = ComponentType<
Omit<Omit<TProps & RefAttributes<TRef>, keyof WithPolicyAndFullscreenLoadingOnyxProps>, keyof WithPolicyOnyxProps>
>;
export default function withPolicyAndFullscreenLoading<TProps extends WithPolicyAndFullscreenLoadingProps, TRef>(
WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>,
): ComponentWithPolicyAndFullscreenLoading<TProps, TRef> {
function WithPolicyAndFullscreenLoading(
{
policy = policyDefaultProps.policy,
policyDraft = policyDefaultProps.policyDraft,
isLoadingPolicy = policyDefaultProps.isLoadingPolicy,
...rest
}: Omit<TProps, keyof WithPolicyAndFullscreenLoadingOnyxProps>,
ref: ForwardedRef<TRef>,
) {
const isLoadingReportData = useReportDataLoading();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: true});
if ((isLoadingPolicy || isLoadingReportData) && isEmpty(policy) && isEmpty(policyDraft)) {
return <FullscreenLoadingIndicator />;
}
return (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...(rest as TProps)}
isLoadingReportData={isLoadingReportData}
personalDetails={personalDetails}
policy={policy}
policyDraft={policyDraft}
ref={ref}
/>
);
}
WithPolicyAndFullscreenLoading.displayName = `WithPolicyAndFullscreenLoading`;
return withPolicy(forwardRef(WithPolicyAndFullscreenLoading));
}
export type {WithPolicyAndFullscreenLoadingProps};