Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions bs3/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ import { Outlet } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { setLocation } from './app/geolocatorSlice';
import useGeoLocationCheck from './api/geolocation';
import { useEffect } from 'react';

function App() {

const dispatch = useDispatch();
const geoLocation = useGeoLocationCheck();
dispatch(setLocation(geoLocation));
const geoLocation = useGeoLocationCheck(); // Custom hook

// Use useEffect to dispatch the action only when geoLocation is valid
useEffect(() => {
if (geoLocation) {
dispatch(setLocation(geoLocation));
}
}, [dispatch, geoLocation]);

return (
<div className="App">
Expand Down
6 changes: 5 additions & 1 deletion bs3/src/app/geolocatorSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { createSlice } from '@reduxjs/toolkit';
export const geolocationSlice = createSlice({
name: 'geolocation',
initialState: {
location: null,
location: {
loaded: false,
coordinates: { lat: null, lng: null },
local: false,
}
},
reducers: {
setLocation: (state, action) => {
Expand Down