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
JIP-20 Filter posts by location #17
Changes from 15 commits
361a5d6
8fae6ad
e53b77c
08160f8
36f074d
36d2f68
f860c26
df23e6a
7cce8c6
c98b9c1
3ec3cca
ea6b358
1f4de52
8a05879
0a26017
d2e3f20
43d9830
d620cfa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { | |
} from "@react-google-maps/api"; | ||
import { CSSProperties } from "@material-ui/styles"; | ||
import { Libraries } from "@react-google-maps/api/dist/utils/make-load-script-url"; | ||
import { Location, setLocation } from "../../redux/slices/location-slice"; | ||
import { Location } from "../../redux/slices/location-slice"; | ||
import { setLocationFilter } from "../../redux/slices/post-slice"; | ||
import REACT_APP_GOOGLE_API_KEY from "../../env"; | ||
import styled from "styled-components"; | ||
import { useAppDispatch, useAppSelector } from "../../redux/store"; | ||
|
@@ -58,15 +59,15 @@ const onAutoCompleteLoad = (autocomplete: any) => { | |
|
||
const Map = () => { | ||
const dispatch = useAppDispatch(); | ||
const location = useAppSelector((state) => state.location); | ||
const location = useAppSelector((state) => state.post.locationFilter); | ||
const updateStore = async ( | ||
lat: number, | ||
lon: number, | ||
newName: string | undefined, | ||
) => { | ||
try { | ||
const response = await dispatch( | ||
setLocation({ | ||
setLocationFilter({ | ||
name: newName !== undefined ? newName : name, | ||
lat: lat, | ||
lon: lon, | ||
|
@@ -84,9 +85,12 @@ const Map = () => { | |
lat: location.lat, | ||
lng: location.lon, | ||
}); | ||
|
||
const [name, setName] = React.useState(location.name); | ||
|
||
if (initialLocation === undefined) { | ||
// TODO: this block is causing getPostsByFilter to be called twice when the page is refreshed | ||
// Another issue: user location is fetched here AND in post-slice | ||
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. Assuming this code block only needs to run once, to address these two issues maybe we could:
We could also keep the location-slice and just have all location-related store logic stay in there (which implies we remove location logic from post-slice) 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. I did it the second way and it fixes most of the problem Just noticed that we also called getPostsByFilter for the initialLocation, but at least the duplicate requests for the detected location are gone 🐵 🐵 |
||
navigator.geolocation.getCurrentPosition(function (position) { | ||
initialLocation = { | ||
lat: position.coords.latitude, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import axios from "axios"; | ||
import { NewPost, Post } from "../redux/slices/post-slice"; | ||
import { NewPost, Post, PostState } from "../redux/slices/post-slice"; | ||
|
||
const baseUrl = "/api/posts"; | ||
|
||
|
@@ -21,6 +21,20 @@ const getAll = async () => { | |
return response; | ||
}; | ||
|
||
const getPostsByFilter = async (postState: PostState) => { | ||
const { posts, sortType, locationFilter, tagFilter, currentPostID } = | ||
postState; | ||
const response = await axios.get( | ||
`${baseUrl}?` + | ||
`sort=${sortType}&` + | ||
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. axios does query params for you, just pass all of that inside an object with a You don't even have to worry about url encoding each component |
||
`lat=${locationFilter.lat}&` + | ||
`lon=${locationFilter.lon}&` + | ||
`tag=${tagFilter}&` + | ||
`id=${currentPostID}`, | ||
); | ||
return response; | ||
}; | ||
|
||
const getByID = async (id: string) => { | ||
const response = await axios.get(`${baseUrl}/${id}`); | ||
return response; | ||
|
@@ -39,6 +53,7 @@ const deleteByID = async (id: string) => { | |
const postService = { | ||
create, | ||
getAll, | ||
getPostsByFilter, | ||
getByID, | ||
update, | ||
deleteByID, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,7 @@ class DataGenerator { | |
const rawLocations = fs.readFileSync(this.locationPath); | ||
this.mockLocations = JSON.parse(rawLocations).map((location) => ({ | ||
type: "Point", | ||
coordinates: [location.lat, location.lng], | ||
coordinates: [location.lng, location.lat], | ||
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. Wack literally everywhere else its lat first. |
||
})); | ||
const rawComments = fs.readFileSync(this.commentsPath); | ||
this.mockComments = JSON.parse(rawComments); | ||
|
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.
Should we just keep lat/lon in the store rather than both?
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.
I wanted to but I had an issue with needing the map to update the post-list when I tried keeping it in the store 🐵