This repository was archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Feature clubs by location #25
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
31316c5
Add service geocode
cristianbgp 82f798a
Update club with district, latitude and longitude
cristianbgp c3c6a66
Sort clubs by location
cristianbgp 3e9fffd
Add latitude and longitude to the last club
cristianbgp 3e6b365
Add icon for location
cristianbgp 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 hidden or 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 hidden or 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 hidden or 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,7 @@ | ||
| class AddDistrictToClubs < ActiveRecord::Migration[5.2] | ||
| def change | ||
| add_column :clubs, :district, :string | ||
| add_column :clubs, :latitude, :string | ||
| add_column :clubs, :longitude, :string | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| REACT_APP_VERSION=$npm_package_version | ||
| REACT_APP_API_URL=http://localhost:4000/api | ||
| REACT_APP_API_URL_PRODUCTION= | ||
| REACT_APP_OPEN_CAGE_DATA_KEY= |
This file contains hidden or 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 hidden or 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,22 @@ | ||
| /** @jsx jsx */ | ||
| import React from "react"; | ||
| import { jsx } from "@emotion/core"; | ||
| import { Card, Title } from "../components/ui"; | ||
|
|
||
| function ClubCard({ club }) { | ||
| const styleCard = { | ||
| maxWidth: "100%", | ||
| marginBottom: "1.5em" | ||
| }; | ||
|
|
||
| return ( | ||
| <Card css={styleCard}> | ||
| <Title>{club.name}</Title> | ||
| <p>{JSON.stringify(club)}</p> | ||
| <p>{club.position}</p> | ||
| <p>{club.distance !== 0 ? `${club.distance / 1000.0}km` : null}</p> | ||
| </Card> | ||
| ); | ||
| } | ||
|
|
||
| export default ClubCard; |
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| async function getCoords(place) { | ||
| let apikey = process.env.REACT_APP_OPEN_CAGE_DATA_KEY; | ||
| let api_url = "https://api.opencagedata.com/geocode/v1/json"; | ||
| let request_url = | ||
| api_url + | ||
| "?" + | ||
| "q=" + | ||
| encodeURIComponent(place) + | ||
| "&key=" + | ||
| apikey + | ||
| "&language=es" + | ||
| "&pretty=1" + | ||
| "&countrycode=pe" + | ||
| "&no_annotations=1"; | ||
| const response = await fetch(request_url); | ||
|
|
||
| if (!response.ok) throw new Error(response.statusText); | ||
| return response.json(); | ||
| } | ||
|
|
||
| export { getCoords }; | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,31 +1,130 @@ | ||
| /** @jsx jsx */ | ||
| import React from "react"; | ||
| import { jsx } from "@emotion/core"; | ||
| import { getClubs } from "../services/club"; | ||
| import { useClubs } from "../selectors/selectors"; | ||
| import { useSetClubs } from "../actions/action-hooks"; | ||
| import { useClubs } from "../selectors/selectors"; | ||
| import { getClubs } from "../services/club"; | ||
| import ClubCard from "../components/club-card"; | ||
| import Club from "../components/club"; | ||
| import { getDistance } from "geolib"; | ||
| import { Select } from "../components/ui"; | ||
|
|
||
| function Home() { | ||
| const [loading, setLoading] = React.useState(false); | ||
| const clubs = useClubs(); | ||
| const setClubs = useSetClubs(); | ||
| const [position, setPosition] = React.useState([0, 0]); | ||
| const [selectedLocation, setSelectedLocation] = React.useState(); | ||
| const [sortType, setSortType] = React.useState("location"); | ||
|
|
||
| React.useEffect(() => { | ||
| setLoading(true); | ||
| getClubs().then(clubs => { | ||
| setClubs(clubs); | ||
| setLoading(false); | ||
| }); | ||
| }, []); | ||
| }, [setClubs]); | ||
|
|
||
| function setDistance(clubPosition, position) { | ||
| return getDistance( | ||
| { latitude: position[0], longitude: position[1] }, | ||
| { | ||
| latitude: parseFloat(clubPosition.latitude), | ||
| longitude: parseFloat(clubPosition.longitude) | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| React.useEffect(() => { | ||
| const watchID = navigator.geolocation.watchPosition(pos => { | ||
| setPosition([pos.coords.latitude, pos.coords.longitude]); | ||
| }); | ||
| return () => { | ||
| navigator.geolocation.clearWatch(watchID); | ||
| }; | ||
| }, [setPosition]); | ||
|
|
||
| function handleChangeLocation(e) { | ||
| console.log(e.target.value); | ||
| setSelectedLocation(e.target.value); | ||
| } | ||
|
|
||
| function handleChangeSortType(e) { | ||
| console.log(e.target.value); | ||
| setSortType(e.target.value); | ||
| } | ||
|
|
||
| function sortBy(a, b) { | ||
| switch (sortType) { | ||
| case "location": | ||
| return a.distance - b.distance; | ||
|
|
||
| case "favorites": | ||
| return b.favorited_count - a.favorited_count; | ||
|
|
||
| case "name": | ||
| return a.name - b.name; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const styleSelectsContainer = { | ||
| display: "flex", | ||
| justifyContent: "space-between", | ||
| marginBottom: "1em" | ||
| }; | ||
|
|
||
| return ( | ||
| <div> | ||
| <h2>Filters</h2> | ||
| {loading && <div>Loading</div>} | ||
| {clubs.map(club => ( | ||
| <Club club={club} key={club.id} /> | ||
| ))} | ||
| <div css={styleSelectsContainer}> | ||
| <Select | ||
| onChange={handleChangeLocation} | ||
| defaultValue="" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you assign a more significant name instead of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the default value for the select tag |
||
| styles={{ container: { width: "50%" } }} | ||
| > | ||
| <option value="" disabled hidden> | ||
| Choose a district | ||
| </option> | ||
| <option value="">None</option> | ||
| {[ | ||
| ...clubs.reduce((districts, club) => { | ||
| districts.add(club.district); | ||
| return districts; | ||
| }, new Set()) | ||
| ].map(dist => { | ||
| return ( | ||
| <option key={dist} value={dist}> | ||
| {dist} | ||
| </option> | ||
| ); | ||
| })} | ||
| </Select> | ||
| <Select onChange={handleChangeSortType} defaultValue="location"> | ||
| <option value="location">Location</option> | ||
| <option value="favorites">Favorites</option> | ||
| <option value="name">Name</option> | ||
| </Select> | ||
| </div> | ||
|
|
||
| {clubs ? ( | ||
| clubs | ||
| .map(club => { | ||
| let distance = 0; | ||
| if (club.latitude == null || club.longitude == null) { | ||
| distance = 0; | ||
| } else if (position[0] !== 0) { | ||
| distance = setDistance(club, position); | ||
| } | ||
| club.distance = distance; | ||
| console.log(club); | ||
| return club; | ||
| }) | ||
| .sort(sortBy) | ||
| .map(club => { | ||
| return <Club key={club.id} club={club} />; | ||
| }) | ||
| ) : ( | ||
| <p>Loading...</p> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is the way to get a request from Open Cage Data API