Skip to content

Commit

Permalink
update: callback function to hook.
Browse files Browse the repository at this point in the history
Signed-off-by: Krishna Gupta <belivethatkg@gmail.com>
  • Loading branch information
Krishna2323 committed Jan 31, 2024
1 parent 40b30f0 commit 5b42bdf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
22 changes: 22 additions & 0 deletions src/hooks/useSearchTermAndSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {Dispatch} from 'react';
import {useCallback} from 'react';
import * as Report from '@userActions/Report';

/**
* Hook for fetching reports when user updated search term and hasn't selected max number of participants
*/
const useSearchTermAndSearch = (setSearchTerm: Dispatch<React.SetStateAction<string>>, maxParticipantsReached: boolean) => {
const setSearchTermAndSearchInServer = useCallback(
(text = '') => {
if (text && !maxParticipantsReached) {
Report.searchInServer(text);
}
setSearchTerm(text);
},
[maxParticipantsReached, setSearchTerm],
);

return setSearchTermAndSearchInServer;
};

export default useSearchTermAndSearch;
14 changes: 3 additions & 11 deletions src/pages/NewChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import withWindowDimensions, {windowDimensionsPropTypes} from '@components/withWindowDimensions';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useNetwork from '@hooks/useNetwork';
import useSearchTermAndSearch from '@hooks/useSearchTermAndSearch';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import compose from '@libs/compose';
Expand Down Expand Up @@ -64,6 +65,8 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);

const maxParticipantsReached = selectedOptions.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const setSearchTermAndSearchInServer = useSearchTermAndSearch(setSearchTerm, maxParticipantsReached);

const headerMessage = OptionsListUtils.getHeaderMessage(
filteredPersonalDetails.length + filteredRecentReports.length !== 0,
Boolean(filteredUserToInvite),
Expand Down Expand Up @@ -229,17 +232,6 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
updateOptions();
}, [didScreenTransitionEnd, updateOptions]);

// When search term updates & user hasn't selected max number of participants we will fetch any reports
const setSearchTermAndSearchInServer = useCallback(
(text = '') => {
if (text && !maxParticipantsReached) {
Report.searchInServer(text);
}
setSearchTerm(text);
},
[maxParticipantsReached],
);

const {inputCallbackRef} = useAutoFocusInput();

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import SelectCircle from '@components/SelectCircle';
import SelectionList from '@components/SelectionList';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useSearchTermAndSearch from '@hooks/useSearchTermAndSearch';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Report from '@libs/actions/Report';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
Expand Down Expand Up @@ -88,6 +88,7 @@ function MoneyTemporaryForRefactorRequestParticipantsSelector({
const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';

const maxParticipantsReached = participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const setSearchTermAndSearchInServer = useSearchTermAndSearch(setSearchTerm, maxParticipantsReached);

/**
* Returns the sections needed for the OptionsSelector
Expand Down Expand Up @@ -244,17 +245,6 @@ function MoneyTemporaryForRefactorRequestParticipantsSelector({
[maxParticipantsReached, newChatOptions, participants, searchTerm],
);

// When search term updates & user hasn't selected max number of participants we will fetch any reports
const setSearchTermAndSearchInServer = useCallback(
(text = '') => {
if (text && !maxParticipantsReached) {
Report.searchInServer(text);
}
setSearchTerm(text);
},
[maxParticipantsReached],
);

// Right now you can't split a request with a workspace and other additional participants
// This is getting properly fixed in https://github.com/Expensify/App/issues/27508, but as a stop-gap to prevent
// the app from crashing on native when you try to do this, we'll going to hide the button if you have a workspace and other participants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import SelectCircle from '@components/SelectCircle';
import SelectionList from '@components/SelectionList';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useSearchTermAndSearch from '@hooks/useSearchTermAndSearch';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Report from '@libs/actions/Report';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import reportPropTypes from '@pages/reportPropTypes';
Expand Down Expand Up @@ -89,6 +89,9 @@ function MoneyRequestParticipantsSelector({
const {isOffline} = useNetwork();
const personalDetails = usePersonalDetails();

const maxParticipantsReached = participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;
const setSearchTermAndSearchInServer = useSearchTermAndSearch(setSearchTerm, maxParticipantsReached);

const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';

const newChatOptions = useMemo(() => {
Expand Down Expand Up @@ -124,8 +127,6 @@ function MoneyRequestParticipantsSelector({
};
}, [betas, reports, participants, personalDetails, searchTerm, iouType, isDistanceRequest]);

const maxParticipantsReached = participants.length === CONST.REPORT.MAXIMUM_PARTICIPANTS;

/**
* Returns the sections needed for the OptionsSelector
*
Expand Down Expand Up @@ -260,17 +261,6 @@ function MoneyRequestParticipantsSelector({
[maxParticipantsReached, newChatOptions.personalDetails.length, newChatOptions.recentReports.length, newChatOptions.userToInvite, participants, searchTerm],
);

// When search term updates & user hasn't selected max number of participants we will fetch any reports
const setSearchTermAndSearchInServer = useCallback(
(text = '') => {
if (text && !maxParticipantsReached) {
Report.searchInServer(text);
}
setSearchTerm(text);
},
[maxParticipantsReached],
);

// Right now you can't split a request with a workspace and other additional participants
// This is getting properly fixed in https://github.com/Expensify/App/issues/27508, but as a stop-gap to prevent
// the app from crashing on native when you try to do this, we'll going to show error message if you have a workspace and other participants
Expand Down

0 comments on commit 5b42bdf

Please sign in to comment.