Skip to content

Commit

Permalink
Merge pull request #39811 from callstack-internal/fix/update-options-…
Browse files Browse the repository at this point in the history
…when-creating-task

Fix: update options when creating task
  • Loading branch information
mountiny authored Apr 10, 2024
2 parents ed6f1a7 + 143b1a4 commit 536f71a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/components/OptionListContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react';
import {withOnyx} from 'react-native-onyx';
import type {OnyxCollection} from 'react-native-onyx';
import usePrevious from '@hooks/usePrevious';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import type {OptionList} from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -42,8 +43,13 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp
reports: [],
personalDetails: [],
});

const personalDetails = usePersonalDetails();
const prevReports = usePrevious(reports);

/**
* This effect is used to update the options list when a report is updated.
*/
useEffect(() => {
// there is no need to update the options if the options are not initialized
if (!areOptionsInitialized.current) {
Expand Down Expand Up @@ -71,6 +77,31 @@ function OptionsListContextProvider({reports, children}: OptionsListProviderProp
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reports]);

/**
* This effect is used to add a new report option to the list of options when a new report is added to the collection.
*/
useEffect(() => {
if (!areOptionsInitialized.current || !reports) {
return;
}
const missingReportId = Object.keys(reports).find((key) => prevReports && !(key in prevReports));
const report = missingReportId ? reports[missingReportId] : null;
if (!missingReportId || !report) {
return;
}

const reportOption = OptionsListUtils.createOptionFromReport(report, personalDetails);
setOptions((prevOptions) => {
const newOptions = {...prevOptions};
newOptions.reports.push(reportOption);
return newOptions;
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [reports]);

/**
* This effect is used to update the options list when personal details change.
*/
useEffect(() => {
// there is no need to update the options if the options are not initialized
if (!areOptionsInitialized.current) {
Expand Down

0 comments on commit 536f71a

Please sign in to comment.