Skip to content

Commit 2fea687

Browse files
committed
Refactoring containers and zooming map when place is submited
1 parent 235cd8c commit 2fea687

File tree

7 files changed

+64
-60
lines changed

7 files changed

+64
-60
lines changed

client/src/Components/GeoSuggest/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import * as React from 'react'
22
import Geosuggest from 'react-geosuggest'
33
import { Subscribe } from 'unstated'
44

5-
import { SearchLocation } from 'src/Containers/SearchLocation'
6-
5+
import { Location } from 'src/Containers/Location'
76
import './geoInput.css'
87

98
export class GeoSuggest extends React.PureComponent {
109
public render() {
1110
return (
12-
<Subscribe to={[SearchLocation]}>
13-
{({ setPlace }: SearchLocation) => (
11+
<Subscribe to={[Location]}>
12+
{({ setPlace }: Location) => (
1413
<Geosuggest
1514
placeholder="Enter a location"
1615
onSuggestSelect={place => setPlace(place)}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Container } from 'unstated'
2+
import { Suggest } from 'react-geosuggest'
3+
4+
interface State {
5+
center: {
6+
lat: number
7+
lng: number
8+
}
9+
marker: {
10+
lat: number | null
11+
lng: number | null
12+
}
13+
place: string[]
14+
}
15+
16+
export class Location extends Container<State> {
17+
public readonly state = {
18+
center: {
19+
lat: 52.0685,
20+
lng: 19.9475
21+
},
22+
marker: {
23+
lat: 52.0685,
24+
lng: 52.0685
25+
},
26+
place: ['']
27+
}
28+
29+
public setPostion = (lat: number, lng: number) =>
30+
this.setState({ marker: { lat, lng } })
31+
32+
public setPlace = async (place: Suggest) => {
33+
if (place) {
34+
const {
35+
label,
36+
location: { lat, lng }
37+
} = place
38+
const splitAddress = label.split(', ')
39+
40+
await this.setState({
41+
place: splitAddress,
42+
center: { lat: parseFloat(lat), lng: parseFloat(lng) }
43+
})
44+
}
45+
}
46+
}

client/src/Containers/Position/index.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

client/src/Containers/SearchLocation/index.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

client/src/Graphql/SearchListings/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface Props {
3636
export class SearchListingsQuery extends React.PureComponent<Props> {
3737
public render() {
3838
const { address } = this.props
39-
39+
console.log(address)
4040
return (
4141
<Query query={searchListingsQuery} variables={{ address }}>
4242
{({ data, loading }) => {

client/src/Pages/Listings/Components/ListingsList/index.tsx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import * as React from 'react'
2+
import { Subscribe } from 'unstated'
23

34
import { SearchListingsQuery } from 'src/Graphql/SearchListings'
45
import { ListingItem } from './Components/ListingItem'
6+
import { Location } from 'src/Containers/Location'
57

68
import { Container } from './style'
7-
import { Subscribe } from 'unstated'
8-
import { SearchLocation } from 'src/Containers/SearchLocation'
9-
import { Postion } from 'src/Containers/Position'
109

1110
export const ListingsList: React.SFC<{}> = () => (
1211
<Container>
13-
<Subscribe to={[SearchLocation, Postion]}>
14-
{(
15-
{ state: { place } }: SearchLocation,
16-
{ setPostion }: Postion
17-
) => (
12+
<Subscribe to={[Location]}>
13+
{({ state: { place }, setPostion }: Location) => (
1814
<SearchListingsQuery address={place}>
1915
{({ listings, loading }) =>
2016
listings.map(listing => (
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import * as React from 'react'
22
import { withGoogleMap, GoogleMap, Marker } from 'react-google-maps'
33
import { Subscribe } from 'unstated'
4-
import { Postion } from 'src/Containers/Position'
4+
5+
import { Location } from 'src/Containers/Location'
56

67
export const Map = withGoogleMap(() => (
7-
<Subscribe to={[Postion]}>
8-
{({ state: { lat, lng } }) => (
9-
<GoogleMap
10-
defaultZoom={7}
11-
defaultCenter={{
12-
lat: 52.0685,
13-
lng: 19.9475
14-
}}
15-
>
16-
<Marker position={{ lat, lng }} />
17-
</GoogleMap>
18-
)}
8+
<Subscribe to={[Location]}>
9+
{({ state: { center, marker, place } }: Location) =>
10+
(console.log(place.length, 'LENG') as any) || (
11+
<GoogleMap zoom={place.length > 1 ? 13 : 6} center={center}>
12+
{marker.lat && <Marker position={marker} />}
13+
</GoogleMap>
14+
)
15+
}
1916
</Subscribe>
2017
))

0 commit comments

Comments
 (0)