Skip to content

Commit d48b270

Browse files
authored
Firebase Working + Project Structure (#4)
* NPM Init * Small refactoring * Project Structure and Firebase * Renaming AddToDB
1 parent 4170674 commit d48b270

16 files changed

Lines changed: 1251 additions & 70 deletions

my-app/.firebaserc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"projects": {
3+
"default": "findmynextcourse"
4+
}
5+
}

my-app/README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
1-
# React + Vite
1+
Project Structure Draft:
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
```
4+
.
5+
├── firebase.js // handles fetching and saving to the realtime database on firebase
6+
├── firebase.json // used for firebase deploy
7+
├── index.html // the main index page - contains header and loads the index.jsx
8+
├── package.json // used for nodejs (npm) packages
9+
├── package-lock.json // used for nodejs (npm) packages
10+
├── README.md // this file ^^
11+
├── src // contains all source filed
12+
│   ├── assets // content, e.g. pictures, ressources etc.
13+
│   │   └── react.svg
14+
│   ├── index.jsx // the react router - routes between pages, all pages are inserted here
15+
│   ├── model.js // the model - handles prog. logic, that is either global or account specific
16+
│   ├── pages // pages combine the presenters to a webpage. mby obsolete for 1 page project
17+
│   │   └── HomeRoot.jsx // The future homepage?
18+
│   ├── presenters // Presenters link views and the model.
19+
│   | Hooks to modify the model & component state is defined here
20+
│   │   └── HandleSearchPresenter.jsx // An example presenter
21+
│   ├── styles.css // a style document - mby one for each view?
22+
│   └── views // views define parts of pages cosmetically.
23+
│   └── DummyView.jsx
24+
└── vite.config.js
25+
```
426

5-
Currently, two official plugins are available:
627

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
9-
10-
## Expanding the ESLint configuration
11-
12-
If you are developing a production application, we recommend using TypeScript and enable type-aware lint rules. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.

my-app/firebase.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { initializeApp } from "firebase/app";
2+
import { getAuth, GoogleAuthProvider, onAuthStateChanged } from "firebase/auth";
3+
import { get, getDatabase, ref, set, serverTimestamp } from "firebase/database";
4+
5+
// Your web app's Firebase configuration
6+
const firebaseConfig = {
7+
apiKey: "AIzaSyCBckVI9nhAP62u5jZJW3F4SLulUv7znis",
8+
authDomain: "findmynextcourse.firebaseapp.com",
9+
databaseURL: "https://findmynextcourse-default-rtdb.europe-west1.firebasedatabase.app",
10+
projectId: "findmynextcourse",
11+
storageBucket: "findmynextcourse.firebasestorage.app",
12+
messagingSenderId: "893484115963",
13+
appId: "1:893484115963:web:59ac087d280dec919ccd5e"
14+
};
15+
16+
17+
// Initialize Firebase
18+
const app = initializeApp(firebaseConfig);
19+
export const auth = getAuth(app);
20+
export const db = getDatabase(app);
21+
// export const googleProvider = new GoogleAuthProvider();
22+
// googleProvider.addScope("profile");
23+
// googleProvider.addScope("email");
24+
25+
// fetches all relevant information to create the model
26+
async function firebaseToModel(model) {
27+
const courses = await fetchCourses();
28+
model.setCourses(courses);
29+
}
30+
31+
export function connectToFirebase(model) {
32+
// onAuthStateChanged(auth, (user) => {
33+
// model.setUser(user);
34+
// }); !can be used for auth!
35+
36+
firebaseToModel(model);
37+
}
38+
39+
export async function addCourse(course){
40+
if(!course?.courseCode)
41+
return;
42+
const myRef = ref(db, `courses/${course.courseCode}`);
43+
await set(myRef, course);
44+
}
45+
46+
export async function fetchCourses() {
47+
const myRef = ref(db, `courses`);
48+
const snapshot = await get(myRef);
49+
if (!snapshot.exists()) return [];
50+
const value = snapshot.val();
51+
const courses = [];
52+
for (const id of Object.keys(courses)) {
53+
courses = [...courses, courses[id]];
54+
}
55+
return courses;
56+
}

my-app/firebase.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"hosting": {
3+
"public": "dist",
4+
"ignore": [
5+
"firebase.json",
6+
"**/.*",
7+
"**/node_modules/**"
8+
]
9+
}
10+
}

my-app/index.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8" />
5-
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7-
<link href="/src/styles.css" rel="stylesheet">
8-
<title>Vite + React</title>
9-
</head>
10-
<body>
11-
<h1 class="text-3xl font-bold underline">
12-
Hello world!
13-
</h1>
14-
</body>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React</title>
8+
</head>
9+
<div id="root"></div>
10+
<script type="module" src="/src/index.jsx"></script>
1511
</html>

0 commit comments

Comments
 (0)