Skip to content

Commit 63111c7

Browse files
authored
Merge pull request Expensify#63103 from callstack-internal/pac-guerreiro/issue/62882-change-function-to-be-pure-where-taskReport-is-passed-as-a-parameter
Refactor completeTestDriveTask() to be pure
2 parents ae43503 + c0ee5cf commit 63111c7

6 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/components/TestDrive/TestDriveDemo.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ function TestDriveDemo() {
2424
const styles = useThemeStyles();
2525
const [onboarding] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {canBeMissing: false});
2626
const [onboardingReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${onboarding?.chatReportID}`, {canBeMissing: true});
27+
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: false});
28+
const viewTourReportID = introSelected?.viewTour;
29+
const [viewTourReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${viewTourReportID}`, {canBeMissing: true});
2730

2831
useEffect(() => {
2932
InteractionManager.runAfterInteractions(() => {
3033
setIsVisible(true);
31-
completeTestDriveTask();
34+
completeTestDriveTask(viewTourReport, viewTourReportID);
3235
});
36+
37+
// This should fire only during mount.
38+
// eslint-disable-next-line react-compiler/react-compiler
39+
// eslint-disable-next-line react-hooks/exhaustive-deps
3340
}, []);
3441

3542
const closeModal = useCallback(() => {

src/libs/actions/Task.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,11 +1318,9 @@ function getFinishOnboardingTaskOnyxData(taskName: IntroSelectedTask): OnyxData
13181318

13191319
return {};
13201320
}
1321-
function completeTestDriveTask(shouldUpdateSelfTourViewedOnlyLocally = false) {
1322-
const taskReportID = introSelected?.viewTour;
1323-
const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`];
1321+
function completeTestDriveTask(viewTourReport: OnyxEntry<OnyxTypes.Report>, viewTourReportID: string | undefined, shouldUpdateSelfTourViewedOnlyLocally = false) {
13241322
setSelfTourViewed(shouldUpdateSelfTourViewedOnlyLocally);
1325-
completeTask(taskReport, taskReportID);
1323+
completeTask(viewTourReport, viewTourReportID);
13261324
}
13271325

13281326
export {

src/pages/Search/EmptySearchView.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ function EmptySearchView({hash, type, groupBy, hasResults}: EmptySearchViewProps
165165
selector: hasSeenTourSelector,
166166
canBeMissing: true,
167167
});
168+
const viewTourReportID = introSelected?.viewTour;
169+
const [viewTourReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${viewTourReportID}`, {canBeMissing: true});
168170

169171
const content: EmptySearchViewItem = useMemo(() => {
170172
// Begin by going through all of our To-do searches, and returning their empty state
@@ -190,7 +192,7 @@ function EmptySearchView({hash, type, groupBy, hasResults}: EmptySearchViewProps
190192
const startTestDrive = () => {
191193
InteractionManager.runAfterInteractions(() => {
192194
if (introSelected?.choice === CONST.ONBOARDING_CHOICES.MANAGE_TEAM || introSelected?.choice === CONST.ONBOARDING_CHOICES.TEST_DRIVE_RECEIVER) {
193-
completeTestDriveTask();
195+
completeTestDriveTask(viewTourReport, viewTourReportID);
194196
Navigation.navigate(ROUTES.TEST_DRIVE_DEMO_ROOT);
195197
} else {
196198
Navigation.navigate(ROUTES.TEST_DRIVE_MODAL_ROOT.route);
@@ -368,6 +370,8 @@ function EmptySearchView({hash, type, groupBy, hasResults}: EmptySearchViewProps
368370
tripViewChildren,
369371
hasResults,
370372
shouldRedirectToExpensifyClassic,
373+
viewTourReport,
374+
viewTourReportID,
371375
]);
372376

373377
return (

src/pages/home/sidebar/FloatingActionButtonAndPopover.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT
131131
selector: hasSeenTourSelector,
132132
canBeMissing: true,
133133
});
134+
const viewTourReportID = introSelected?.viewTour;
135+
const [viewTourReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${viewTourReportID}`, {canBeMissing: true});
134136

135137
const {setRootStatusBarEnabled} = useContext(CustomStatusBarAndBackgroundContext);
136138

@@ -489,7 +491,7 @@ function FloatingActionButtonAndPopover({onHideCreateMenu, onShowCreateMenu, isT
489491
onSelected: () => {
490492
InteractionManager.runAfterInteractions(() => {
491493
if (introSelected?.choice === CONST.ONBOARDING_CHOICES.MANAGE_TEAM || introSelected?.choice === CONST.ONBOARDING_CHOICES.TEST_DRIVE_RECEIVER) {
492-
completeTestDriveTask(isAnonymousUser());
494+
completeTestDriveTask(viewTourReport, viewTourReportID, isAnonymousUser());
493495
Navigation.navigate(ROUTES.TEST_DRIVE_DEMO_ROOT);
494496
} else {
495497
Navigation.navigate(ROUTES.TEST_DRIVE_MODAL_ROOT.route);

src/pages/iou/request/step/IOURequestStepConfirmation.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ function IOURequestStepConfirmation({
185185
// TODO: remove beta check after the feature is enabled
186186
const {isBetaEnabled} = usePermissions();
187187

188+
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: false});
189+
const viewTourReportID = introSelected?.viewTour;
190+
const [viewTourReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${viewTourReportID}`, {canBeMissing: true});
191+
188192
const headerTitle = useMemo(() => {
189193
if (isCategorizingTrackExpense) {
190194
return translate('iou.categorize');
@@ -478,7 +482,7 @@ function IOURequestStepConfirmation({
478482
const isTestDriveReceipt = receipt?.isTestDriveReceipt ?? false;
479483

480484
if (isTestDriveReceipt) {
481-
completeTestDriveTask();
485+
completeTestDriveTask(viewTourReport, viewTourReportID);
482486
}
483487

484488
requestMoneyIOUActions({
@@ -534,6 +538,8 @@ function IOURequestStepConfirmation({
534538
transactionTaxAmount,
535539
customUnitRateID,
536540
backToReport,
541+
viewTourReport,
542+
viewTourReportID,
537543
],
538544
);
539545

tests/actions/TaskTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ describe('actions/Task', () => {
224224
it('Completes test drive task', () => {
225225
const writeSpy = jest.spyOn(API, 'write');
226226

227-
completeTestDriveTask();
227+
completeTestDriveTask(testDriveTaskReport, testDriveTaskReport.reportID);
228228

229229
expect(writeSpy).toHaveBeenCalledWith(WRITE_COMMANDS.COMPLETE_TASK, expect.anything(), expect.anything());
230230
});

0 commit comments

Comments
 (0)