diff --git a/lib/getStationList.js b/lib/getStationList.js new file mode 100644 index 0000000..8f50a95 --- /dev/null +++ b/lib/getStationList.js @@ -0,0 +1,167 @@ +import 予讃線 from "../assets/四国旅客鉄道予讃線.json"; +import 土讃線 from "../assets/四国旅客鉄道土讃線.json"; +import 高徳線 from "../assets/四国旅客鉄道高徳線.json"; +import 内子線 from "../assets/四国旅客鉄道内子線.json"; +import 徳島線 from "../assets/四国旅客鉄道徳島線.json"; +import 鳴門線 from "../assets/四国旅客鉄道鳴門線.json"; + +let status = undefined; + +export const getStationList = async (props) => { + if (status) return status; + //駅リストイニシャライズ + const HeaderConfig = { + headers: { + referer: "https://train.jr-shikoku.co.jp/sp.html", + }, + }; + return await Promise.all([ + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between", + HeaderConfig + ).then((response) => response.json()), + fetch( + "https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang", + HeaderConfig + ).then((response) => response.json()), + ]).then((values) => { + let stationList = {}; + [ + stationList.予讃線, + stationList.松宇線, + stationList.伊予灘線, + stationList.土讃線, + stationList.窪川線, + stationList.高徳線, + stationList.徳島線, + stationList.鳴門線, + stationList.駅間リスト, + stationList.日英対応表, + ] = values; + const concatBetweenStations = (eachRouteData) => { + let additional = []; + eachRouteData.forEach((routeData, routeIndex) => { + try { + const currentStationID = parseInt( + routeData.StationNumber.replace(/[A-Z]/g, "") + ); + const nextStationID = parseInt( + eachRouteData[routeIndex + 1].StationNumber.replace(/[A-Z]/g, "") + ); + if (nextStationID - currentStationID != 1) { + stationList.駅間リスト.forEach((betweenList) => { + if ( + betweenList.BetweenStation == + routeData.Station_JP + + "~" + + eachRouteData[routeIndex + 1].Station_JP + ) { + additional = additional.concat(betweenList.Datas); + } + }); + } + } catch (e) {} + }); + return eachRouteData + .concat(additional) + .sort((a, b) => (a.StationNumber > b.StationNumber ? 1 : -1)); + }; + const addStationPosition = (setDataBase, geoJson, EnJpList) => { + return setDataBase.map((data) => { + let stationName; + if (data.hasOwnProperty("Station_JP")) stationName = data.Station_JP; + else if (data.hasOwnProperty("StationName")) { + stationName = data.StationName; + data.Station_JP = data.StationName; + data.Station_EN = EnJpList.find( + (d) => d.Station_JP == data.Station_JP + ).Station_EN; + } + geoJson.features + .filter((d) => d.geometry.type == "Point") + .forEach((element) => { + if (element.properties.name == stationName) { + data.lat = element.geometry.coordinates[1]; + data.lng = element.geometry.coordinates[0]; + } + }); + return data; + }); + }; + console.log(stationList.予讃線); + stationList.予讃線 = addStationPosition( + concatBetweenStations(stationList.予讃線), + 予讃線, + stationList.日英対応表 + ); + stationList.松宇線 = addStationPosition( + concatBetweenStations(stationList.松宇線), + 予讃線, + stationList.日英対応表 + ); + stationList.伊予灘線 = addStationPosition( + concatBetweenStations(stationList.伊予灘線), + 予讃線, + stationList.日英対応表 + ); + stationList.土讃線 = addStationPosition( + concatBetweenStations(stationList.土讃線), + 土讃線, + stationList.日英対応表 + ); + stationList.窪川線 = addStationPosition( + concatBetweenStations(stationList.窪川線), + 土讃線, + stationList.日英対応表 + ); + stationList.高徳線 = addStationPosition( + concatBetweenStations(stationList.高徳線), + 高徳線, + stationList.日英対応表 + ); + stationList.徳島線 = addStationPosition( + concatBetweenStations(stationList.徳島線), + 徳島線, + stationList.日英対応表 + ); + stationList.鳴門線 = addStationPosition( + concatBetweenStations(stationList.鳴門線), + 鳴門線, + stationList.日英対応表 + ); + status = stationList; + return stationList; + }); +}; diff --git a/menu.js b/menu.js index cbba030..3702776 100644 --- a/menu.js +++ b/menu.js @@ -47,6 +47,7 @@ import Sign from "./components/駅名表/Sign"; import { UsefulBox } from "./components/atom/UsefulBox"; import { TicketBox } from "./components/atom/TicketBox"; import { TextBox } from "./components/atom/TextBox"; +import { getStationList } from "./lib/getStationList"; export default function Menu(props) { const { @@ -78,159 +79,8 @@ export default function Menu(props) { const [originalStationList, setOriginalStationList] = useState(); useEffect(() => { - //駅リストイニシャライズ - const HeaderConfig = { - headers: { - referer: "https://train.jr-shikoku.co.jp/sp.html", - }, - }; - Promise.all([ - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=yosan", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=uwajima2", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=dosan2", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=koutoku", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=tokushima", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=naruto", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=station&arg2=between", - HeaderConfig - ).then((response) => response.json()), - fetch( - "https://train.jr-shikoku.co.jp/g?arg1=line&arg2=train_lang", - HeaderConfig - ).then((response) => response.json()), - ]).then((values) => { - let stationList = {}; - [ - stationList.予讃線, - stationList.松宇線, - stationList.伊予灘線, - stationList.土讃線, - stationList.窪川線, - stationList.高徳線, - stationList.徳島線, - stationList.鳴門線, - stationList.駅間リスト, - stationList.日英対応表, - ] = values; - - const concatBetweenStations = (eachRouteData) => { - let additional = []; - eachRouteData.forEach((routeData, routeIndex) => { - try { - const currentStationID = parseInt( - routeData.StationNumber.replace(/[A-Z]/g, "") - ); - const nextStationID = parseInt( - eachRouteData[routeIndex + 1].StationNumber.replace(/[A-Z]/g, "") - ); - if (nextStationID - currentStationID != 1) { - stationList.駅間リスト.forEach((betweenList) => { - if ( - betweenList.BetweenStation == - routeData.Station_JP + - "~" + - eachRouteData[routeIndex + 1].Station_JP - ) { - additional = additional.concat(betweenList.Datas); - } - }); - } - } catch (e) {} - }); - return eachRouteData - .concat(additional) - .sort((a, b) => (a.StationNumber > b.StationNumber ? 1 : -1)); - }; - const addStationPosition = (setDataBase, geoJson, EnJpList) => { - return setDataBase.map((data) => { - let stationName; - if (data.hasOwnProperty("Station_JP")) stationName = data.Station_JP; - else if (data.hasOwnProperty("StationName")) { - stationName = data.StationName; - data.Station_JP = data.StationName; - data.Station_EN = EnJpList.find( - (d) => d.Station_JP == data.Station_JP - ).Station_EN; - } - geoJson.features - .filter((d) => d.geometry.type == "Point") - .forEach((element) => { - if (element.properties.name == stationName) { - data.lat = element.geometry.coordinates[1]; - data.lng = element.geometry.coordinates[0]; - } - }); - return data; - }); - }; - stationList.予讃線 = addStationPosition( - concatBetweenStations(stationList.予讃線), - 予讃線, - stationList.日英対応表 - ); - stationList.松宇線 = addStationPosition( - concatBetweenStations(stationList.松宇線), - 予讃線, - stationList.日英対応表 - ); - stationList.伊予灘線 = addStationPosition( - concatBetweenStations(stationList.伊予灘線), - 予讃線, - stationList.日英対応表 - ); - stationList.土讃線 = addStationPosition( - concatBetweenStations(stationList.土讃線), - 土讃線, - stationList.日英対応表 - ); - stationList.窪川線 = addStationPosition( - concatBetweenStations(stationList.窪川線), - 土讃線, - stationList.日英対応表 - ); - stationList.高徳線 = addStationPosition( - concatBetweenStations(stationList.高徳線), - 高徳線, - stationList.日英対応表 - ); - stationList.徳島線 = addStationPosition( - concatBetweenStations(stationList.徳島線), - 徳島線, - stationList.日英対応表 - ); - stationList.鳴門線 = addStationPosition( - concatBetweenStations(stationList.鳴門線), - 鳴門線, - stationList.日英対応表 - ); - + getStationList().then((stationList) => { + console.log(stationList); setOriginalStationList(stationList); }); }, []);