Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rahaf-aziza-router-firebase #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions router-firebase/rahaf_aziza/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Firebase and routing activity

Steps to follow to start the activity

- Fork and clone
- On your local machine make sure to run `yarn` in order to install the packages (Firebase and React Router is already installed)
- Run `yarn start` to start the development server

In this activity we will build a small website using Firebase and React router

This exercise will have 2 parts, we advice doing them in order(It will make things easier)

## Part 1: React Router

Looking at the components folder, for each component create a route in the App.js and a Link in the Nav.js :

- Home should be accesed using `/`
- About should be accesed using `/about`
- ContactUs should be accesed using `/contact-us`
- Users should be accesed using `/users`
- Nav should always be displayed at all the routes

## Part 2: Firebase

Setup the Firebase firestore and add your configs to the firebaseConfig.js file.
After you setup the Firestore, you will be able to use `db` inside the `Home.js` file in order to communicate with your database.

Inside the Home.js, create a form which takes 3 inputs (Make sure that all the inputs are controlled)

- Username
- Email
- Password

after the user submits this input, add new document to the "users" collection in the firestore.

Inside the Users.js,

- Fetch the "users" collection
- Save the data in a state
- iterate over the state and for each user render the User component
- User component should display all the info for the user

BONUS:

- Add a delete button inside User.js component: When this button is pressed delete the user's document from the database
- Add edit button inside User.js, this allows user to edit a certain user's details and then they can confirm the edit which edits the document in the database too

### Happy hacking :'D
16,020 changes: 16,020 additions & 0 deletions router-firebase/rahaf_aziza/package-lock.json

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions router-firebase/rahaf_aziza/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "firebase-routing-activity",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"firebase": "^8.6.8",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Binary file added router-firebase/rahaf_aziza/public/favicon.ico
Binary file not shown.
46 changes: 46 additions & 0 deletions router-firebase/rahaf_aziza/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l"
crossorigin="anonymous"
/>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
Binary file added router-firebase/rahaf_aziza/public/logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added router-firebase/rahaf_aziza/public/logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions router-firebase/rahaf_aziza/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
3 changes: 3 additions & 0 deletions router-firebase/rahaf_aziza/public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
19 changes: 19 additions & 0 deletions router-firebase/rahaf_aziza/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@import url("https://fonts.googleapis.com/css2?family=KoHo:wght@500;700&display=swap");
html,
body {
font-family: "KoHo", sans-serif;
font-size: 15px;
}
.card {
margin: 10px 0px;
border: 1px solid rgba(227, 227, 227, 1);
border-radius: 9px;
overflow: hidden;
box-shadow: 0 2px 8px rgb(0 0 0 / 10%);
background-color: #fff;
color: #000;
padding: 15px 10px;
}
.container {
margin-top: 30px;
}
22 changes: 22 additions & 0 deletions router-firebase/rahaf_aziza/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import logo from "./logo.svg";
import "./App.css";
import { BrowserRouter as Router, Route } from "react-router-dom";
import About from "./components/About";
import Home from "./components/Home";
import ContactUs from "./components/ContactUs";
import Nav from "./components/Nav";
import Users from "./components/Users";

function App() {
return (
<Router>
<Route path="/" component={Nav} />
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={ContactUs} />
<Route path="/users" component={Users} />
</Router>
);
}

export default App;
8 changes: 8 additions & 0 deletions router-firebase/rahaf_aziza/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { render, screen } from '@testing-library/react';
import App from './App';

test('renders learn react link', () => {
render(<App />);
const linkElement = screen.getByText(/learn react/i);
expect(linkElement).toBeInTheDocument();
});
7 changes: 7 additions & 0 deletions router-firebase/rahaf_aziza/src/components/About.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

const About = () => {
return <div className="container">About us</div>;
};

export default About;
7 changes: 7 additions & 0 deletions router-firebase/rahaf_aziza/src/components/ContactUs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

const ContactUs = () => {
return <div className="container">ContactUs</div>;
};

export default ContactUs;
69 changes: 69 additions & 0 deletions router-firebase/rahaf_aziza/src/components/Home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React, { useEffect, useState } from "react";
// You will be able to use this, when you paste your firestore configs inside the firebaseConfig.js file
import db from "../firebaseConfig";

const Home = () => {
const [inputForm, setinputForm] = useState({
username: "",
password: "",
email: "",
});

function handelInput(event) {
setinputForm({ ...inputForm, [event.target.name]: event.target.value });
}
function sendData(event) {
event.preventDefault();
db.collection("users").add(inputForm);
setinputForm({
username: "",
password: "",
email: "",
});
}

return (
<div class="container">
<form onSubmit={sendData}>
<div class="form-group">
<input
type="text"
name="username"
className="form-control"
onChange={handelInput}
value={inputForm.username}
placeholder="username"
/>
</div>

<div class="form-group">
<input
type="password"
name="password"
className="form-control"
onChange={handelInput}
value={inputForm.password}
placeholder="password"
/>
</div>

<div class="form-group">
<input
type="email"
name="email"
className="form-control"
onChange={handelInput}
value={inputForm.email}
placeholder="email"
/>
</div>
<br />
<button type="submit" class="btn btn-primary">
Submit
</button>
</form>
</div>
);
};

export default Home;
51 changes: 51 additions & 0 deletions router-firebase/rahaf_aziza/src/components/Nav.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from "react";
import { Link } from "react-router-dom";

const Nav = () => {
return (
<div>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<Link to="/" class="nav-link">
Firebase & Route
</Link>
<button
class="navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarNav"
aria-controls="navbarNav"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<Link to="/" class="nav-link">
Home
</Link>
</li>
<li class="nav-item">
<Link to="/about" class="nav-link">
About
</Link>
</li>
<li class="nav-item">
<Link to="/contact" class="nav-link">
Contact Us
</Link>
</li>
<li class="nav-item">
<Link to="/users" class="nav-link">
Users
</Link>
</li>
</ul>
</div>
</nav>
</div>
);
};

export default Nav;
19 changes: 19 additions & 0 deletions router-firebase/rahaf_aziza/src/components/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React, { useEffect, useState } from "react";

const User = ({ id, password, email, username }) => {
return (
<div id={id} className="card">
<p>
<b>Username:</b> {username}
</p>
<p>
<b>Password:</b> {password}
</p>
<p>
<b>email:</b> {email}
</p>
</div>
);
};

export default User;
Loading