-
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
Changes from 4 commits
4d15239
5829f66
6a18afb
a5d5822
26aed7a
02e1191
d9a2b22
ae7b35e
54cf1f5
572e0f6
e72d7bf
199c918
dd27831
8fc1426
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,109 @@ | ||||||
/* 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 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 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 = { | ||||||
PAID: (policy: OnyxEntry<OnyxTypes.Policy>) => !PolicyUtils.isPaidGroupPolicy(policy) || !policy?.isPolicyExpenseChatEnabled, | ||||||
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; | ||||||
}; | ||||||
|
||||||
type PageNotFoundFallackProps = Pick<AccessOrNotFoundWrapperProps, 'policyID'> & {showFullScreenFallback: boolean}; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
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))} /> | ||||||
); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think no need for using both <FullPageNotFoundView
shouldShow
onBackButtonPress={() => Navigation.goBack(!isFeatureEnabled ? ROUTES.SETTINGS_WORKSPACES : ROUTES.WORKSPACE_PROFILE.getRoute(policyID))}
shouldForceFullScreen={!isFeatureEnabled}
/> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can also use it inline and remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
|
||||||
function AccessOrNotFoundWrapper({accessVariants = [], ...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 pageUnaccessible = accessVariants.reduce((acc, variant) => { | ||||||
const accessFunction = POLICY_ACCESS_VARIANTS[variant]; | ||||||
return acc || accessFunction(policy); | ||||||
}, false); | ||||||
|
||||||
const shouldShowNotFoundPage = isEmptyObject(policy) || (Object.keys(policy).length === 1 && !isEmptyObject(policy.errors)) || !policy?.id || pageUnaccessible || !isFeatureEnabled; | ||||||
|
||||||
if (shouldShowFullScreenLoadingIndicator) { | ||||||
return <FullscreenLoadingIndicator />; | ||||||
} | ||||||
|
||||||
if (shouldShowNotFoundPage) { | ||||||
return ( | ||||||
<PageNotFoundFallback | ||||||
policyID={policyID} | ||||||
showFullScreenFallback={!isFeatureEnabled} | ||||||
/> | ||||||
); | ||||||
} | ||||||
|
||||||
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); |
This file was deleted.
This file was deleted.
This file was deleted.
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.
The code here is working well, but I think we need to make it more clean, because while the const name is
ACCESS
, the return method return true whenINACCESS
not whenACCESS
, so I suggest the following change.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.
Also, I think using accessVariant name as a const (
CONST.POLICY.ACCESS_VARIANTS.PAID
) will be more safe than string ('PAID'
)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.
POLICY_ACCESS_VARIANTS
is being used as the type source which would be exported to the other places. I'd suggest keeping it this way until we find a case for usingPOLICY_ACCESS_VARIANTS
outside of this component and then move it as a wholeThere 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.
@BrtqKr But it used in many places where
AccessOrNotFoundWrapper
is used as a prop of it, so I think we need to update it.Also, this point is a Reviewer Checklist item