Skip to content

Commit

Permalink
⚙️ Fix: erro de service_id e formato de tempo inválido no card de inf…
Browse files Browse the repository at this point in the history
…os (#107)
  • Loading branch information
Gabriel0109 committed Apr 10, 2024
1 parent 30897fc commit 0c4d315
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
38 changes: 21 additions & 17 deletions src/components/InfoCard/InfoCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { format, parse, addDays } from "date-fns"


export function InfoCard() {
const { setGpsUrl, name, infoLinha, linha } = useContext(CodeContext)
const { setGpsUrl, name, infoLinha, linha, setCancelSearch } = useContext(CodeContext)
const {serviceId} = useContext(ServiceIdContext)
const { routes, isParent, getMultiplePages, plataforms, setRoutes, activateLoader } = useContext(RoutesContext)
const { setTracked, arrivals, setArrivals, setRoutesAndFrequencies, routesAndFrequencies } = useContext(MovingMarkerContext)
Expand Down Expand Up @@ -70,18 +70,21 @@ export function InfoCard() {
);
}
}, [plataforms])

function convertTime(timeString) {
const [hoursStr, minutesStr] = timeString?.split(':');
const hours = parseInt(hoursStr, 10);
const minutes = parseInt(minutesStr, 10);
if(timeString !== null){
const [hoursStr, minutesStr] = timeString?.split(':');
const hours = parseInt(hoursStr, 10);
const minutes = parseInt(minutesStr, 10);

if (hours >= 24) {
const baseDate = new Date('1970-01-01');
const newDate = addDays(baseDate, Math.floor(hours / 24));
return parse(`${format(newDate, 'yyyy-MM-dd')} ${hours % 24}:${minutes}:00`, 'yyyy-MM-dd HH:mm:ss', new Date());
} else {
return parse(`1970-01-01 ${hours}:${minutes}:00`, 'yyyy-MM-dd HH:mm:ss', new Date());
}
if (hours >= 24) {
const baseDate = new Date('1970-01-01');
const newDate = addDays(baseDate, Math.floor(hours / 24));
return parse(`${format(newDate, 'yyyy-MM-dd')} ${hours % 24}:${minutes}:00`, 'yyyy-MM-dd HH:mm:ss', new Date());
} else {
return parse(`1970-01-01 ${hours}:${minutes}:00`, 'yyyy-MM-dd HH:mm:ss', new Date());
}
}
}

return (
Expand All @@ -97,7 +100,7 @@ export function InfoCard() {
</svg>
</button>
{!routes ? <></> : <div className='flex justify-end'>
<button onClick={() => (setRoutes(), setRoutesAndFrequencies(), setTracked(), infoLinha(), setArrivals(), stopFetching(), setGpsUrl(), resetMultiModalTheme())}>
<button onClick={() => (setRoutes(), setRoutesAndFrequencies(), setTracked(), infoLinha(), setArrivals(), stopFetching(), setGpsUrl(), resetMultiModalTheme(), setCancelSearch(true))}>
<GrClose />
</button>
</div>}
Expand Down Expand Up @@ -173,11 +176,12 @@ export function InfoCard() {
</div>
<div className="flex flex-col ml-2.5">
<p className="text-sm">{e.trip_id?.trip_headsign ?? 'Circular'}</p>
{e.trip_id.route_id.route_type === 702 ?
<p className="text-xs">
Funcionamento: {format(convertTime(e.start_time), 'HH:mm')} - {format(convertTime(e.end_time), 'HH:mm')}
</p>
: null}
{e.trip_id.route_id.route_type === 702 && e.start_time && e.end_time ? (
<p className="text-xs">
Funcionamento: {format(convertTime(e.start_time), 'HH:mm')} - {format(convertTime(e.end_time), 'HH:mm')}
</p>
) : null}

</div>
</div>

Expand Down
4 changes: 3 additions & 1 deletion src/hooks/getCode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export function CodeProvider({ children }) {
// COORDENADAS PRA USAR NO SHAPE
const [stopCoords, setStopCoords] = useState()
const [linha, setLinha] = useState(false)
// BACK
const [cancelSearch, setCancelSearch] = useState(false)



Expand Down Expand Up @@ -63,7 +65,7 @@ export function CodeProvider({ children }) {
}

return (
<CodeContext.Provider value={{ code, setCode, setSearchParams, active, setActive, stopId, locationType, setStopId, gpsUrl, setGpsUrl, name, center, stopCoords, infoLinha, linha, setLinha }}>
<CodeContext.Provider value={{ code, setCode, setSearchParams, active, setActive, stopId, locationType, setStopId, gpsUrl, setGpsUrl, name, center, stopCoords, infoLinha, linha, setLinha, cancelSearch, setCancelSearch }}>
{children}
</CodeContext.Provider>
)
Expand Down
12 changes: 9 additions & 3 deletions src/hooks/getRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const RoutesContext = createContext()


export function RoutesProvider({ children }) {
const { code, stopId, locationType } = useContext(CodeContext)
const { code, stopId, locationType, setCancelSearch, cancelSearch } = useContext(CodeContext)
const { serviceId } = useContext(ServiceIdContext)
const { routeType } = useContext(ThemeContext)
const [routes, setRoutes] = useState()
Expand Down Expand Up @@ -49,6 +49,13 @@ export function RoutesProvider({ children }) {
setLoader(true)
setPlataforms([])
}
useEffect(() => {
if (cancelSearch) {
getStations(`/stop_times/?stop_id=${stopId}&service_id=${serviceId}`);
}
}, [cancelSearch])


const rawTrips = [];
const tripPromises = [];

Expand Down Expand Up @@ -79,7 +86,6 @@ export function RoutesProvider({ children }) {
rawTrips[index].lastStop = specificData[specificData.length - 1];
}
});
getStations(`/stop_times/?stop_id=${stopId}&service_id=${serviceId}`);
} else {
const stopTimeResponses = await Promise.all(tripPromises);
stopTimeResponses.forEach((response, index) => {
Expand All @@ -94,6 +100,7 @@ export function RoutesProvider({ children }) {
})
filteredTrips.sort(compareTripName);
setRoutes([...filteredTrips]);
setCancelSearch(false)
}
}

Expand Down Expand Up @@ -124,7 +131,6 @@ export function RoutesProvider({ children }) {
for (let index = 0; index < stopIdsResponses.length; index++) {
const response = stopIdsResponses[index];
const stopTimes = response.data.results;

const tripPromises = stopTimes.map((stopTime) =>
api.get(`/stop_times/?trip_id=${stopTime.trip_id.trip_id}`)
);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/getServiceId.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function ServiceIdProvider({ children }) {
fetchCalendars()
}, [])


let exceptionService = []
let baseService = []
function findService(todayDate) {
Expand Down Expand Up @@ -96,7 +96,7 @@ export function ServiceIdProvider({ children }) {
baseService.push(todayService)

if (todayService && exceptionService) {
const baseService = todayService[0]?.service_id || 'U_REG'
const baseService = todayService.map(item => item.service_id)
const allServices = exceptionService.concat(baseService)
setServiceId(allServices);

Expand Down

0 comments on commit 0c4d315

Please sign in to comment.