<commit_before>// @flow

import * as React from 'react';
import { ScrollView } from 'react-native';
import idx from 'idx';
import { graphql } from 'react-relay';
import { PublicApiRenderer } from '@kiwicom/react-native-app-relay';
import { Layout } from '@kiwicom/react-native-app-shared';
import { connect } from '@kiwicom/react-native-app-redux';

import AllHotelsSearch from './AllHotelsSearch';
import SearchForm from './searchForm/SearchForm';
import FilterStripe from '../filter/FilterStripe';
import type {
  SearchParams,
  OnChangeSearchParams,
} from './searchForm/SearchParametersType';
import type {
  FilterParams,
  OnChangeFilterParams,
} from '../filter/FilterParametersType';
import type { AllHotels_cityLookup_QueryResponse } from './__generated__/AllHotels_cityLookup_Query.graphql';
import GeneralError from '../../../shared/src/errors/GeneralError';
import type { HotelsReducerState } from '../HotelsReducer';
import type { FilterReducerState } from '../filter/FiltersReducer';
import type { AvailableHotelSearchInput } from '../singleHotel/AvailableHotelSearchInput';

type Props = {|
  onSearchChange: OnChangeSearchParams => void,
  onLocationChange: (location: string) => void,
  search: SearchParams,
  location: string,
  filter: FilterParams,
  onFilterChange: OnChangeFilterParams => void,
  currency: string,
  openSingleHotel: (searchParams: AvailableHotelSearchInput) => void,
|};

/**
 * We need to lookup city ID first and only after that we can search for all
 * hotels. This is why we use two nested query renderers.
 */
class AllHotels extends React.Component<Props> {
  renderAllHotelsSearchPublicRenderer = (
    rendererProps: AllHotels_cityLookup_QueryResponse,
  ) => {
    const cityId = idx(rendererProps, _ => _.city.edges[0].node.id) || null;
    if (cityId === null) {
      return <GeneralError errorMessage="Cannot find such city." />;
    }
    return (
      <AllHotelsSearch
        {...rendererProps}
        openSingleHotel={this.props.openSingleHotel}
        cityId={cityId}
      />
    );
  };

  render = () => (
    <Layout>
      <ScrollView bounces={false} contentContainerStyle={{ flexGrow: 1 }}>
        <SearchForm
          onChange={this.props.onSearchChange}
          onLocationChange={this.props.onLocationChange}
          search={this.props.search}
          location={this.props.location}
        />
        <FilterStripe
          filter={this.props.filter}
          onChange={this.props.onFilterChange}
          currency={this.props.currency}
        />
        <PublicApiRenderer
          query={graphql`
            query AllHotels_cityLookup_Query($prefix: String!) {
              city: hotelCities(prefix: $prefix, first: 1) {
                edges {
                  node {
                    id
                  }
                }<commit_msg>Fix all hotels search coordinates<commit_after>// @flow

import * as React from 'react';
import { ScrollView } from 'react-native';
import idx from 'idx';
import { graphql } from 'react-relay';
import { PublicApiRenderer } from '@kiwicom/react-native-app-relay';
import { Layout } from '@kiwicom/react-native-app-shared';
import { connect } from '@kiwicom/react-native-app-redux';

import AllHotelsSearch from './AllHotelsSearch';
import SearchForm from './searchForm/SearchForm';
import FilterStripe from '../filter/FilterStripe';
import type {
  SearchParams,
  OnChangeSearchParams,
} from './searchForm/SearchParametersType';
import type {
  FilterParams,
  OnChangeFilterParams,
} from '../filter/FilterParametersType';
import type { AllHotels_cityLookup_QueryResponse } from './__generated__/AllHotels_cityLookup_Query.graphql';
import GeneralError from '../../../shared/src/errors/GeneralError';
import type { HotelsReducerState } from '../HotelsReducer';
import type { FilterReducerState } from '../filter/FiltersReducer';
import type { AvailableHotelSearchInput } from '../singleHotel/AvailableHotelSearchInput';
import type { Coordinates } from '../CoordinatesType';

type Props = {|
  onSearchChange: OnChangeSearchParams => void,
  onLocationChange: (location: string) => void,
  search: SearchParams,
  coordinates: Coordinates | null,
  location: string,
  filter: FilterParams,
  onFilterChange: OnChangeFilterParams => void,
  currency: string,
  openSingleHotel: (searchParams: AvailableHotelSearchInput) => void,
|};

/**
 * We need to lookup city ID first and only after that we can search for all
 * hotels. This is why we use two nested query renderers.
 */
class AllHotels extends React.Component<Props> {
  renderAllHotelsSearchPublicRenderer = (
    rendererProps: AllHotels_cityLookup_QueryResponse,
  ) => {
    const cityId = idx(rendererProps, _ => _.city.edges[0].node.id) || null;
    if (cityId === null) {
      return <GeneralError errorMessage="Cannot find such city." />;
    }
    return (
      <AllHotelsSearch
        {...rendererProps}
        coordinates={this.props.coordinates}
        openSingleHotel={this.props.openSingleHotel}
        cityId={cityId}
      />
    );
  };

  render = () => (
    <Layout>
      <ScrollView bounces={false} contentContainerStyle={{ flexGrow: 1 }}>
        <SearchForm
          onChange={this.props.onSearchChange}
          onLocationChange={this.props.onLocationChange}
          search={this.props.search}
          location={this.props.location}
        />
        <FilterStripe
          filter={this.props.filter}
          onChange={this.props.onFilterChange}
          currency={this.props.currency}
        />
        <PublicApiRenderer
          query={graphql`
            query AllHotels_cityLookup_Query($prefix: String!) {
              city: hotelCities(prefix: $prefix, first: 1) {
                edges {
                  node {
                    id
                  }
                }
{'fraction_non_alphanumeric': 0.11758638473439917, 'fraction_numerical': 0.0010314595152140279, 'mean_word_length': 3.1203966005665724, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4212268', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1640, 'n_words': 381}