Skip to content

Commit

Permalink
Search parameters become persistant
Browse files Browse the repository at this point in the history
It was achieved using async/await.
  • Loading branch information
KarinaDavtyan authored and FilipMessa committed Jun 17, 2019
1 parent 7ad06be commit 0c685a1
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 110 deletions.
2 changes: 1 addition & 1 deletion apps/core/components/searchForm/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Props = {|
+confirm: string,
|},
+numberOfRenderedMonths: number,
+onConfirm: ConfirmType => void,
+onConfirm: ConfirmType => void | Promise<void>,
+onDismiss: () => void,
+onPress: () => void,
+style?: StylePropType,
Expand Down
15 changes: 8 additions & 7 deletions apps/core/components/searchForm/Datepickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Props = {|
+setNightsInDestinationSelection: boolean => void,
+setTripType: TripType => void,
+layout: number,
+onDateSelect: () => void,
|};

type State = {|
Expand Down Expand Up @@ -72,7 +73,7 @@ class Datepickers extends React.Component<Props, State> {
});
};

handleDateChange = ({
handleDateChange = async ({
dates,
isNightsInDestinationSelected,
nightsInDestination,
Expand All @@ -87,23 +88,23 @@ class Datepickers extends React.Component<Props, State> {
} = this.props;

if (this.state.isDepartureDatePickerVisible && dates) {
setDepartureDate(...dates);
await setDepartureDate(...dates);
}
if (
this.state.isArrivalDatePickerVisible &&
isNightsInDestinationSelected != null
) {
setNightsInDestinationSelection(isNightsInDestinationSelected);

await setNightsInDestinationSelection(isNightsInDestinationSelected);
if (!isNightsInDestinationSelected && dates) {
setReturnDate(...dates);
await setReturnDate(...dates);
} else if (nightsInDestination) {
setNightsInDestination(...nightsInDestination);
await setNightsInDestination(...nightsInDestination);
}
if (tripType === TRIP_TYPES.ONEWAY) {
setTripType(TRIP_TYPES.RETURN);
await setTripType(TRIP_TYPES.RETURN);
}
}
this.props.onDateSelect();
this.handleDatePickerDismiss();
};

Expand Down
6 changes: 4 additions & 2 deletions apps/core/components/searchForm/PassengersInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = {|
+adults: number,
+infants: number,
+setPassengerData: ($ReadOnly<Passengers>) => void,
+onParamsUpdate: () => void,
|};

type State = {|
Expand All @@ -28,8 +29,9 @@ class PassengersModal extends React.Component<Props, State> {
showPassengerModal: false,
};

handlePassengersSave = (passengersData: $ReadOnly<Passengers>) => {
this.props.setPassengerData(passengersData);
handlePassengersSave = async (passengersData: $ReadOnly<Passengers>) => {
await this.props.setPassengerData(passengersData);
this.props.onParamsUpdate();
this.togglePassengerModal();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Props = {|
type: LocationSearchType,
location: Location | Location[],
) => void,
+onPressSelect: () => void,
+onPlaceSelect: () => void,
|};

type State = {|
Expand Down Expand Up @@ -122,11 +124,13 @@ class PlacePickerContent extends React.Component<Props, State> {
this.refetchSuggestions(text);
};

handlePressOption = (option: OptionType) => {
const { setLocation, pickerType } = this.props;
handlePressOption = async (option: OptionType) => {
const { setLocation, pickerType, onPlaceSelect } = this.props;
const location = mapOptionToLocation(option);

setLocation(pickerType, location);
await setLocation(pickerType, location);
onPlaceSelect();

this.props.onClose();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ import {
type Props = {|
+placeType: PlaceType,
+onClose: () => void,
+onPlaceSelect: () => void,
|};

export default class PlacePickerRenderer extends React.Component<Props> {
renderInner = (data: PlacePickerRendererQueryResponse) => {
const { placeType, onClose } = this.props;
const { placeType, onClose, onPlaceSelect } = this.props;
return (
<PlacePickerContent
pickerType={this.getPickerType(placeType)}
locations={data}
onClose={onClose}
onPlaceSelect={onPlaceSelect}
/>
);
};
Expand Down
21 changes: 16 additions & 5 deletions apps/core/components/searchForm/Placepickers.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import {
type Props = {|
+travelFrom: ?Array<Location>,
+travelTo: ?Array<Location>,
+handlePlaceSwitchPress: () => void,
+switchFromTo: () => void,
+layout: number,
+onPlaceSelect: () => void,
|};

type State = {|
Expand All @@ -58,17 +59,26 @@ class Placepickers extends React.Component<Props, State> {
return '';
};

onClose = () => this.setState({ isModalOpen: false });
onClose = () => {
this.setState({ isModalOpen: false });
this.props.onPlaceSelect();
};

handleFromPress = () =>
this.setState({ placeType: PLACE_TYPE.ORIGIN, isModalOpen: true });

handleToPress = () =>
this.setState({ placeType: PLACE_TYPE.DESTINATION, isModalOpen: true });

handlePlaceSwitchPress = async () => {
await this.props.switchFromTo();
this.props.onPlaceSelect();
};

render() {
const { layout, travelFrom, travelTo, handlePlaceSwitchPress } = this.props;
const { layout, travelFrom, travelTo, onPlaceSelect } = this.props;
const rowLayout = layout >= LAYOUT.largeMobile;

return (
<PickersWrapper layout={layout}>
<TripInput
Expand All @@ -78,7 +88,7 @@ class Placepickers extends React.Component<Props, State> {
icon={<Icon name="airplane-takeoff" />}
value={this.getLocationsNames(travelFrom)}
/>
<TouchableWithoutFeedback onPress={handlePlaceSwitchPress}>
<TouchableWithoutFeedback onPress={this.handlePlaceSwitchPress}>
<View
style={[styles.placeSwitch, rowLayout && styles.rowPlaceSwitch]}
>
Expand All @@ -95,6 +105,7 @@ class Placepickers extends React.Component<Props, State> {
<PlacePicker
onClose={this.onClose}
placeType={this.state.placeType}
onPlaceSelect={onPlaceSelect}
/>
</Modal>
</PickersWrapper>
Expand Down Expand Up @@ -148,7 +159,7 @@ const select = ({
}: SearchContextState) => ({
travelFrom,
travelTo,
handlePlaceSwitchPress: switchFromTo,
switchFromTo,
});

export default withLayoutContext(layoutSelect)(
Expand Down
112 changes: 57 additions & 55 deletions apps/core/components/searchForm/SearchForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,76 +66,78 @@ class SearchForm extends React.Component<Props> {
return '';
};

handleSubmitPress = () => {
getSearchParams = () => {
const {
travelFrom,
travelTo,
tripType,
adults,
infants,
limit,
onSubmit,
isNightsInDestinationSelected,
nightsInDestinationFrom,
nightsInDestinationTo,
} = this.props;

const dateFrom = format(this.props.dateFrom, BASIC_ISO_DATE_FORMAT);
const dateTo = format(this.props.dateTo, BASIC_ISO_DATE_FORMAT);
const returnDateFrom = format(
this.props.returnDateFrom,
BASIC_ISO_DATE_FORMAT,
);
const returnDateTo = format(this.props.returnDateTo, BASIC_ISO_DATE_FORMAT);

return {
travelFrom: qs.stringify(travelFrom),
travelTo: qs.stringify(travelTo),
travelFromName: this.convertLocationsToParams(travelFrom, 'name'),
travelToName: this.convertLocationsToParams(travelTo, 'name'),
sortBy: 'QUALITY',
limit,
adults,
infants,
dateFrom,
dateTo,
tripType,
...(tripType === TRIP_TYPES.RETURN // @TODO refactor
? isNightsInDestinationSelected
? {
isNightsInDestinationSelected,
nightsInDestinationFrom: parseInt(nightsInDestinationFrom, 10),
nightsInDestinationTo: parseInt(nightsInDestinationTo, 10),
}
: {
returnDateFrom,
returnDateTo,
}
: {}),
};
};

handleSubmitPress = () => {
const { travelFrom } = this.props;
if (travelFrom == null || travelFrom.length === 0) {
this.props.setAlertContent({
message: 'Please choose an origin place',
});
} else {
const dateFrom = format(this.props.dateFrom, BASIC_ISO_DATE_FORMAT);
const dateTo = format(this.props.dateTo, BASIC_ISO_DATE_FORMAT);
const returnDateFrom = format(
this.props.returnDateFrom,
BASIC_ISO_DATE_FORMAT,
);
const returnDateTo = format(
this.props.returnDateTo,
BASIC_ISO_DATE_FORMAT,
);
if (onSubmit != null) {
onSubmit({
dateFrom,
dateTo,
returnDateFrom,
returnDateTo,
nightsInDestinationFrom,
nightsInDestinationTo,
tripType,
travelFrom,
travelTo,
sort: 'QUALITY',
limit,
adults,
infants,
});
}
const searchParams = this.getSearchParams();
this.props.navigation.navigate(Routes.RESULTS, {
...searchParams,
});
}
};

handleParamsUpdate = () => {
const { travelFrom } = this.props;
if (travelFrom == null || travelFrom.length === 0) {
this.props.setAlertContent({
message: 'Please choose an origin place',
});
} else if (Platform.OS === 'web') {
const searchParams = this.getSearchParams();
this.props.navigation.navigate(Routes.RESULTS, {
travelFrom: qs.stringify(travelFrom),
travelTo: qs.stringify(travelTo),
travelFromName: this.convertLocationsToParams(travelFrom, 'name'),
travelToName: this.convertLocationsToParams(travelTo, 'name'),
sort: 'QUALITY',
limit,
adults,
infants,
dateFrom,
dateTo,
tripType,
...(tripType === TRIP_TYPES.RETURN // @TODO refactor
? isNightsInDestinationSelected
? {
isNightsInDestinationSelected,
nightsInDestinationFrom: parseInt(nightsInDestinationFrom, 10),
nightsInDestinationTo: parseInt(nightsInDestinationTo, 10),
}
: {
returnDateFrom,
returnDateTo,
}
: {}),
...searchParams,
});
}
};
Expand All @@ -144,10 +146,10 @@ class SearchForm extends React.Component<Props> {
const desktopLayout = this.props.layout >= LAYOUT.desktop;
return (
<>
<SearchFormModes />
<SearchFormModes onParamsUpdate={this.handleParamsUpdate} />
<View style={desktopLayout && styles.inputsDesktop}>
<Placepickers />
<Datepickers />
<Placepickers onPlaceSelect={this.handleParamsUpdate} />
<Datepickers onDateSelect={this.handleParamsUpdate} />
<View style={[styles.bottom, desktopLayout && styles.bottomDesktop]}>
{this.props.showButton && (
<Button
Expand Down
10 changes: 7 additions & 3 deletions apps/core/components/searchForm/SearchFormModes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import { StyleSheet } from '@kiwicom/universal-components';
import TripTypeSelect from './TripTypeSelect';
import PassengersInput from './PassengersInput';

const SearchFormModes = () => {
type Props = {|
+onParamsUpdate: () => void,
|};

const SearchFormModes = ({ onParamsUpdate }: Props) => {
return (
<View style={styles.container}>
<TripTypeSelect />
<TripTypeSelect onParamsUpdate={onParamsUpdate} />
<View style={styles.hSpacer} />
<PassengersInput />
<PassengersInput onParamsUpdate={onParamsUpdate} />
</View>
);
};
Expand Down
6 changes: 4 additions & 2 deletions apps/core/components/searchForm/TripTypeSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Props = {|
+tripType: string,
+layout: number,
+setTripType: string => void,
+onParamsUpdate: () => void,
|};

type State = {|
Expand All @@ -37,9 +38,10 @@ class TripTypeSelect extends React.Component<Props, State> {
showTripTypeModal: false,
};

handleTripTypeSelect = (type: string) => {
handleTripTypeSelect = async (type: string) => {
if (type === TRIP_TYPES.RETURN || type === TRIP_TYPES.ONEWAY) {
this.props.setTripType(type);
await this.props.setTripType(type);
this.props.onParamsUpdate();
}
};

Expand Down
4 changes: 2 additions & 2 deletions apps/core/contexts/searchContext/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const urlQuery = {
'0%5Bid%5D=TG9jYXRpb246b3Nsb19ubw%3D%3D&0%5BlocationId%5D=oslo_no&0%5Bname%5D=Oslo&0%5Btype%5D=destination',
travelFromName: 'Prague',
travelToName: 'Oslo',
sort: 'QUALITY',
sortBy: 'QUALITY',
limit: '50',
adults: '2',
infants: '1',
Expand All @@ -63,7 +63,7 @@ describe('parseURLqueryToState', () => {
"limit": 50,
"returnDateFrom": 2019-06-03T00:00:00.000Z,
"returnDateTo": 2019-06-03T00:00:00.000Z,
"sort": "QUALITY",
"sortBy": "QUALITY",
"travelFrom": Array [
Object {
"id": "TG9jYXRpb246cHJhZ3VlX2N6",
Expand Down
Loading

0 comments on commit 0c685a1

Please sign in to comment.