Skip to content
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

Feature/geolocate region #8

Merged
merged 8 commits into from
Jun 26, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ preprocessing/data-import/consolidatedCsv.csv
data/data++
tmp
preprocessing/geodata/maps/dist/mou/*.geojson
firebase-functions/geodata/
firebase-functions/static/
9 changes: 9 additions & 0 deletions app/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# local deployment
# firebase emulators:start

# VITE_APP_BASE_PATH="/iarsenic-staging/us-central1/default" # staging
# VITE_APP_BASE_PATH="/iarsenic/us-central1/default" # production

# firebase deploy

# VITE_APP_BASE_PATH="/default" # staging & production
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

.env
File renamed without changes.
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@mui/icons-material": "^5.15.14",
"@mui/material": "^5.15.14",
"@mui/styled-engine-sc": "^6.0.0-alpha.18",
"dotenv": "^16.4.5",
"react": "^18.2.0",
"react-d3-speedometer": "^2.2.1",
"react-dom": "^18.2.0",
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions app/src/config.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
basePath: import.meta.env.VITE_APP_BASE_PATH || '',
};
File renamed without changes.
File renamed without changes.
173 changes: 173 additions & 0 deletions app/src/pages/Region/GeolocationButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { Button, CircularProgress } from "@mui/material";
import { useEffect, useState } from "react";
import MyLocationIcon from '@mui/icons-material/MyLocation';
import ErrorOutlineIcon from '@mui/icons-material/ErrorOutline';
import { DropdownDistrict, DropdownDivision, DropdownUnion, DropdownUpazila, RegionKey } from "../../types";
import config from "../../config";

type props = {
dropdownData: DropdownDivision[],
setSelectedDivision: (selectedDivision: DropdownDivision) => void,
setSelectedDistrict: (selectedDistrict: DropdownDistrict) => void,
setSelectedUpazila: (selectedUpazila: DropdownUpazila) => void,
setSelectedUnion: (selectedUnion: DropdownUnion) => void,
setSelectedMouza: (selectedMouza: string) => void,
}

export default function GeolocationButton({
dropdownData,
setSelectedDivision,
setSelectedDistrict,
setSelectedUpazila,
setSelectedUnion,
setSelectedMouza,
}: props): JSX.Element {
const [geolocation, setGeolocation] = useState<[number, number]>();
const [geolocationFailed, setGeolocationFailed] = useState<boolean>(false);
const [gettingRegionKey, setGettingRegionKey] = useState<boolean>(false);
const [regionNotFound, setRegionNotFound] = useState<boolean>(false);

function getGeolocation() {
setGettingRegionKey(true);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
console.log(`Latitude: ${latitude}, Longitude: ${longitude}`);
console.log(`${latitude}, ${longitude}`);
setGeolocation([latitude, longitude]);
},
(error) => {
console.error("Error getting geolocation: ", error);
setGeolocationFailed(true);
}
);
} else {
setGeolocationFailed(true);
console.error("Geolocation is not supported by this browser.");
}
}

async function setRegionFromGeolocation(geolocation: [number, number], dropdownData: DropdownDivision[]) {
const response = await fetch(
`${config.basePath}/api/gps-region?lat=${geolocation[0]}&lon=${geolocation[1]}`
);

if (response.status === 404) {
setGettingRegionKey(false);
setRegionNotFound(true);
return;
}

if (!response.ok) {
console.error("Error getting region from geolocation");
setGeolocationFailed(true);
setGettingRegionKey(false);
return;
}

const data = await response.json();

if (!data.regionKey) return;

const geoRegionKey = data.regionKey as RegionKey;
if (!geoRegionKey.division || !dropdownData) return;
const selectedDivision = dropdownData.find(d => d.division === geoRegionKey.division);

if (!selectedDivision) {
setGettingRegionKey(false);
return;
}
setSelectedDivision(selectedDivision);

const selectedDistrict = selectedDivision.districts.find(d => d.district === geoRegionKey.district);

if (!selectedDistrict) {
setGettingRegionKey(false);
return;
}
setSelectedDistrict(selectedDistrict);

const selectedUpazila = selectedDistrict.upazilas.find(u => u.upazila === geoRegionKey.upazila);

if (!selectedUpazila) {
setGettingRegionKey(false);
return;
}
setSelectedUpazila(selectedUpazila);

const selectedUnion = selectedUpazila.unions.find(u => u.union === geoRegionKey.union);

if (!selectedUnion) {
setGettingRegionKey(false);
return;
}
setSelectedUnion(selectedUnion);

if (!geoRegionKey.mouza) {
setGettingRegionKey(false);
return;
}
setSelectedMouza(geoRegionKey.mouza);
setGettingRegionKey(false);
}

useEffect(() => {
if (!geolocation) return;

setRegionFromGeolocation(geolocation, dropdownData);
}, [geolocation]);

if (geolocationFailed) {
return (
<Button
sx={{ height: '3rem' }}
variant='outlined'
startIcon={<ErrorOutlineIcon />}
onClick={getGeolocation}
disabled={true}
>
Geolocation not supported
</Button>
);
}

if (gettingRegionKey) {
return (
<Button
sx={{ height: '3rem' }}
variant='outlined'
startIcon={<CircularProgress size={20} />}
onClick={getGeolocation}
disabled={true}
>
Getting Region
</Button>
);
}

if (regionNotFound) {
return (
<Button
sx={{ height: '3rem' }}
variant='outlined'
startIcon={<ErrorOutlineIcon />}
onClick={getGeolocation}
disabled={true}
>
Region not found
</Button>
);
}

return (
<Button
sx={{ height: '3rem' }}
variant='outlined'
startIcon={<MyLocationIcon />}
onClick={getGeolocation}
>
Use Geolocation
</Button>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DropdownDistrict, DropdownDivision, DropdownUnion, DropdownUpazila, Reg
import PredictorsStorage from "../../utils/PredictorsStorage";
import EnglishRegionSelector from "./EnglishRegionSelector";
import BengaliRegionSelector from "./BengaliRegionSelector";
import GeolocationButton from "./GeolocationButton";

export type RegionErrors = {
division: boolean;
Expand Down Expand Up @@ -56,6 +57,7 @@ export default function Region(): JSX.Element {
setRegionTranslations(data);
}


useEffect(() => {
fetchDropdownData();
fetchRegionTranslations();
Expand All @@ -73,6 +75,15 @@ export default function Region(): JSX.Element {
<>
<Typography alignSelf='center' variant="h4">Region</Typography>

<GeolocationButton
dropdownData={dropdownData}
setSelectedDivision={setSelectedDivision}
setSelectedDistrict={setSelectedDistrict}
setSelectedUpazila={setSelectedUpazila}
setSelectedUnion={setSelectedUnion}
setSelectedMouza={setSelectedMouza}
/>

<EnglishRegionSelector
dropdownData={dropdownData}
selectedDivision={selectedDivision}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions app/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';
import { config as dotenvConfig } from 'dotenv';
import { resolve } from 'path';

// Load environment variables from .env file
dotenvConfig({ path: resolve(__dirname, '.env') });

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
base: process.env.VITE_APP_BASE_PATH || '',
});
1 change: 0 additions & 1 deletion docs/CNAME

This file was deleted.

1 change: 0 additions & 1 deletion docs/assets/index-BJWJaWaw.css

This file was deleted.

Loading
Loading