Skip to content

Commit

Permalink
Merge pull request #71 from agiledev-students-fall2023/map-feature-3
Browse files Browse the repository at this point in the history
Map feature 3
  • Loading branch information
unfiltered-syrup authored Nov 21, 2023
2 parents 417e195 + a11c2b2 commit 4020937
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
4 changes: 4 additions & 0 deletions front-end/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import PrivacyPolicyPage from './components/settings/PrivacyPolicyPage';
import LoadingScreen from './components/LoadingScreen';
import TutorialComponent from './components/TutorialComponent';
import useDarkMode from './hooks/darkMode';
import { registerService } from './utils/serviceRegister';
import { getUserPos } from './utils/mapUtility';
import './index.css';
import './css/tutorialComponent.css';

Expand Down Expand Up @@ -50,6 +52,8 @@ function App() {
window.addEventListener('keydown', devTools); // add button press even listeners for dev tools

// if first time is null, set it to true
registerService();
getUserPos();
}, []);

useEffect(() => {
Expand Down
25 changes: 24 additions & 1 deletion front-end/src/utils/mapUtility.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios from 'axios';

const nycCoordinates = [
[-73.935242, 40.73061], // Manhattan
[-73.944158, 40.678178], // Brooklyn
Expand Down Expand Up @@ -54,8 +56,9 @@ export function loadGoogleMapsAPI(callback) {
}

export function initializeMap(mapRef, setIsMapLoaded, setMap) {
const center = localStorage.center ? localStorage.center.split(',') : POS_DEFAULT;
const googleMap = new window.google.maps.Map(mapRef.current, {
center: new window.google.maps.LatLng(...POS_DEFAULT),
center: new window.google.maps.LatLng(...center),
zoom: 13,
styles: SIMPLE_MAP,
options: MAP_OPTIONS,
Expand All @@ -67,6 +70,26 @@ export function initializeMap(mapRef, setIsMapLoaded, setMap) {
});
}

export async function getUserPos() {
const url = new URL('mapGetData.php', localStorage.serviceEndpointSub);
url.search = new URLSearchParams({
getSystems: '2',
sortMode: '1',
deviceId: '0',
credentials: '1',
acronymId: localStorage.agencyId,
}).toString();

try {
const response = await axios.get(url);
const data = response.data;
const pos = data && data.lat && data.lng ? `${data.lat},${data.lng}` : POS_DEFAULT.join(',');
localStorage.center = pos;
} catch (error) {
console.error('Error fetching systems:', error);
}
}

export function getCoordinates() {
return nycCoordinates;
}
Expand Down
76 changes: 76 additions & 0 deletions front-end/src/utils/serviceRegister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import axios from 'axios';

const SERVICE_HOME_URL = 'https://passiogo.com/';
const SERVICE_SUB_URL = 'https://nyu.passiogo.com';
const acronymId = 1007;
const FALLBACK = {
softwareVer: 110,
};

// Get service endpoint version.
async function checkSoftwareVersion() {
try {
const response = await axios.get(`${SERVICE_HOME_URL}goServices.php?goWebVer=1`);
const data = response.data;

// console.log('Version', data ? data.ver : data);
localStorage.softwareVer = data.ver;
} catch (error) {
console.error('Error fetching software version:', error);
localStorage.softwareVer = FALLBACK.softwareVer;
}
}

// Generate a pseudo UUID
function generatePseudoUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

// Send a pseudo token to the service endpoint
function savePseudoTokenToServer() {
let token = localStorage.currentToken || `pseudo${localStorage.softwareVer}_${generatePseudoUUID()}`;
saveTokenToServer(token);
}

// Send the current token to the service endpoint
function saveTokenToServer(currentToken) {
if (!currentToken) {
currentToken = localStorage.currentToken || '';
} else {
localStorage.currentToken = currentToken;
}

const queryParams = new URLSearchParams({
register: '1',
deviceId: localStorage.deviceId,
token: currentToken,
platform: 'web',
buildNo: localStorage.softwareVer,
oldToken: localStorage.currentToken || '',
});

const url = `${SERVICE_SUB_URL}/goServices.php?${queryParams.toString()}`;

axios
.get(url)
.then((response) => {
const data = response.data;
// if (parseInt(localStorage.deviceId) !== data.deviceId) {
// console.log(`Changed deviceId ${localStorage.deviceId} to ${data.deviceId}`);
// }
localStorage.deviceId = data.deviceId;
localStorage.removeItem('currentToken');
})
.catch((error) => {
console.error('Error while saving token to server:', error);
});
}

export function registerService() {
localStorage.acronymId = acronymId;
checkSoftwareVersion().then(savePseudoTokenToServer);
}

0 comments on commit 4020937

Please sign in to comment.