Skip to content

Commit

Permalink
remove feature wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
BrtqKr committed Apr 19, 2024
1 parent 5829f66 commit 6a18afb
Show file tree
Hide file tree
Showing 44 changed files with 1,361 additions and 1,469 deletions.
49 changes: 39 additions & 10 deletions src/pages/workspace/AccessOrNotFoundWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, {useEffect} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
Expand All @@ -10,6 +11,8 @@ import * as Policy from '@userActions/Policy';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type * as OnyxTypes from '@src/types/onyx';
import type {PolicyFeatureName} from '@src/types/onyx/Policy';
import callOrReturn from '@src/types/utils/callOrReturn';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

const POLICY_ACCESS_VARIANTS = {
Expand All @@ -36,38 +39,64 @@ type AccessOrNotFoundWrapperProps = AccessOrNotFoundWrapperOnyxProps & {

/** Defines which types of access should be verified */
accessVariants?: PolicyAccessVariant[];

/** The current feature name that the user tries to get access to */
featureName?: PolicyFeatureName;
};

function AccessOrNotFoundWrapper({accessVariants = ['ADMIN', 'PAID'], ...props}: AccessOrNotFoundWrapperProps) {
const isPolicyIDInRoute = !!props.policyID?.length;
type PageNotFoundFallackProps = Pick<AccessOrNotFoundWrapperProps, 'policyID'> & {showFullScreenFallback: boolean};

function PageNotFoundFallback({policyID, showFullScreenFallback}: PageNotFoundFallackProps) {
return showFullScreenFallback ? (
<FullPageNotFoundView
shouldShow
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)}
shouldForceFullScreen
/>
) : (
<NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_PROFILE.getRoute(policyID))} />
);
}

function AccessOrNotFoundWrapper({accessVariants = [], ...props}: AccessOrNotFoundWrapperProps) {
const {policy, policyID, featureName, isLoadingReportData} = props;

const isPolicyIDInRoute = !!policyID?.length;

useEffect(() => {
if (!isPolicyIDInRoute || !isEmptyObject(props.policy)) {
if (!isPolicyIDInRoute || !isEmptyObject(policy)) {
// If the workspace is not required or is already loaded, we don't need to call the API
return;
}

Policy.openWorkspace(props.policyID, []);
Policy.openWorkspace(policyID, []);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isPolicyIDInRoute, props.policyID]);
}, [isPolicyIDInRoute, policyID]);

const shouldShowFullScreenLoadingIndicator = props.isLoadingReportData !== false && (!Object.entries(props.policy ?? {}).length || !props.policy?.id);
const shouldShowFullScreenLoadingIndicator = isLoadingReportData !== false && (!Object.entries(policy ?? {}).length || !policy?.id);

const isFeatureEnabled = featureName ? PolicyUtils.isPolicyFeatureEnabled(policy, featureName) : true;
const pageUnaccessible = accessVariants.reduce((acc, variant) => {
const accessFunction = POLICY_ACCESS_VARIANTS[variant];
return acc || accessFunction(props.policy);
return acc || accessFunction(policy);
}, false);
const shouldShowNotFoundPage = isEmptyObject(props.policy) || !props.policy?.id || pageUnaccessible;

const shouldShowNotFoundPage = isEmptyObject(policy) || (Object.keys(policy).length === 1 && !isEmptyObject(policy.errors)) || !policy?.id || pageUnaccessible || !isFeatureEnabled;

if (shouldShowFullScreenLoadingIndicator) {
return <FullscreenLoadingIndicator />;
}

if (shouldShowNotFoundPage) {
return <NotFoundPage onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_PROFILE.getRoute(props.policyID))} />;
return (
<PageNotFoundFallback
policyID={policyID}
showFullScreenFallback={!isFeatureEnabled}
/>
);
}

return typeof props.children === 'function' ? props.children(props) : props.children;
return callOrReturn(props.children, props);
}

export default withOnyx<AccessOrNotFoundWrapperProps, AccessOrNotFoundWrapperOnyxProps>({
Expand Down
74 changes: 0 additions & 74 deletions src/pages/workspace/FeatureEnabledAccessOrNotFoundWrapper.tsx

This file was deleted.

5 changes: 4 additions & 1 deletion src/pages/workspace/WorkspaceMoreFeaturesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
);

return (
<AccessOrNotFoundWrapper policyID={route.params.policyID}>
<AccessOrNotFoundWrapper
accessVariants={['ADMIN', 'PAID']}
policyID={route.params.policyID}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
style={[styles.defaultModalContainer]}
Expand Down
98 changes: 48 additions & 50 deletions src/pages/workspace/accounting/PolicyAccountingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import useWindowDimensions from '@hooks/useWindowDimensions';
import {removePolicyConnection} from '@libs/actions/connections';
import Navigation from '@navigation/Navigation';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import withPolicyConnections from '@pages/workspace/withPolicyConnections';
import type {AnchorPosition} from '@styles/index';
Expand Down Expand Up @@ -174,56 +173,55 @@ function PolicyAccountingPage({policy, connectionSyncProgress}: PolicyAccounting
];

return (
<AccessOrNotFoundWrapper policyID={policyID}>
<FeatureEnabledAccessOrNotFoundWrapper
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED}
<AccessOrNotFoundWrapper
accessVariants={['ADMIN', 'PAID']}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED}
>
<ScreenWrapper
testID={PolicyAccountingPage.displayName}
includeSafeAreaPaddingBottom={false}
shouldShowOfflineIndicatorInWideScreen
>
<ScreenWrapper
testID={PolicyAccountingPage.displayName}
includeSafeAreaPaddingBottom={false}
shouldShowOfflineIndicatorInWideScreen
>
<HeaderWithBackButton
title={translate('workspace.common.accounting')}
shouldShowBackButton={isSmallScreenWidth}
icon={Illustrations.Accounting}
shouldShowThreeDotsButton
threeDotsAnchorPosition={styles.threeDotsPopoverOffsetNoCloseButton(windowWidth)}
threeDotsMenuItems={headerThreeDotsMenuItems}
/>
<ScrollView contentContainerStyle={styles.pt3}>
<View style={[styles.flex1, isSmallScreenWidth ? styles.workspaceSectionMobile : styles.workspaceSection]}>
<Section
title={translate('workspace.accounting.title')}
subtitle={translate('workspace.accounting.subtitle')}
isCentralPane
subtitleMuted
titleStyles={styles.accountSettingsSectionTitle}
childrenStyles={styles.pt5}
>
<MenuItemList
menuItems={menuItems}
shouldUseSingleExecution
/>
</Section>
</View>
</ScrollView>
<ConfirmModal
title={translate('workspace.accounting.disconnectTitle')}
isVisible={isDisconnectModalOpen}
onConfirm={() => {
removePolicyConnection(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO);
setIsDisconnectModalOpen(false);
}}
onCancel={() => setIsDisconnectModalOpen(false)}
prompt={translate('workspace.accounting.disconnectPrompt')}
confirmText={translate('workspace.accounting.disconnect')}
cancelText={translate('common.cancel')}
danger
/>
</ScreenWrapper>
</FeatureEnabledAccessOrNotFoundWrapper>
<HeaderWithBackButton
title={translate('workspace.common.accounting')}
shouldShowBackButton={isSmallScreenWidth}
icon={Illustrations.Accounting}
shouldShowThreeDotsButton
threeDotsAnchorPosition={styles.threeDotsPopoverOffsetNoCloseButton(windowWidth)}
threeDotsMenuItems={headerThreeDotsMenuItems}
/>
<ScrollView contentContainerStyle={styles.pt3}>
<View style={[styles.flex1, isSmallScreenWidth ? styles.workspaceSectionMobile : styles.workspaceSection]}>
<Section
title={translate('workspace.accounting.title')}
subtitle={translate('workspace.accounting.subtitle')}
isCentralPane
subtitleMuted
titleStyles={styles.accountSettingsSectionTitle}
childrenStyles={styles.pt5}
>
<MenuItemList
menuItems={menuItems}
shouldUseSingleExecution
/>
</Section>
</View>
</ScrollView>
<ConfirmModal
title={translate('workspace.accounting.disconnectTitle')}
isVisible={isDisconnectModalOpen}
onConfirm={() => {
removePolicyConnection(policyID, CONST.POLICY.CONNECTIONS.NAME.QBO);
setIsDisconnectModalOpen(false);
}}
onCancel={() => setIsDisconnectModalOpen(false)}
prompt={translate('workspace.accounting.disconnectPrompt')}
confirmText={translate('workspace.accounting.disconnect')}
cancelText={translate('common.cancel')}
danger
/>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Connections from '@libs/actions/connections';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import FeatureEnabledAccessOrNotFoundWrapper from '@pages/workspace/FeatureEnabledAccessOrNotFoundWrapper';
import withPolicy from '@pages/workspace/withPolicy';
import type {WithPolicyProps} from '@pages/workspace/withPolicy';
import variables from '@styles/variables';
Expand All @@ -27,46 +26,42 @@ function QuickbooksChartOfAccountsPage({policy}: WithPolicyProps) {
<AccessOrNotFoundWrapper
accessVariants={['ADMIN']}
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED}
>
<FeatureEnabledAccessOrNotFoundWrapper
policyID={policyID}
featureName={CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED}
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={QuickbooksChartOfAccountsPage.displayName}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={QuickbooksChartOfAccountsPage.displayName}
>
<HeaderWithBackButton title={translate('workspace.qbo.accounts')} />
<ScrollView contentContainerStyle={[styles.pb2, styles.ph5]}>
<Text style={styles.pb5}>{translate('workspace.qbo.accountsDescription')}</Text>
<View style={[styles.flexRow, styles.mb2, styles.alignItemsCenter, styles.justifyContentBetween]}>
<View style={styles.flex1}>
<Text fontSize={variables.fontSizeNormal}>{translate('workspace.qbo.accountsSwitchTitle')}</Text>
</View>
<OfflineWithFeedback pendingAction={pendingFields?.enableNewCategories}>
<View style={[styles.flex1, styles.alignItemsEnd, styles.pl3]}>
<Switch
accessibilityLabel={translate('workspace.qbo.accounts')}
isOn={isSwitchOn}
onToggle={() =>
Connections.updatePolicyConnectionConfig(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICKBOOKS_IMPORTS.ENABLE_NEW_CATEGORIES,
isSwitchOn ? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE : CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
)
}
/>
</View>
</OfflineWithFeedback>
</View>
<HeaderWithBackButton title={translate('workspace.qbo.accounts')} />
<ScrollView contentContainerStyle={[styles.pb2, styles.ph5]}>
<Text style={styles.pb5}>{translate('workspace.qbo.accountsDescription')}</Text>
<View style={[styles.flexRow, styles.mb2, styles.alignItemsCenter, styles.justifyContentBetween]}>
<View style={styles.flex1}>
<Text style={styles.mutedTextLabel}>{translate('workspace.qbo.accountsSwitchDescription')}</Text>
<Text fontSize={variables.fontSizeNormal}>{translate('workspace.qbo.accountsSwitchTitle')}</Text>
</View>
</ScrollView>
</ScreenWrapper>
</FeatureEnabledAccessOrNotFoundWrapper>
<OfflineWithFeedback pendingAction={pendingFields?.enableNewCategories}>
<View style={[styles.flex1, styles.alignItemsEnd, styles.pl3]}>
<Switch
accessibilityLabel={translate('workspace.qbo.accounts')}
isOn={isSwitchOn}
onToggle={() =>
Connections.updatePolicyConnectionConfig(
policyID,
CONST.POLICY.CONNECTIONS.NAME.QBO,
CONST.QUICKBOOKS_IMPORTS.ENABLE_NEW_CATEGORIES,
isSwitchOn ? CONST.INTEGRATION_ENTITY_MAP_TYPES.NONE : CONST.INTEGRATION_ENTITY_MAP_TYPES.TAG,
)
}
/>
</View>
</OfflineWithFeedback>
</View>
<View style={styles.flex1}>
<Text style={styles.mutedTextLabel}>{translate('workspace.qbo.accountsSwitchDescription')}</Text>
</View>
</ScrollView>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
Expand Down
Loading

0 comments on commit 6a18afb

Please sign in to comment.