Skip to content

17548 Personal details push to page #21049

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

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
6c2c140
push to page menu item
gedu Jun 9, 2023
c9c320a
Fixed comments
gedu Jun 19, 2023
e746928
push to page menu item
gedu Jun 9, 2023
062bc8a
Fixed comments
gedu Jun 19, 2023
b603569
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jun 22, 2023
56924ba
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jun 23, 2023
474125f
Made array containerStyle prop
gedu Jun 26, 2023
74cc95a
handling navigation and state update
gedu Jun 28, 2023
f01b1e0
improved navigation animation
gedu Jun 28, 2023
f850f23
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 4, 2023
a2cd0b6
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 7, 2023
e48c601
Updated navigation and state changes
gedu Jul 7, 2023
8148f4f
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 10, 2023
c2e715e
Fixed comments
gedu Jul 11, 2023
7ef53f1
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 11, 2023
aee62fa
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 14, 2023
b13ac10
Removed Navigation fro State and Country pages
gedu Jul 14, 2023
8898224
fixed lint errors
gedu Jul 14, 2023
fe46e3c
fixed missing dependency
gedu Jul 14, 2023
64b8dbb
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 17, 2023
761c75a
Fixed comments
gedu Jul 17, 2023
d641006
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 18, 2023
e06bc96
Fixed comments
gedu Jul 19, 2023
b94e99f
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
gedu Jul 19, 2023
4ee01d6
Removed fallback
gedu Jul 19, 2023
2cead61
Fixed comments
gedu Jul 20, 2023
675a7c3
Fixed comments
gedu Jul 24, 2023
0779f34
fixed comments
gedu Jul 24, 2023
d063f55
fix: add missing space
koko57 Jul 25, 2023
128597b
fix: update searchValue when current value changes
koko57 Jul 25, 2023
94bd35a
fix: include allCountries in the deps array
koko57 Jul 25, 2023
f750d18
fix: apply requested changes
koko57 Jul 25, 2023
0e416e0
fix: minor change
koko57 Jul 25, 2023
5c0ef29
fix: fix prop name problem
koko57 Jul 26, 2023
60c9fbb
fix: resolve conflicts
koko57 Jul 26, 2023
b34b031
Merge branch 'main' into edu/17548_personal_details_push_to_page_patch
koko57 Jul 26, 2023
4852ccb
fix: change useOnNetworkReconnect to useNetwork
koko57 Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ROUTES.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export default {
getReportShareCodeRoute: (reportID) => `r/${reportID}/details/shareCode`,
REPORT_ATTACHMENTS: 'r/:reportID/attachment',
getReportAttachmentRoute: (reportID, source) => `r/${reportID}/attachment?source=${encodeURI(source)}`,
SELECT_YEAR: 'select-year',
getYearSelectionRoute: (minYear, maxYear, currYear, backTo) => `select-year?min=${minYear}&max=${maxYear}&year=${currYear}&backTo=${backTo}`,
SELECT_COUNTRY: 'select-country',
getCountrySelectionRoute: (key, backTo) => `select-country?key=${key}&backTo=${backTo}`,
SELECT_STATE: 'select-states',
getUsaStateSelectionRoute: (key, backTo) => `select-states?key=${key}&backTo=${encodeURIComponent(backTo)}`,

/** This is a utility route used to go to the user's concierge chat, or the sign-in page if the user's not authenticated */
CONCIERGE: 'concierge',
Expand Down
22 changes: 16 additions & 6 deletions src/components/AddressSearch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const propTypes = {
/** Maximum number of characters allowed in search input */
maxInputLength: PropTypes.number,

onAddressChange: PropTypes.func,

...withLocalizePropTypes,
};

Expand All @@ -86,6 +88,7 @@ const defaultProps = {
zipCode: 'addressZipCode',
},
maxInputLength: undefined,
onAddressChange: () => {},
};

// Do not convert to class component! It's been tried before and presents more challenges than it's worth.
Expand Down Expand Up @@ -168,9 +171,15 @@ function AddressSearch(props) {
state: state || stateAutoCompleteFallback,
};

const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, country);
if (isValidCountryCode) {
values.country = country;
}

// If the address is not in the US, use the full length state name since we're displaying the address's
// state / province in a TextInput instead of in a picker.
if (country !== CONST.COUNTRY.US) {
const isUS = country === CONST.COUNTRY.US;
if (!isUS) {
values.state = longStateName;
}

Expand All @@ -186,18 +195,19 @@ function AddressSearch(props) {
values.street += `, ${subpremise}`;
}

const isValidCountryCode = lodashGet(CONST.ALL_COUNTRIES, country);
if (isValidCountryCode) {
values.country = country;
}

if (props.inputID) {
_.each(values, (value, key) => {
const inputKey = lodashGet(props.renamedInputKeys, key, key);
props.onInputChange(value, inputKey);
});
if (props.onAddressChange) {
props.onAddressChange();
}
} else {
props.onInputChange(values);
if (props.onAddressChange) {
props.onAddressChange();
}
}
};

Expand Down
114 changes: 69 additions & 45 deletions src/components/CountryPicker.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,91 @@
import _ from 'underscore';
import React, {forwardRef} from 'react';
import React, {useCallback, useEffect} from 'react';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import Picker from './Picker';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import {useFocusEffect} from '@react-navigation/native';
import styles from '../styles/styles';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import * as PersonalDetails from '../libs/actions/PersonalDetails';
import useNavigationStorage from '../hooks/useNavigationStorage';
import Navigation from '../libs/Navigation/Navigation';
import useLocalize from '../hooks/useLocalize';
import ROUTES from '../ROUTES';
import FormHelpMessage from './FormHelpMessage';

const propTypes = {
/** The label for the field */
label: PropTypes.string,
/** The ISO code of the country */
countryISO: PropTypes.string,

/** A callback method that is called when the value changes and it receives the selected value as an argument. */
onInputChange: PropTypes.func.isRequired,

/** The value that needs to be selected */
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),

/** The ID used to uniquely identify the input in a form */
inputID: PropTypes.string,

/** Saves a draft of the input value when used in a form */
shouldSaveDraft: PropTypes.bool,
/** Form Error description */
errorText: PropTypes.string,

/** Callback that is called when the text input is blurred */
onBlur: PropTypes.func,
/** Country to display */
// eslint-disable-next-line react/require-default-props
value: PropTypes.string,

/** Error text to display */
errorText: PropTypes.string,
/** ID of the input */
inputID: PropTypes.string.isRequired,

...withLocalizePropTypes,
/** Callback to call when the input changes */
onInputChange: PropTypes.func,
};

const defaultProps = {
label: '',
value: undefined,
countryISO: '',
errorText: '',
shouldSaveDraft: false,
inputID: undefined,
onBlur: () => {},
onInputChange: () => {},
};

const CountryPicker = forwardRef((props, ref) => {
const COUNTRIES = _.map(props.translate('allCountries'), (countryName, countryISO) => ({
value: countryISO,
label: countryName,
}));
const CountryPicker = React.forwardRef(({value, countryISO, inputID, errorText, onInputChange}, ref) => {
const {translate} = useLocalize();
const countryValue = value || countryISO || '';
const [collect, save] = useNavigationStorage(inputID, countryValue);

useEffect(() => {
const savedCountry = collect();
if (!countryValue || savedCountry === countryValue) {
return;
}
save(countryValue);
}, [countryValue, collect, save]);

useFocusEffect(
useCallback(() => {
const savedCountry = collect();
if (savedCountry && savedCountry !== countryValue) {
save(savedCountry);
// Needed to call onInputChange, so Form can update the validation and values
onInputChange(savedCountry);
}
// onInputChange isn't a stable function, so we can't add it to the dependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collect, countryValue]),
);

const navigateToCountrySelector = () => {
Navigation.navigate(ROUTES.getCountrySelectionRoute(inputID, Navigation.getActiveRoute()));
};

const title = PersonalDetails.getCountryName(countryValue);
const descStyle = title.length === 0 ? styles.addressPickerDescription : null;
return (
<Picker
ref={ref}
inputID={props.inputID}
placeholder={{value: '', label: '-'}}
items={COUNTRIES}
onInputChange={props.onInputChange}
value={props.value}
label={props.label || props.translate('common.country')}
errorText={props.errorText}
onBlur={props.onBlur}
shouldSaveDraft={props.shouldSaveDraft}
/>
<View>
<MenuItemWithTopDescription
ref={ref}
shouldShowRightIcon
title={title}
descriptionTextStyle={descStyle}
description={translate('common.country')}
onPress={navigateToCountrySelector}
/>
<View style={styles.ml5}>
<FormHelpMessage message={errorText} />
</View>
</View>
);
});

CountryPicker.propTypes = propTypes;
CountryPicker.defaultProps = defaultProps;
CountryPicker.displayName = 'CountryPicker';

export default withLocalize(CountryPicker);
export default CountryPicker;
19 changes: 9 additions & 10 deletions src/components/MenuItemWithTopDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ const propTypes = {
...menuItemPropTypes,
};

function MenuItemWithTopDescription(props) {
return (
<MenuItem
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
shouldShowBasicTitle
shouldShowDescriptionOnTop
/>
);
}
const MenuItemWithTopDescription = React.forwardRef((props, ref) => (
<MenuItem
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to forward the ref here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was needed for the Form MenuItem wasn't handling well the ref from Form so I had to add it, I don't remember the exact case that it fixes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you are right. Form relies on the ref function to be called to set its internal refs.

App/src/components/Form.js

Lines 277 to 278 in 751cf85

ref: (node) => {
inputRefs.current[inputID] = node;

In that case can you please re-write the function back using the function MenuItemWithTopDescription format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and it doesn't show the error labels for example. I think It would be better to handle in a separate PR

cc: @mountiny

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely better in a separate PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gedu I'm sorry did you mean that if we write this in function format the ref won't work correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if before was a function, it wasn't working, that's why I need to add the ref

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is to write this as:

function MenuItemWithTopDescription {...}
export default React.forwardRef(...);

Just for consistency, I'm aware that ref is still needed.

shouldShowBasicTitle
shouldShowDescriptionOnTop
/>
));

MenuItemWithTopDescription.propTypes = propTypes;
MenuItemWithTopDescription.displayName = 'MenuItemWithTopDescription';
Expand Down
125 changes: 80 additions & 45 deletions src/components/StatePicker.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,102 @@
import _ from 'underscore';
import React, {forwardRef} from 'react';
import React, {useCallback, useMemo, useEffect} from 'react';
import {View} from 'react-native';
import {useRoute, useFocusEffect} from '@react-navigation/native';
import PropTypes from 'prop-types';
import Picker from './Picker';
import withLocalize, {withLocalizePropTypes} from './withLocalize';
import useNavigationStorage from '../hooks/useNavigationStorage';
import styles from '../styles/styles';
import MenuItemWithTopDescription from './MenuItemWithTopDescription';
import useLocalize from '../hooks/useLocalize';
import Navigation from '../libs/Navigation/Navigation';
import ROUTES from '../ROUTES';
import FormHelpMessage from './FormHelpMessage';

const propTypes = {
/** The label for the field */
label: PropTypes.string,

/** A callback method that is called when the value changes and it receives the selected value as an argument. */
onInputChange: PropTypes.func.isRequired,

/** The value that needs to be selected */
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),

/** The ID used to uniquely identify the input in a Form */
inputID: PropTypes.string,
/** Error text to display */
errorText: PropTypes.string,

/** Saves a draft of the input value when used in a form */
shouldSaveDraft: PropTypes.bool,
/** Default value to display */
defaultValue: PropTypes.string,

/** Callback that is called when the text input is blurred */
onBlur: PropTypes.func,
/** State to display */
// eslint-disable-next-line react/require-default-props
value: PropTypes.string,

/** Error text to display */
errorText: PropTypes.string,
/** ID of the input */
inputID: PropTypes.string.isRequired,

...withLocalizePropTypes,
/** Callback to call when the input changes */
onInputChange: PropTypes.func,
};

const defaultProps = {
label: '',
value: undefined,
errorText: '',
shouldSaveDraft: false,
inputID: undefined,
onBlur: () => {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #30195 , onBlur is crucial for the Form validation; it should have been preserved when migrating the StatePicker to the new push-to-page component.

defaultValue: '',
onInputChange: () => {},
};

const StatePicker = forwardRef((props, ref) => {
const STATES = _.chain(props.translate('allStates'))
.sortBy((state) => state.stateName.toLowerCase())
.map((state) => ({value: state.stateISO, label: state.stateName}))
.value();
const StatePicker = React.forwardRef(({value, defaultValue, inputID, errorText, onInputChange}, ref) => {
const route = useRoute();
const {translate} = useLocalize();
const formValue = value || defaultValue || '';
const [collect, save] = useNavigationStorage(inputID, formValue);

useEffect(() => {
const collectedState = collect();
if (!formValue || collectedState === formValue) {
return;
}
save(formValue);
}, [formValue, collect, save]);

useFocusEffect(
useCallback(() => {
const collectedState = collect();
if (collectedState && collectedState !== formValue) {
save(collectedState);
// Needed to call onInputChange, so Form can update the validation and values
onInputChange(collectedState);
}
// onInputChange isn't a stable function, so we can't add it to the dependency array
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [collect, formValue]),
);

const navigateToCountrySelector = () => {
// Try first using the route.path so I can keep any query params
Navigation.navigate(ROUTES.getUsaStateSelectionRoute(inputID, route.path || Navigation.getActiveRoute()));
};

const title = useMemo(() => {
const allStates = translate('allStates');

if (allStates[formValue]) {
return allStates[formValue].stateName;
}

return '';
}, [translate, formValue]);

const descStyle = title.length === 0 ? styles.addressPickerDescription : null;

return (
<Picker
ref={ref}
inputID={props.inputID}
placeholder={{value: '', label: '-'}}
items={STATES}
onInputChange={props.onInputChange}
value={props.value}
label={props.label || props.translate('common.state')}
errorText={props.errorText}
onBlur={props.onBlur}
shouldSaveDraft={props.shouldSaveDraft}
/>
<View>
<MenuItemWithTopDescription
ref={ref}
shouldShowRightIcon
title={title}
description={translate('common.state')}
descriptionTextStyle={descStyle}
onPress={navigateToCountrySelector}
/>
<View style={styles.ml5}>
<FormHelpMessage message={errorText} />
</View>
</View>
);
});

StatePicker.propTypes = propTypes;
StatePicker.defaultProps = defaultProps;
StatePicker.displayName = 'StatePicker';

export default withLocalize(StatePicker);
export default StatePicker;
1 change: 1 addition & 0 deletions src/components/TextInput/BaseTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ function BaseTextInput(props) {
</PressableWithoutFeedback>
{!_.isEmpty(inputHelpText) && (
<FormHelpMessage
style={props.hintContainerStyle}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't seem to pass hintContainerStyle anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems that get lost in some merge, I've been looking for a comment or something about why I added in the first place, but I couldn't, and can't remember I will remove it

isError={!_.isEmpty(props.errorText)}
message={inputHelpText}
/>
Expand Down
Loading