Skip to content

Commit

Permalink
Use message statefor snackbar alert
Browse files Browse the repository at this point in the history
  • Loading branch information
borsemayur2 committed May 14, 2021
1 parent 2e46b91 commit 2570494
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/weather/components/LocationSetter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import MyLocationIcon from "@material-ui/icons/MyLocation";
import { IconButton } from "@material-ui/core";

export default function LocationSetter() {
const [error, setError] = useState();
const [message, setMessage] = useState();

const dispatch = useDispatch();

Expand All @@ -22,12 +22,18 @@ export default function LocationSetter() {
);
};
const onError = () => {
setError("Error while fetching location. Please allow location access");
setMessage({
content: "Error while fetching location. Please allow location access",
severity: "error",
});
};
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
} else {
setError("Location not available.");
setMessage({
content: "Location not available.",
severity: "info",
});
}
};

Expand All @@ -37,7 +43,12 @@ export default function LocationSetter() {

return (
<div>
{error && <MessageSnackbar message={error} severity="error" />}
{message && (
<MessageSnackbar
message={message.content}
severity={message.severity}
/>
)}
<IconButton onClick={requestLatLong}>
<MyLocationIcon />
</IconButton>
Expand Down

0 comments on commit 2570494

Please sign in to comment.