Skip to content

Commit

Permalink
load data from RTDB
Browse files Browse the repository at this point in the history
  • Loading branch information
yianL committed Aug 5, 2023
1 parent 0d420cb commit 4a653d3
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ firebase-debug.log*
firestore-debug.log*
ui-debug.log*
firebase-config.json
database-debug.log

File renamed without changes.
6 changes: 6 additions & 0 deletions database.rules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rules": {
".read": "true",
".write": "false"
}
}
3 changes: 3 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@
"enabled": true
},
"singleProjectMode": true
},
"database": {
"rules": "database.rules.json"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"web-vitals": "^2.1.0"
},
"scripts": {
"start": "react-scripts start",
"start": "source .env && react-scripts start",
"firebase-start": "firebase emulators:start",
"build": "react-scripts build",
"test": "react-scripts test",
Expand Down
40 changes: 10 additions & 30 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,21 @@ import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./components/App";
import { Signature } from "./utils/constants";

import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";

const fetchData = async () => {
try {
const response = await fetch(`${process.env.PUBLIC_URL}/data.json`);
if (response.status >= 400) {
throw new Error("Bad response from server");
}
return await response.json();
} catch (error) {
console.error(error);
}
};

const initFirebase = async () => {
const FirebaseConfig = process.env.REACT_APP_FIREBASE_CONFIG_JSON
? JSON.parse(process.env.REACT_APP_FIREBASE_CONFIG_JSON)
: await (await fetch("/__/firebase/init.json")).json();
const app = initializeApp(FirebaseConfig);
// Initialize Analytics
getAnalytics(app);
};
import { onValue } from "./utils/database";
import { initFirebase } from "./utils/firebase";

(async function init() {
await initFirebase();

const data = await fetchData();

console.log(Signature, "color: blue; font-weight: bold");
console.log("Quick start: yian.skills.map(s => s.name).join(', ')");
window.yian = { ...data };

await initFirebase();

const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement
);
root.render(<App data={data} version={process.env.REACT_APP_VERSION} />);

onValue("profile", (data) => {
window.yian = { ...data };
root.render(<App data={data} version={process.env.REACT_APP_VERSION} />);
});
})();
31 changes: 31 additions & 0 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
getDatabase,
ref,
connectDatabaseEmulator,
Database,
onValue as _onValue,
} from "firebase/database";

let db: Database;

export const getDB = (): Database => {
if (db) {
return db;
}

db = getDatabase();

if (process.env.REACT_APP_DEV_MODE) {
// Point to the RTDB emulator running on localhost.
connectDatabaseEmulator(db, "127.0.0.1", 9000);
}

return db;
};

export const onValue = (path: string, callback: (snapshot: any) => void) => {
const starCountRef = ref(getDB(), path);
return _onValue(starCountRef, (snapshot) => {
callback(snapshot.val());
});
};
11 changes: 11 additions & 0 deletions src/utils/firebase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";

export const initFirebase = async () => {
const FirebaseConfig = process.env.REACT_APP_FIREBASE_CONFIG_JSON
? JSON.parse(process.env.REACT_APP_FIREBASE_CONFIG_JSON)
: await (await fetch("/__/firebase/init.json")).json();
const app = initializeApp(FirebaseConfig);
getAnalytics(app);
return app;
};

0 comments on commit 4a653d3

Please sign in to comment.