-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Simplified Collect][CLEAN UP][LOW] Standardize on Workspace wrapper #40598
Merged
luacmartins
merged 14 commits into
Expensify:main
from
software-mansion-labs:brtqkr/standardize-wrappers
Apr 29, 2024
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4d15239
merge admin and paid policy wrappers
BrtqKr 5829f66
cleanup
BrtqKr 6a18afb
remove feature wrapper
BrtqKr a5d5822
fix eslint
BrtqKr 26aed7a
review fixes wip
BrtqKr 02e1191
Merge remote-tracking branch 'origin/main' into brtqkr/standardize-wr…
BrtqKr d9a2b22
Merge remote-tracking branch 'origin/main' into brtqkr/standardize-wr…
BrtqKr ae7b35e
fix typo
BrtqKr 54cf1f5
move access variants to consts
BrtqKr 572e0f6
Merge remote-tracking branch 'origin/main' into brtqkr/standardize-wr…
BrtqKr e72d7bf
Merge remote-tracking branch 'origin/main' into brtqkr/standardize-wr…
BrtqKr 199c918
fix remaining wrappers
BrtqKr dd27831
remove admin wrapper
BrtqKr 8fc1426
add props spread for not found page
BrtqKr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* eslint-disable rulesdir/no-negated-variables */ | ||
import React, {useEffect} from 'react'; | ||
import type {OnyxEntry} from 'react-native-onyx'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import type {FullPageNotFoundViewProps} from '@components/BlockingViews/FullPageNotFoundView'; | ||
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView'; | ||
import FullscreenLoadingIndicator from '@components/FullscreenLoadingIndicator'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import * as PolicyUtils from '@libs/PolicyUtils'; | ||
import NotFoundPage from '@pages/ErrorPage/NotFoundPage'; | ||
import * as Policy from '@userActions/Policy'; | ||
import CONST from '@src/CONST'; | ||
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 = { | ||
[CONST.POLICY.ACCESS_VARIANTS.PAID]: (policy: OnyxEntry<OnyxTypes.Policy>) => PolicyUtils.isPaidGroupPolicy(policy) && !!policy?.isPolicyExpenseChatEnabled, | ||
[CONST.POLICY.ACCESS_VARIANTS.ADMIN]: (policy: OnyxEntry<OnyxTypes.Policy>) => PolicyUtils.isPolicyAdmin(policy), | ||
} as const satisfies Record<string, (policy: OnyxTypes.Policy) => boolean>; | ||
|
||
type PolicyAccessVariant = keyof typeof POLICY_ACCESS_VARIANTS; | ||
type AccessOrNotFoundWrapperOnyxProps = { | ||
/** The report currently being looked at */ | ||
policy: OnyxEntry<OnyxTypes.Policy>; | ||
|
||
/** Indicated whether the report data is loading */ | ||
isLoadingReportData: OnyxEntry<boolean>; | ||
}; | ||
|
||
type AccessOrNotFoundWrapperProps = AccessOrNotFoundWrapperOnyxProps & { | ||
/** The children to render */ | ||
children: ((props: AccessOrNotFoundWrapperOnyxProps) => React.ReactNode) | React.ReactNode; | ||
|
||
/** The report currently being looked at */ | ||
policyID: string; | ||
|
||
/** Defines which types of access should be verified */ | ||
accessVariants?: PolicyAccessVariant[]; | ||
|
||
/** The current feature name that the user tries to get access to */ | ||
featureName?: PolicyFeatureName; | ||
|
||
/** Props for customizing fallback pages */ | ||
fullPageNotFoundViewProps?: FullPageNotFoundViewProps; | ||
} & Pick<FullPageNotFoundViewProps, 'subtitleKey' | 'onLinkPress'>; | ||
|
||
type PageNotFoundFallbackProps = Pick<AccessOrNotFoundWrapperProps, 'policyID' | 'fullPageNotFoundViewProps'> & {shouldShowFullScreenFallback: boolean}; | ||
|
||
function PageNotFoundFallback({policyID, shouldShowFullScreenFallback, fullPageNotFoundViewProps}: PageNotFoundFallbackProps) { | ||
return shouldShowFullScreenFallback ? ( | ||
<FullPageNotFoundView | ||
shouldShow | ||
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)} | ||
shouldForceFullScreen | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...fullPageNotFoundViewProps} | ||
/> | ||
) : ( | ||
<NotFoundPage | ||
onBackButtonPress={() => Navigation.goBack(ROUTES.WORKSPACE_PROFILE.getRoute(policyID))} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...fullPageNotFoundViewProps} | ||
/> | ||
); | ||
} | ||
|
||
function AccessOrNotFoundWrapper({accessVariants = [], fullPageNotFoundViewProps, ...props}: AccessOrNotFoundWrapperProps) { | ||
const {policy, policyID, featureName, isLoadingReportData} = props; | ||
|
||
const isPolicyIDInRoute = !!policyID?.length; | ||
|
||
useEffect(() => { | ||
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(policyID, []); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [isPolicyIDInRoute, policyID]); | ||
|
||
const shouldShowFullScreenLoadingIndicator = isLoadingReportData !== false && (!Object.entries(policy ?? {}).length || !policy?.id); | ||
|
||
const isFeatureEnabled = featureName ? PolicyUtils.isPolicyFeatureEnabled(policy, featureName) : true; | ||
|
||
const isPageAccessible = accessVariants.reduce((acc, variant) => { | ||
const accessFunction = POLICY_ACCESS_VARIANTS[variant]; | ||
return acc && accessFunction(policy); | ||
}, true); | ||
|
||
const shouldShowNotFoundPage = isEmptyObject(policy) || (Object.keys(policy).length === 1 && !isEmptyObject(policy.errors)) || !policy?.id || !isPageAccessible || !isFeatureEnabled; | ||
|
||
if (shouldShowFullScreenLoadingIndicator) { | ||
return <FullscreenLoadingIndicator />; | ||
} | ||
|
||
if (shouldShowNotFoundPage) { | ||
return ( | ||
<PageNotFoundFallback | ||
policyID={policyID} | ||
shouldShowFullScreenFallback={!isFeatureEnabled} | ||
fullPageNotFoundViewProps={fullPageNotFoundViewProps} | ||
/> | ||
); | ||
} | ||
|
||
return callOrReturn(props.children, props); | ||
} | ||
|
||
export default withOnyx<AccessOrNotFoundWrapperProps, AccessOrNotFoundWrapperOnyxProps>({ | ||
policy: { | ||
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY}${policyID ?? ''}`, | ||
}, | ||
isLoadingReportData: { | ||
key: ONYXKEYS.IS_LOADING_REPORT_DATA, | ||
}, | ||
})(AccessOrNotFoundWrapper); |
91 changes: 0 additions & 91 deletions
91
src/pages/workspace/AdminPolicyAccessOrNotFoundWrapper.tsx
This file was deleted.
Oops, something went wrong.
91 changes: 0 additions & 91 deletions
91
src/pages/workspace/FeatureEnabledAccessOrNotFoundWrapper.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming from #44206:
Please be careful when fix conflict.
Recent code in
FeatureEnabledAccessOrWrapper
was not applied to this file so the fix of #39443 was partially reverted.More details: #44206 (comment)