Skip to content

Commit

Permalink
Firebase 연동(2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorious77 committed May 5, 2022
1 parent dcf8572 commit 6df1a8b
Show file tree
Hide file tree
Showing 4 changed files with 791 additions and 15 deletions.
18 changes: 5 additions & 13 deletions src/App.js
@@ -1,23 +1,15 @@
import { useEffect, useState } from "react";
import { fireStore } from "./Firebase";
import InitStation from "./InitStation";

function App() {
const [subwayStation, setSubwayStation] = useState("");
const handleInitStation = async () => {
const stations = InitStation();

const handleOnClick = () => {
console.log("hi");
setSubwayStation("new Station");
console.log(stations);
};

useEffect(() => {
console.log(fireStore);
});

return (
<div className="App">
<button onClick={handleOnClick}>클릭</button>
<div>{subwayStation}</div>
{fireStore._databaseId.projectId}
<button onClick={handleInitStation}>초기화</button>
</div>
);
}
Expand Down
12 changes: 10 additions & 2 deletions src/Firebase.js
@@ -1,5 +1,5 @@
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore/lite";
import { getFirestore, doc, setDoc } from "firebase/firestore";

const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
Expand All @@ -15,4 +15,12 @@ const firebase = initializeApp(firebaseConfig);

const fireStore = getFirestore(firebase);

export { fireStore };
const initStation = async (stations) => {
stations.map(async (station) => {
const stationRef = doc(fireStore, "station", station.station_cd);

await setDoc(stationRef, { ...station, visited: false });
});
};

export { fireStore, initStation };
18 changes: 18 additions & 0 deletions src/InitStation.js
@@ -0,0 +1,18 @@
import StationInfo from "./StationInfo.json";
import { initStation } from "./Firebase";

const filterLine = (line) => {
if (line.length !== 4 || isNaN(parseInt(line.substring(0, 2)))) {
return false;
}

return true;
};

export default async () => {
const result = StationInfo.DATA.filter((station) => {
return filterLine(station.line_num);
});

initStation(result);
};

0 comments on commit 6df1a8b

Please sign in to comment.