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

Identify and fix issue with API returning empty array #678

Closed
wants to merge 3 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TrainAutoComplete = ({ to }) => {
const selectedService = to ? autoCompleteState.selectedItemTo : autoCompleteState.selectedItem;

const { loading, errorInfo, results, getAutoCompleteResults } = useAutoCompleteAPI(
`/rail/v2/station?q=${encodeURI(trainQuery)}`,
`/rail/v2/stations?q=${encodeURI(trainQuery)}`,
'train',
trainQuery,
to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const TramAutoCompleteResult = (props) => {
if (autoCompleteState.selectedItem.selectedByMap) {
autoCompleteDispatch({ type: 'RESET_SELECTED_SERVICES' });
}

autoCompleteDispatch({
type: 'UDPATE_SELECTED_ITEM',
payload: {
id: result?.atcoCode,
id: result?.naPTAN,
severity: result?.disruptionSeverity || 'success',
stopName: result.name,
operator: 'MML1',
lines: [],
naPTAN: result.naPTAN,
to,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const useAutoCompleteAPI = (apiPath, mode, query, to) => {
)[0];

payload = {
id: result.atcoCode,
id: result.naPTAN,
severity: result.disruptionSeverity || 'none',
stopName: result.name,
operator: result.service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const useGetTramStopByStop = () => {
(response) => {
setLoading(false);
// Update selectedItem.lines with inbetween lines to match the user's input
const lines = response.data?.metroStopsBetween || [];
const lines = response.data || [];

if (lines.length) {
autoCompleteDispatch({
Expand Down Expand Up @@ -77,15 +77,18 @@ const useGetTramStopByStop = () => {
setLoading(true); // Update loading state to true as we are hitting API
startApiTimeout();
axios
.get(`${REACT_APP_API_HOST}/Metro/v2/stopsbetween/${selectedItem.id}/${selectedItemTo.id}`, {
headers: {
'Ocp-Apim-Subscription-Key': REACT_APP_API_KEY,
},
cancelToken: source.current.token, // Set token with API call, so we can cancel this call on unmount
})
.get(
`${REACT_APP_API_HOST}/Metro/v2/stopsbetween/${selectedItem.naPTAN}/${selectedItemTo.naPTAN}`,
{
headers: {
'Ocp-Apim-Subscription-Key': REACT_APP_API_KEY,
},
cancelToken: source.current.token, // Set token with API call, so we can cancel this call on unmount
}
)
.then(handleAutoCompleteApiResponse)
.catch(handleAutoCompleteApiError);
}, [handleAutoCompleteApiResponse, selectedItem.id, selectedItemTo.id, startApiTimeout]);
}, [handleAutoCompleteApiResponse, selectedItem.naPTAN, selectedItemTo.naPTAN, startApiTimeout]);

useEffect(() => {
setErrorInfo(null);
Expand Down
3 changes: 2 additions & 1 deletion src/customHooks/useFilterLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const useFilterLogic = () => {
if (stopsAffected && stopsAffected.length > 0) {
const lineCodes =
selectedItem.lines && selectedItem.lines.length > 0
? selectedItem.lines.map((stop) => stop.atcoCode)
? selectedItem.lines.map((stop) => stop.naPTAN)
: [selectedItem.id, selectedItemTo.id];

return disrItem.stopsAffected.some((el) => lineCodes.indexOf(el.atcoCode) > -1);
Expand Down Expand Up @@ -151,6 +151,7 @@ const useFilterLogic = () => {
}
}
}
console.log('filteredData3', filteredData);

return filteredData;
};
Expand Down
5 changes: 5 additions & 0 deletions src/globalState/AutoCompleteContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const AutoCompleteProvider = (props) => {
routeName: null,
stopName: null,
lines: [],
naPTAN: null,
to: null,
},
selectedItemTo: {
Expand All @@ -35,6 +36,7 @@ export const AutoCompleteProvider = (props) => {
stopName: null,
routeName: null,
lines: [],
naPTAN: null,
to: null,
},
// The selected location is used to store selected roads address
Expand All @@ -43,6 +45,7 @@ export const AutoCompleteProvider = (props) => {
radius: parseInt(getSearchParam('radius'), 10) || null,
lat: getSearchParam('lat') || null,
lon: getSearchParam('lon') || null,
naPTAN: getSearchParam('naPTAN') || '',
},
};

Expand All @@ -68,6 +71,7 @@ export const AutoCompleteProvider = (props) => {
delSearchParam('selectedByMap'); // Delete URL
}

console.log('action.payload12', action.payload);
const item = action.payload.to ? 'selectedItemTo' : 'selectedItem'; // If 'to' exists in payload then make sure we set the correct field
setSearchParam(item, action.payload.id); // Set URL

Expand Down Expand Up @@ -132,6 +136,7 @@ export const AutoCompleteProvider = (props) => {
setSearchParam('lat', action.payload.lat);
setSearchParam('lon', action.payload.lon);
setSearchParam('radius', action.payload.radius);
setSearchParam('naPTAN', action.payload.naPTAN);

return {
...state,
Expand Down