Skip to content

Commit

Permalink
lightRail should respect status field from API
Browse files Browse the repository at this point in the history
  • Loading branch information
desmondlsl committed Oct 26, 2024
1 parent e2b2020 commit 02049f4
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions src/lightRail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,62 @@ export default function fetchEtas({
},
)
.then((response) => response.json())
.then(({ platform_list }) =>
platform_list.reduce(
(acc: Eta[], { route_list, platform_id }: any) => [
...acc,
...route_list
.filter(
({ route_no, dest_ch, dest_en, stop }: any) =>
route === route_no &&
(dest_ch === dest.zh || dest_en.includes("Circular")) &&
stop === 0,
)
.map(({ time_en, train_length }: any) => {
let waitTime = 0;
switch (time_en.toLowerCase()) {
case "arriving":
case "departing":
case "-":
waitTime = 0;
break;
default:
waitTime = parseInt(time_en, 10);
break;
}
const etaDate = new Date(
Date.now() + waitTime * 60 * 1000 + 8 * 3600000,
);
return {
eta:
`${etaDate.getUTCFullYear()}-${`0${
etaDate.getUTCMonth() + 1
}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}` +
`T${`0${etaDate.getUTCHours()}`.slice(
-2,
)}:${`0${etaDate.getMinutes()}`.slice(
-2,
)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`,
remark: {
zh: `${platform_id}號月台 - ${Array(train_length)
.fill("▭")
.join("")}`,
en: `Platform ${platform_id} - ${Array(train_length)
.fill("▭")
.join("")}`,
},
dest: {
zh: "",
en: "",
},
co: "lightRail",
};
}, []),
],
[],
),
.then(({ platform_list, status }) =>
status === 0
? []
: platform_list.reduce(
(acc: Eta[], { route_list, platform_id }: any) => [
...acc,
...route_list
.filter(
({ route_no, dest_ch, dest_en, stop }: any) =>
route === route_no &&
(dest_ch === dest.zh || dest_en.includes("Circular")) &&
stop === 0,
)
.map(({ time_en, train_length }: any) => {
let waitTime = 0;
switch (time_en.toLowerCase()) {
case "arriving":
case "departing":
case "-":
waitTime = 0;
break;
default:
waitTime = parseInt(time_en, 10);
break;
}
const etaDate = new Date(
Date.now() + waitTime * 60 * 1000 + 8 * 3600000,
);
return {
eta:
`${etaDate.getUTCFullYear()}-${`0${
etaDate.getUTCMonth() + 1
}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}` +
`T${`0${etaDate.getUTCHours()}`.slice(
-2,
)}:${`0${etaDate.getMinutes()}`.slice(
-2,
)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`,
remark: {
zh: `${platform_id}號月台 - ${Array(train_length)
.fill("▭")
.join("")}`,
en: `Platform ${platform_id} - ${Array(train_length)
.fill("▭")
.join("")}`,
},
dest: {
zh: "",
en: "",
},
co: "lightRail",
};
}, []),
],
[],
),
)
.catch((e) => {
console.error(e);
Expand Down

0 comments on commit 02049f4

Please sign in to comment.