lodアドレス追加に伴うお気に入り機能のマイグレーション
This commit is contained in:
parent
ff9faf939e
commit
bbc7db6e69
@ -1,11 +1,13 @@
|
||||
import React, { FC, useEffect } from "react";
|
||||
import { Text } from "react-native";
|
||||
import { useFavoriteStation } from "../../stateBox/useFavoriteStation";
|
||||
type Props = {
|
||||
currentStation: any[];
|
||||
isMatsuyama: boolean;
|
||||
};
|
||||
export const AddressText: FC<Props> = (props) => {
|
||||
const { currentStation, isMatsuyama } = props;
|
||||
const {lodAddMigration} = useFavoriteStation();
|
||||
const [stationAddress, setStationAddress] = React.useState("");
|
||||
useEffect(() => {
|
||||
if (!!currentStation[0].jslodApi) {
|
||||
@ -18,6 +20,8 @@ export const AddressText: FC<Props> = (props) => {
|
||||
][0]["value"];
|
||||
setStationAddress(c);
|
||||
});
|
||||
}else{
|
||||
lodAddMigration();
|
||||
}
|
||||
}, [currentStation]);
|
||||
return (
|
||||
|
@ -6,9 +6,11 @@ import React, {
|
||||
useLayoutEffect,
|
||||
} from "react";
|
||||
import { AS } from "../storageControl";
|
||||
import { useStationList } from "./useStationList";
|
||||
const initialState = {
|
||||
favoriteStation: [],
|
||||
setFavoriteStation: () => {},
|
||||
lodAddMigration: () => {},
|
||||
};
|
||||
|
||||
const FavoriteStationContext = createContext(initialState);
|
||||
@ -19,6 +21,13 @@ export const useFavoriteStation = () => {
|
||||
|
||||
export const FavoriteStationProvider = ({ children }) => {
|
||||
const [favoriteStation, setFavoriteStation] = useState([]);
|
||||
const { getStationData } = useStationList();
|
||||
const lodAddMigration = () => {
|
||||
const migration = favoriteStation.map((d) => {
|
||||
return getStationData(d[0].Station_JP);
|
||||
});
|
||||
setFavoriteStation(migration);
|
||||
};
|
||||
useEffect(() => {
|
||||
AS.getItem("favoriteStation")
|
||||
.then((d) => {
|
||||
@ -29,7 +38,7 @@ export const FavoriteStationProvider = ({ children }) => {
|
||||
}, []);
|
||||
return (
|
||||
<FavoriteStationContext.Provider
|
||||
value={{ favoriteStation, setFavoriteStation }}
|
||||
value={{ favoriteStation, setFavoriteStation, lodAddMigration }}
|
||||
>
|
||||
{children}
|
||||
</FavoriteStationContext.Provider>
|
||||
|
@ -12,10 +12,12 @@ import { getStationList } from "../lib/getStationList";
|
||||
type initialStateType = {
|
||||
originalStationList: any[];
|
||||
setOriginalStationList: React.Dispatch<React.SetStateAction<any[]>>;
|
||||
getStationData: (id: string) => void;
|
||||
};
|
||||
const initialState = {
|
||||
originalStationList: [],
|
||||
setOriginalStationList: () => {},
|
||||
getStationData: () => {},
|
||||
};
|
||||
|
||||
const StationListContext = createContext<initialStateType>(initialState);
|
||||
@ -31,10 +33,22 @@ export const StationListProvider: FC<Props> = ({ children }) => {
|
||||
useEffect(() => {
|
||||
getStationList().then(setOriginalStationList);
|
||||
}, []);
|
||||
const getStationData: (name: string) => void = (name) => {
|
||||
const returnArray = [];
|
||||
Object.keys(originalStationList).forEach((key) => {
|
||||
originalStationList[key].forEach((station) => {
|
||||
if (station.Station_JP === name) {
|
||||
if(!!station.jslodApi)returnArray.push(station);
|
||||
}
|
||||
});
|
||||
});
|
||||
return returnArray;
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
<StationListContext.Provider
|
||||
value={{ originalStationList, setOriginalStationList }}
|
||||
value={{ originalStationList, setOriginalStationList, getStationData }}
|
||||
>
|
||||
{children}
|
||||
</StationListContext.Provider>
|
||||
|
Loading…
Reference in New Issue
Block a user