Skip to content

Commit

Permalink
initial logic for 'use my current location' button
Browse files Browse the repository at this point in the history
  • Loading branch information
Erw1nn committed Jul 11, 2023
1 parent 0799278 commit efc44a9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/components/AddResourceModal/AddForaging.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function AddForaging({
onNameChange,
address,
onAddressChange,
onAddressClick,
website,
onWebsiteChange,
description,
Expand Down Expand Up @@ -115,6 +116,14 @@ function AddForaging({
}
];

// const addressLinkOnClick = () => {
// const currAddress = getCurrentAddress();

// if (currAddress) {
// address = currAddress;
// }
// };

const {
register,
handleSubmit,
Expand Down Expand Up @@ -221,8 +230,7 @@ function AddForaging({
error={errors.address ? true : false}
FormHelperTextProps={{
sx: { marginLeft: 'auto', marginRight: 0 },
onClick: () =>
alert('Use My Location onClick PlaceHolder!')
onClick: onAddressClick
}}
style={{ backgroundColor: 'white' }}
InputLabelProps={{ shrink: true }}
Expand Down
13 changes: 13 additions & 0 deletions src/components/AddResourceModal/AddResourceModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Popover,
Accordion
} from 'react-bootstrap';

import getCurrentAddress from '../ReactGoogleMaps/ReactGoogleMaps';
import Dialog from '@mui/material/Dialog';
import ImageUploader from 'react-images-upload';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
Expand Down Expand Up @@ -158,6 +160,15 @@ export class AddResourceModal extends Component {
});
}

onClickAddress() {
const currAddressLocation = getCurrentAddress();
if (currAddressLocation) {
this.setState({
address: currAddressLocation
});
}
}

onChangeWebsite(e) {
this.setState({
website: e.target.value
Expand Down Expand Up @@ -552,6 +563,7 @@ export class AddResourceModal extends Component {
onNameChange={this.onNameChange}
address={this.state.address}
onAddressChange={this.onChangeAddress}
onAddressClick={this.onClickAddress}
website={this.state.website}
onWebsiteChange={this.onChangeWebsite}
description={this.state.description}
Expand Down Expand Up @@ -590,6 +602,7 @@ export class AddResourceModal extends Component {
onNameChange={this.onNameChange}
address={this.state.address}
onAddressChange={this.onChangeAddress}
onAddressClick={this.onClickAddress}
website={this.state.website}
onWebsiteChange={this.onChangeWebsite}
description={this.state.description}
Expand Down
23 changes: 22 additions & 1 deletion src/components/ReactGoogleMaps/ReactGoogleMaps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Map, GoogleApiWrapper, Marker } from 'google-maps-react';
import { Map, GoogleApiWrapper, Marker, geocoder } from 'google-maps-react';
import React, { Component } from 'react';
import ReactTouchEvents from 'react-touch-events';
import SearchBar from '../SearchBar/SearchBar';
Expand Down Expand Up @@ -288,6 +288,27 @@ export class ReactGoogleMaps extends Component {
}));
};

getCurrentAddress = () => {
let address = '';
const coord = {
lat: this.state.currlat,
lng: this.state.currlon
};

geocoder
.geocode({ location: coord })
.then(response => {
if (response.results[0]) {
address = response.results[0].formatted_address;
} else {
window.alert('No results found. Try typing the address instead');
}
})
.catch(error => window.alert('Geocoder failed due to: ' + error));

return address;
};

render() {
return (
<div id="react-google-map" className={styles.mapContainer}>
Expand Down

0 comments on commit efc44a9

Please sign in to comment.