Skip to content

Commit

Permalink
fix: estações multimodais aparecendo corretamente
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel0109 committed Mar 23, 2023
1 parent 2c9b1cd commit 425e346
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 205 deletions.
4 changes: 4 additions & 0 deletions src/assets/imgs/whitePin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/CenterButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default function CenterButton({ location }) {
const { stopId } = useContext(CodeContext)
const map = useMap()
function fixCenter() {
console.log(location)
map.setView(location);
map.setZoom(13)

}
useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/Header/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ import { GPSContext } from '../../hooks/getGPS'

export function Header(props) {
let navigate = useNavigate()
const { code, setCode } = useContext(CodeContext)
const { code, setCode, setStopId } = useContext(CodeContext)
const { setTrip } = useContext(TripContext)
const {setPoints} = useContext(ShapeContext)
const {theme, setTheme, setSppo} = useContext(ThemeContext)
const {theme, setTheme, setSppo, setRouteType} = useContext(ThemeContext)
const [newCode, setNewCode] = useState("")
const [value, setValue] = useState('')
const [codeIdentifier, setCodeIdentifier] = useState()
const { setRoutes, setPlataforms, setStopId} = useContext(RoutesContext)
const { setRoutes, setPlataforms} = useContext(RoutesContext)
const { setResults, results, similarNames } = useContext(NameContext)
const {setTracked, setInnerCircle} = useContext(MovingMarkerContext)
const {stopFetching} = useContext(GPSContext)
Expand All @@ -42,7 +42,7 @@ export function Header(props) {
setSppo()
setInnerCircle([])
stopFetching()

setRouteType()
}

useEffect(() => {
Expand Down
117 changes: 60 additions & 57 deletions src/components/InfoCard/InfoCard.jsx

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions src/components/InfoCard/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,38 @@

}

.routeList{
.routeList {

li:last-child{
li:last-child {
border-bottom: none !important;
}

.routeName {
// display: flex;
align-items: center;
// justify-content: space-between;

.shortName {
display: flex;
background-color: var(--items-bg);
border-radius: 3px;
padding: 4px 3px;
p{
color: var(--text-color);
}
}
align-items: center;

.shortName {
display: flex;
border-radius: 3px;
padding: 4px 3px;


}
}
}
.directionBanner{

.directionBanner {
background-color: var(--gray-100);
text-align: center;
padding: 8px;
margin-bottom: 25px;
}
#wifi1,#wifi2, #wifi3{

#wifi1,
#wifi2,
#wifi3 {
opacity: 0;
}

.bg-SPPO {
background-color: #004a80 !important;
}
20 changes: 16 additions & 4 deletions src/hooks/getCode.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import {toast } from 'react-toastify'
import { toast } from 'react-toastify'
import { api } from "../services/api";

export const CodeContext = createContext()
Expand All @@ -13,12 +13,14 @@ export function CodeProvider({ children }) {
const [searchParams, setSearchParams] = useSearchParams();
const [codeExists, setCodeExists] = useState(false)
const [active, setActive] = useState(false)
const [gpsUrl, setGpsUrl] = useState()
const [stopId, setStopId] = useState()
const [locationType, setLocationType] = useState()
const [gpsUrl, setGpsUrl] = useState()


useEffect(() => {
const urlCode = searchParams.get('code');
if(urlCode !== null){
if (urlCode !== null) {
setCode(urlCode.toUpperCase())
}
}, [])
Expand All @@ -42,11 +44,21 @@ export function CodeProvider({ children }) {
})

}

useEffect(() => {
if (code != undefined) {
api
.get("/stops/?stop_code=" + code.toUpperCase())
.then(response => {
setStopId(response.data.results[0].stop_id)
setLocationType(response.data.results[0].location_type)
})
}
checkCode()

}, [code])
return (
<CodeContext.Provider value={{ code, setCode, setSearchParams, codeExists, active, setActive, gpsUrl, setGpsUrl }}>
<CodeContext.Provider value={{ code, setCode, setSearchParams, codeExists, active, setActive, stopId, locationType, setStopId, gpsUrl, setGpsUrl }}>
{children}
</CodeContext.Provider>
)
Expand Down
15 changes: 7 additions & 8 deletions src/hooks/getGPS.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ export function GPSProvider({ children }) {
data.results.forEach((item) => {
allBuses.push(item);
});

if (data.next) {
getGPS(data.next);
} else {
setRealtime([...allBuses]);
allBuses = [];
}
});
}

function startFetching() {
const id = setInterval(() => {
getGPS(gpsUrl);
getGPS(gpsUrl);

const intervalId = setInterval(() => {
getGPS(gpsUrl);
}, 6000);
setIntervalId(id);
setIntervalId(intervalId);
console.log("rodou");
}


function stopFetching() {
clearInterval(intervalId);
setIntervalId(null);
Expand Down
72 changes: 21 additions & 51 deletions src/hooks/getMovingMarkers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export function MovingMarkerProvider({ children }) {
const { setResults } = useContext(NameContext)
const { routes } = useContext(RoutesContext)
const { stopInfo } = useContext(TripContext)
const [tripsShortName, setTripShortName] = useState([])
const [center, setCenter] = useState()
const [radius, setRadius] = useState()
const [tracked, setTracked] = useState([])
Expand All @@ -40,30 +39,7 @@ export function MovingMarkerProvider({ children }) {
setResults()
}, [code])

// NOVA CHAMADA EM STOP_TIMES PRA PODER TER O trip_short_name
// e filtrar os onibus do gps
async function getTripNames(url) {
let tripNames = [];
await api
.get(url)
.then(({ data }) => {
data.results.forEach((item) => {
tripNames.push(item);
});
if (data.next) {
getTripNames(data.next);
}
setTripShortName([...tripNames]);
});
}

useEffect(() => {
if (stopId) {
getTripNames('/stop_times/?stop_id=' + stopId)

}
}, [stopId])


useEffect(() => {
if (realtime && routes) {
let trackedBuses = []
Expand Down Expand Up @@ -97,7 +73,6 @@ export function MovingMarkerProvider({ children }) {
trackedBuses.push(result);
}
});
// if (routes) {
let filteredGPS = trackedBuses.filter(item => {
return routes.some(filterItem => {
if (stopInfo) {
Expand All @@ -109,40 +84,35 @@ export function MovingMarkerProvider({ children }) {
});
setTracked(filteredGPS)
setInnerCircle([])

// }
}
}, [realtime, routes])
}, [realtime])




useEffect(() => {
// Filtrar resultado pelo stop_id selecionado
// Mostrar só 3 primeiros (os 3 primeiros menores tempos maiores q 0)
// TENTAR RELACIONAR SEM O FOR
// usar reduce

if (routes && tracked) {
const arrivals = routes.reduce((acc, obj1) => {
const matched = tracked.filter(obj2 =>
obj1.trip_id.trip_short_name === obj2.linha &&
obj1.trip_id.direction_id === obj2.sentido
);

if (matched.length > 0) {
const sortedMatched = matched.sort((a, b) => a.chegada - b.chegada);
const smallestEtas = sortedMatched.slice(0, 3).map(obj2 => obj2.chegada);
const combinedObj = { ...obj1, smallestEtas };
acc.push(combinedObj);
const arrivals = routes.reduce((acc, obj1) => {
const matched = tracked.filter(obj2 =>
obj1.trip_id.trip_short_name === obj2.linha &&
obj1.trip_id.direction_id === obj2.sentido);

if (matched.length > 0) {
const sortedMatched = matched.sort((a, b) => a.chegada - b.chegada);
const smallestEtas = sortedMatched.slice(0, 3).map(obj2 => obj2.chegada);
const combinedObj = { ...obj1, smallestEtas };
acc.push(combinedObj);
}
return acc;
}, []);
if(arrivals.length === 0){
setArrivals(routes)
} else {
setArrivals(arrivals)
}

return acc;
}, []);

setArrivals(arrivals);
}

}, [routes, tracked])
}, [tracked])


return (
Expand Down
1 change: 0 additions & 1 deletion src/hooks/getName.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createContext, useEffect, useState } from "react";
import { toast } from 'react-toastify'
import { api } from "../services/api";
import { useNavigate } from "react-router-dom";

Expand Down
51 changes: 24 additions & 27 deletions src/hooks/getRoutes.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createContext, useContext, useEffect, useState } from "react";
import { CodeContext } from "./getCode";
import { ThemeContext } from "./getTheme";
import { api } from "../services/api";
import { ServiceIdContext } from "./getServiceId";
export const RoutesContext = createContext()
Expand All @@ -8,30 +9,16 @@ export const RoutesContext = createContext()


export function RoutesProvider({ children }) {
const { code } = useContext(CodeContext)
const { code, stopId, locationType } = useContext(CodeContext)
const {serviceId} = useContext(ServiceIdContext)
const [stopId, setStopId] = useState()
const { routeType } = useContext(ThemeContext)
const [routes, setRoutes] = useState()
const [plataforms, setPlataforms] = useState([])
const [locationType, setLocationType] = useState()
const [stations, setStations] = useState()
const [isParent, setIsParent] = useState()
const [childName, setChildName] = useState()
const [loader, setLoader] = useState()


useEffect(() => {
if (code != undefined) {
api
.get("/stops/?stop_code=" + code.toUpperCase())
.then(response => {
setStopId(response.data.results[0].stop_id)
setChildName(response.data.results[0].stop_name)
setLocationType(response.data.results[0].location_type)
})
}
}, [code])

function compareTripName(a, b) {
const aShortName = a.trip_id.trip_short_name;
const bShortName = b.trip_id.trip_short_name;
Expand Down Expand Up @@ -78,7 +65,6 @@ export function RoutesProvider({ children }) {

} else {
if(locationType === 1){
setLoader(false)
getStations("/stop_times/?stop_id=" + stopId)
}
filteredTrips.sort(compareTripName)
Expand Down Expand Up @@ -111,24 +97,35 @@ export function RoutesProvider({ children }) {
setIsParent(false)
}
}
}, [stopId, locationType])
}, [routeType])



useEffect(() => {
if (locationType != null || locationType != undefined || stations != undefined) {
const iteratee = stations.map((e) => e.stop_id)
const result = iteratee.reduce((acc, curr) => {
acc[curr.stop_desc] = acc[curr.stop_desc] || {};
acc[curr.stop_desc][curr.stop_id] = curr;
return acc;
}, {});
setPlataforms((prevResults) => [...prevResults, result]);
if (routeType) {
if (locationType != null || locationType != undefined || stations != undefined ) {
const iteratee = stations.map((e) => e.stop_id)
const result = iteratee.reduce((acc, curr) => {
if (routeType.includes(3) && routeType.includes(702)) {
acc[curr.platform_code] = acc[curr.platform_code] || {};
acc[curr.platform_code][curr.stop_id] = curr;
return acc;
} else {
acc[curr.stop_desc] = acc[curr.stop_desc] || {};
acc[curr.stop_desc][curr.stop_id] = curr;
return acc;
}

}, {});
setPlataforms((prevResults) => [...prevResults, result]);
}
}
}, [stations]);



return (
<RoutesContext.Provider value={{ routes, stopId, setRoutes, getMultiplePages, isParent, plataforms, setPlataforms, stations, locationType, childName, loader, activateLoader, setStopId}}>
<RoutesContext.Provider value={{ routes, setRoutes, getMultiplePages, isParent, plataforms, setPlataforms, stations, setStations, loader, activateLoader}}>
{children}
</RoutesContext.Provider>
)
Expand Down
Loading

0 comments on commit 425e346

Please sign in to comment.