Skip to content

Commit

Permalink
Merge pull request #49592 from fabioh8010/feature/combined-expense-fl…
Browse files Browse the repository at this point in the history
…ow-48787-withOnyx-migration

[A/B Testing][ESLint Clean Up] Use useOnyx cases for files in combinedTrackSubmit
  • Loading branch information
grgia authored Oct 21, 2024
2 parents 893ef36 + b735a36 commit 030f67c
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function AttachmentPickerWithMenuItems({
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {isDelegateAccessRestricted, delegatorEmail} = useDelegateUserDetails();
const [isNoDelegateAccessMenuVisible, setIsNoDelegateAccessMenuVisible] = useState(false);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '-1'}`, {initialValue: {} as OnyxTypes.Policy});
const {canUseCombinedTrackSubmit} = usePermissions();

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {useIsFocused as useIsFocusedOriginal, useNavigationState} from '@react-navigation/native';
import type {ImageContentFit} from 'expo-image';
import type {ForwardedRef, RefAttributes} from 'react';
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import type {SvgProps} from 'react-native-svg';
import FloatingActionButton from '@components/FloatingActionButton';
import * as Expensicons from '@components/Icon/Expensicons';
Expand Down Expand Up @@ -39,6 +39,7 @@ import SCREENS from '@src/SCREENS';
import type * as OnyxTypes from '@src/types/onyx';
import type {QuickActionName} from '@src/types/onyx/QuickAction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems';

// On small screen we hide the search page from central pane to show the search bottom tab page with bottom tab bar.
// We need to take this in consideration when checking if the screen is focused.
Expand All @@ -51,33 +52,7 @@ const useIsFocused = () => {

type PolicySelector = Pick<OnyxTypes.Policy, 'type' | 'role' | 'isPolicyExpenseChatEnabled' | 'pendingAction' | 'avatarURL' | 'name' | 'id' | 'areInvoicesEnabled'>;

type FloatingActionButtonAndPopoverOnyxProps = {
/** The list of policies the user has access to. */
allPolicies: OnyxCollection<PolicySelector>;

/** Whether app is in loading state */
isLoading: OnyxEntry<boolean>;

/** Information on the last taken action to display as Quick Action */
quickAction: OnyxEntry<OnyxTypes.QuickAction>;

/** The report data of the quick action */
quickActionReport: OnyxEntry<OnyxTypes.Report>;

/** The policy data of the quick action */
quickActionPolicy: OnyxEntry<OnyxTypes.Policy>;

/** The current session */
session: OnyxEntry<OnyxTypes.Session>;

/** Personal details of all the users */
personalDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>;

/** Has user seen track expense training interstitial */
hasSeenTrackTraining: OnyxEntry<boolean>;
};

type FloatingActionButtonAndPopoverProps = FloatingActionButtonAndPopoverOnyxProps & {
type FloatingActionButtonAndPopoverProps = {
/* Callback function when the menu is shown */
onShowCreateMenu?: () => void;

Expand Down Expand Up @@ -161,21 +136,16 @@ const getQuickActionTitle = (action: QuickActionName): TranslationPaths => {
* Responsible for rendering the {@link PopoverMenu}, and the accompanying
* FAB that can open or close the menu.
*/
function FloatingActionButtonAndPopover(
{
onHideCreateMenu,
onShowCreateMenu,
isLoading = false,
allPolicies,
quickAction,
quickActionReport,
quickActionPolicy,
session,
personalDetails,
hasSeenTrackTraining,
}: FloatingActionButtonAndPopoverProps,
ref: ForwardedRef<FloatingActionButtonAndPopoverRef>,
) {
function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu}: FloatingActionButtonAndPopoverProps, ref: ForwardedRef<FloatingActionButtonAndPopoverRef>) {
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (c) => mapOnyxCollectionItems(c, policySelector)});
const [isLoading = false] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const [quickAction] = useOnyx(ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE);
const [quickActionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${quickAction?.chatReportID ?? '-1'}`);
const [quickActionPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${quickActionReport?.policyID ?? '-1'}`);
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [session] = useOnyx(ONYXKEYS.SESSION);
const [hasSeenTrackTraining] = useOnyx(ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING);

const styles = useThemeStyles();
const {translate} = useLocalize();
const [reportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${quickActionReport?.reportID ?? -1}`);
Expand Down Expand Up @@ -510,32 +480,6 @@ function FloatingActionButtonAndPopover(

FloatingActionButtonAndPopover.displayName = 'FloatingActionButtonAndPopover';

export default withOnyx<FloatingActionButtonAndPopoverProps & RefAttributes<FloatingActionButtonAndPopoverRef>, FloatingActionButtonAndPopoverOnyxProps>({
allPolicies: {
key: ONYXKEYS.COLLECTION.POLICY,
selector: policySelector,
},
isLoading: {
key: ONYXKEYS.IS_LOADING_APP,
},
quickAction: {
key: ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE,
},
quickActionReport: {
key: ({quickAction}) => `${ONYXKEYS.COLLECTION.REPORT}${quickAction?.chatReportID}`,
},
quickActionPolicy: {
key: ({quickActionReport}) => `${ONYXKEYS.COLLECTION.POLICY}${quickActionReport?.policyID}`,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
hasSeenTrackTraining: {
key: ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING,
},
})(forwardRef(FloatingActionButtonAndPopover));
export default forwardRef(FloatingActionButtonAndPopover);

export type {PolicySelector};
62 changes: 9 additions & 53 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useFocusEffect} from '@react-navigation/native';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
Expand Down Expand Up @@ -32,25 +32,7 @@ type AmountParams = {
paymentMethod?: PaymentMethodType;
};

type IOURequestStepAmountOnyxProps = {
/** The draft transaction that holds data to be persisted on the current transaction */
splitDraftTransaction: OnyxEntry<OnyxTypes.Transaction>;

/** Whether the confirmation step should be skipped */
skipConfirmation: OnyxEntry<boolean>;

/** The draft transaction object being modified in Onyx */
draftTransaction: OnyxEntry<OnyxTypes.Transaction>;

/** Personal details of all users */
personalDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>;

/** The policy which the user has access to and which the report is tied to */
policy: OnyxEntry<OnyxTypes.Policy>;
};

type IOURequestStepAmountProps = IOURequestStepAmountOnyxProps &
WithCurrentUserPersonalDetailsProps &
type IOURequestStepAmountProps = WithCurrentUserPersonalDetailsProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_AMOUNT | typeof SCREENS.MONEY_REQUEST.CREATE> & {
/** The transaction object being modified in Onyx */
transaction: OnyxEntry<OnyxTypes.Transaction>;
Expand All @@ -65,14 +47,15 @@ function IOURequestStepAmount({
params: {iouType, reportID, transactionID, backTo, pageIndex, action, currency: selectedCurrency = ''},
},
transaction,
policy,
personalDetails,
currentUserPersonalDetails,
splitDraftTransaction,
skipConfirmation,
draftTransaction,
shouldKeepUserInput = false,
}: IOURequestStepAmountProps) {
const [splitDraftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID ?? '-1'}`);
const [draftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID ?? '0'}`);
const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID ?? '-1'}`);
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID ?? '-1'}`);

const {translate} = useLocalize();
const textInput = useRef<BaseTextInputRef | null>(null);
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
Expand Down Expand Up @@ -331,34 +314,7 @@ function IOURequestStepAmount({

IOURequestStepAmount.displayName = 'IOURequestStepAmount';

const IOURequestStepAmountWithOnyx = withOnyx<IOURequestStepAmountProps, IOURequestStepAmountOnyxProps>({
splitDraftTransaction: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? -1;
return `${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`;
},
},
draftTransaction: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? 0;
return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`;
},
},
skipConfirmation: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? -1;
return `${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`;
},
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
policy: {
key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '-1'}`,
},
})(IOURequestStepAmount);

const IOURequestStepAmountWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepAmountWithOnyx);
const IOURequestStepAmountWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepAmount);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepAmountWithCurrentUserPersonalDetails, true);
// eslint-disable-next-line rulesdir/no-negated-variables
Expand Down
4 changes: 1 addition & 3 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,7 @@ function IOURequestStepDistance({

IOURequestStepDistance.displayName = 'IOURequestStepDistance';

const IOURequestStepDistanceWithOnyx = IOURequestStepDistance;

const IOURequestStepDistanceWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepDistanceWithOnyx);
const IOURequestStepDistanceWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepDistance);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepDistanceWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepDistanceWithCurrentUserPersonalDetails, true);
// eslint-disable-next-line rulesdir/no-negated-variables
Expand Down
25 changes: 5 additions & 20 deletions src/pages/iou/request/step/IOURequestStepParticipants.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {useIsFocused} from '@react-navigation/core';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import FormHelpMessage from '@components/FormHelpMessage';
import useLocalize from '@hooks/useLocalize';
import usePermissions from '@hooks/usePermissions';
Expand All @@ -26,22 +25,17 @@ import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound';
import withWritableReportOrNotFound from './withWritableReportOrNotFound';

type IOURequestStepParticipantsOnyxProps = {
/** Whether the confirmation step should be skipped */
skipConfirmation: OnyxEntry<boolean>;
};

type IOURequestStepParticipantsProps = IOURequestStepParticipantsOnyxProps &
WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_PARTICIPANTS> &
type IOURequestStepParticipantsProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_PARTICIPANTS> &
WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_PARTICIPANTS>;

function IOURequestStepParticipants({
route: {
params: {iouType, reportID, transactionID, action},
},
transaction,
skipConfirmation,
}: IOURequestStepParticipantsProps) {
const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID ?? '-1'}`);

const participants = transaction?.participants;
const {translate} = useLocalize();
const styles = useThemeStyles();
Expand Down Expand Up @@ -207,13 +201,4 @@ function IOURequestStepParticipants({

IOURequestStepParticipants.displayName = 'IOURequestStepParticipants';

const IOURequestStepParticipantsWithOnyx = withOnyx<IOURequestStepParticipantsProps, IOURequestStepParticipantsOnyxProps>({
skipConfirmation: {
key: ({route}) => {
const transactionID = route.params.transactionID ?? -1;
return `${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`;
},
},
})(IOURequestStepParticipants);

export default withWritableReportOrNotFound(withFullTransactionOrNotFound(IOURequestStepParticipantsWithOnyx));
export default withWritableReportOrNotFound(withFullTransactionOrNotFound(IOURequestStepParticipants));
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,7 @@ function IOURequestStepScan({

IOURequestStepScan.displayName = 'IOURequestStepScan';

const IOURequestStepScanWithOnyx = IOURequestStepScan;

const IOURequestStepScanWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepScanWithOnyx);
const IOURequestStepScanWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepScan);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepScanWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepScanWithCurrentUserPersonalDetails, true);
// eslint-disable-next-line rulesdir/no-negated-variables
Expand Down
4 changes: 1 addition & 3 deletions src/pages/iou/request/step/IOURequestStepScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,7 @@ function IOURequestStepScan({

IOURequestStepScan.displayName = 'IOURequestStepScan';

const IOURequestStepScanWithOnyx = IOURequestStepScan;

const IOURequestStepScanWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepScanWithOnyx);
const IOURequestStepScanWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepScan);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepScanWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepScanWithCurrentUserPersonalDetails, true);
// eslint-disable-next-line rulesdir/no-negated-variables
Expand Down
11 changes: 7 additions & 4 deletions src/pages/workspace/AccessOrNotFoundWrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable rulesdir/no-negated-variables */
import React, {useEffect, useState} from 'react';
import React, {useEffect, useMemo, useState} from 'react';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import type {FullPageNotFoundViewProps} from '@components/BlockingViews/FullPageNotFoundView';
Expand Down Expand Up @@ -44,6 +44,7 @@ const ACCESS_VARIANTS = {
>;

type AccessVariant = keyof typeof ACCESS_VARIANTS;

type AccessOrNotFoundWrapperChildrenProps = {
/** The report that holds the transaction */
report: OnyxEntry<OnyxTypes.Report>;
Expand Down Expand Up @@ -116,8 +117,8 @@ function AccessOrNotFoundWrapper({
featureName,
...props
}: AccessOrNotFoundWrapperProps) {
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`);
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID ?? '-1'}`);
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID ?? '-1'}`);
const [isLoadingReportData] = useOnyx(ONYXKEYS.IS_LOADING_REPORT_DATA, {initialValue: true});
const {login = ''} = useCurrentUserPersonalDetails();
const isPolicyIDInRoute = !!policyID?.length;
Expand Down Expand Up @@ -160,6 +161,8 @@ function AccessOrNotFoundWrapper({
setIsPolicyFeatureEnabled(isFeatureEnabled);
}, [pendingField, isOffline, isFeatureEnabled]);

const childrenProps = useMemo(() => ({report, policy, isLoadingReportData}), [report, policy, isLoadingReportData]);

if (shouldShowFullScreenLoadingIndicator) {
return <FullscreenLoadingIndicator />;
}
Expand All @@ -175,7 +178,7 @@ function AccessOrNotFoundWrapper({
);
}

return callOrReturn(props.children, {report, policy, isLoadingReportData});
return callOrReturn(props.children, childrenProps);
}

export type {AccessVariant};
Expand Down

0 comments on commit 030f67c

Please sign in to comment.