-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Create amount filter #47343
Merged
luacmartins
merged 14 commits into
Expensify:main
from
software-mansion-labs:@szymczak/amount-filter
Aug 21, 2024
Merged
Create amount filter #47343
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
449f22a
add AmountWithoutCurrencyForm component
SzymczakJ afa7e1c
add amount filter page
SzymczakJ 4885ee5
fix greaterThan and lessThan values
SzymczakJ 1ca1423
add build query from string logic
SzymczakJ 3ceeb4a
fix bug in buildQueryString
SzymczakJ e231cc8
fix bug
SzymczakJ 8cdfe96
review suggestions
289Adam289 d568ed6
fix focus bug
289Adam289 7adcd4b
Merge branch 'main' into @szymczak/amount-filter
289Adam289 eed752a
fix merge
289Adam289 a7cbfe5
review suggestions
289Adam289 070731e
add leading zero to comma
289Adam289 52ab72e
remove commas from input
289Adam289 c205d92
fix removing value
289Adam289 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, {useCallback, useMemo} from 'react'; | ||
import type {ForwardedRef} from 'react'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import {addLeadingZero, replaceAllDigits, replaceCommasWithPeriod, stripSpacesFromAmount, validateAmount} from '@libs/MoneyRequestUtils'; | ||
import CONST from '@src/CONST'; | ||
import TextInput from './TextInput'; | ||
import type {BaseTextInputProps, BaseTextInputRef} from './TextInput/BaseTextInput/types'; | ||
|
||
type AmountFormProps = { | ||
/** Amount supplied by the FormProvider */ | ||
value?: string; | ||
|
||
/** Callback to update the amount in the FormProvider */ | ||
onInputChange?: (value: string) => void; | ||
} & Partial<BaseTextInputProps>; | ||
|
||
function AmountWithoutCurrencyForm( | ||
{value: amount, onInputChange, inputID, name, defaultValue, accessibilityLabel, role, label, ...rest}: AmountFormProps, | ||
ref: ForwardedRef<BaseTextInputRef>, | ||
) { | ||
const {toLocaleDigit} = useLocalize(); | ||
|
||
const currentAmount = useMemo(() => (typeof amount === 'string' ? amount : ''), [amount]); | ||
|
||
/** | ||
* Sets the selection and the amount accordingly to the value passed to the input | ||
* @param newAmount - Changed amount from user input | ||
*/ | ||
const setNewAmount = useCallback( | ||
(newAmount: string) => { | ||
// Remove spaces from the newAmount value because Safari on iOS adds spaces when pasting a copied value | ||
// More info: https://github.com/Expensify/App/issues/16974 | ||
const newAmountWithoutSpaces = stripSpacesFromAmount(newAmount); | ||
const replacedCommasAmount = replaceCommasWithPeriod(newAmountWithoutSpaces); | ||
const withLeadingZero = addLeadingZero(replacedCommasAmount); | ||
if (!validateAmount(withLeadingZero, 2)) { | ||
return; | ||
} | ||
onInputChange?.(withLeadingZero); | ||
}, | ||
[onInputChange], | ||
); | ||
|
||
const formattedAmount = replaceAllDigits(currentAmount, toLocaleDigit); | ||
|
||
return ( | ||
<TextInput | ||
value={formattedAmount} | ||
onChangeText={setNewAmount} | ||
inputID={inputID} | ||
name={name} | ||
label={label} | ||
defaultValue={defaultValue} | ||
accessibilityLabel={accessibilityLabel} | ||
role={role} | ||
ref={ref} | ||
keyboardType={CONST.KEYBOARD_TYPE.DECIMAL_PAD} | ||
// eslint-disable-next-line react/jsx-props-no-spreading | ||
{...rest} | ||
/> | ||
); | ||
} | ||
|
||
AmountWithoutCurrencyForm.displayName = 'AmountWithoutCurrencyForm'; | ||
|
||
export default React.forwardRef(AmountWithoutCurrencyForm); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React from 'react'; | ||
import {View} from 'react-native'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import AmountWithoutCurrencyForm from '@components/AmountWithoutCurrencyForm'; | ||
import FormProvider from '@components/Form/FormProvider'; | ||
import InputWrapper from '@components/Form/InputWrapper'; | ||
import type {FormOnyxValues} from '@components/Form/types'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import useAutoFocusInput from '@hooks/useAutoFocusInput'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import {updateAdvancedFilters} from '@libs/actions/Search'; | ||
import {convertToBackendAmount, convertToFrontendAmountAsString} from '@libs/CurrencyUtils'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
import INPUT_IDS from '@src/types/form/SearchAdvancedFiltersForm'; | ||
|
||
function SearchFiltersAmountPage() { | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
|
||
const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM); | ||
const greaterThan = searchAdvancedFiltersForm?.[INPUT_IDS.GREATER_THAN]; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
const greaterThanFormattedAmount = greaterThan ? convertToFrontendAmountAsString(Number(greaterThan)) : undefined; | ||
const lessThan = searchAdvancedFiltersForm?.[INPUT_IDS.LESS_THAN]; | ||
This comment was marked as resolved.
Sorry, something went wrong. |
||
const lessThanFormattedAmount = lessThan ? convertToFrontendAmountAsString(Number(lessThan)) : undefined; | ||
const {inputCallbackRef} = useAutoFocusInput(); | ||
|
||
const updateAmountFilter = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM>) => { | ||
const greater = values[INPUT_IDS.GREATER_THAN]; | ||
const greaterThanBackendAmount = greater ? convertToBackendAmount(Number(greater)) : ''; | ||
const less = values[INPUT_IDS.LESS_THAN]; | ||
const lessThanBackendAmount = less ? convertToBackendAmount(Number(less)) : ''; | ||
updateAdvancedFilters({greaterThan: greaterThanBackendAmount?.toString(), lessThan: lessThanBackendAmount?.toString()}); | ||
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); | ||
}; | ||
|
||
return ( | ||
<ScreenWrapper | ||
testID={SearchFiltersAmountPage.displayName} | ||
shouldShowOfflineIndicatorInWideScreen | ||
offlineIndicatorStyle={styles.mtAuto} | ||
includeSafeAreaPaddingBottom={false} | ||
> | ||
<HeaderWithBackButton | ||
title={translate('common.total')} | ||
onBackButtonPress={() => { | ||
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS); | ||
}} | ||
/> | ||
<FormProvider | ||
style={[styles.flex1, styles.ph5]} | ||
formID={ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM} | ||
onSubmit={updateAmountFilter} | ||
submitButtonText={translate('common.save')} | ||
enabledWhenOffline | ||
> | ||
<View style={styles.mb5}> | ||
<InputWrapper | ||
InputComponent={AmountWithoutCurrencyForm} | ||
inputID={INPUT_IDS.GREATER_THAN} | ||
name={INPUT_IDS.GREATER_THAN} | ||
defaultValue={greaterThanFormattedAmount} | ||
label={translate('search.filters.amount.greaterThan')} | ||
accessibilityLabel={translate('search.filters.amount.greaterThan')} | ||
role={CONST.ROLE.PRESENTATION} | ||
ref={inputCallbackRef} | ||
/> | ||
</View> | ||
<View style={styles.mb5}> | ||
<InputWrapper | ||
InputComponent={AmountWithoutCurrencyForm} | ||
inputID={INPUT_IDS.LESS_THAN} | ||
name={INPUT_IDS.LESS_THAN} | ||
defaultValue={lessThanFormattedAmount} | ||
label={translate('search.filters.amount.lessThan')} | ||
accessibilityLabel={translate('search.filters.amount.lessThan')} | ||
role={CONST.ROLE.PRESENTATION} | ||
/> | ||
</View> | ||
</FormProvider> | ||
</ScreenWrapper> | ||
); | ||
} | ||
|
||
SearchFiltersAmountPage.displayName = 'SearchFiltersAmountPage'; | ||
|
||
export default SearchFiltersAmountPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as resolved.
Sorry, something went wrong.