├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── screenshot.png └── src ├── App.js ├── App.test.js ├── components ├── Layout.js ├── Navbar.js ├── WebsiteCard.js ├── WebsiteForm.js └── WebsiteList.js ├── firebase ├── api.js └── config.js ├── index.css └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### React Firestore CRUD 2 | 3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 4 | 5 | ![](screenshot.png) 6 | 7 | ## Available Scripts 8 | 9 | In the project directory, you can run: 10 | 11 | ### `npm start` 12 | 13 | Runs the app in the development mode.
14 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 15 | 16 | The page will reload if you make edits.
17 | You will also see any lint errors in the console. 18 | 19 | ### `npm test` 20 | 21 | Launches the test runner in the interactive watch mode.
22 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 23 | 24 | ### `npm run build` 25 | 26 | Builds the app for production to the `build` folder.
27 | It correctly bundles React in production mode and optimizes the build for the best performance. 28 | 29 | The build is minified and the filenames include the hashes.
30 | Your app is ready to be deployed! 31 | 32 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 33 | 34 | ### `npm run eject` 35 | 36 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 37 | 38 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 39 | 40 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 41 | 42 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 43 | 44 | ## Learn More 45 | 46 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 47 | 48 | To learn React, check out the [React documentation](https://reactjs.org/). 49 | 50 | ### Code Splitting 51 | 52 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting 53 | 54 | ### Analyzing the Bundle Size 55 | 56 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size 57 | 58 | ### Making a Progressive Web App 59 | 60 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app 61 | 62 | ### Advanced Configuration 63 | 64 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration 65 | 66 | ### Deployment 67 | 68 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment 69 | 70 | ### `npm run build` fails to minify 71 | 72 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-firebase-crud", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.16.1", 7 | "@testing-library/react": "^12.1.2", 8 | "@testing-library/user-event": "^13.5.0", 9 | "bootswatch": "^5.1.3", 10 | "firebase": "^9.6.2", 11 | "react": "^17.0.2", 12 | "react-dom": "^17.0.2", 13 | "react-icons": "^4.3.1", 14 | "react-router-dom": "^6.2.1", 15 | "react-scripts": "5.0.0", 16 | "react-toastify": "^8.1.0" 17 | }, 18 | "scripts": { 19 | "start": "react-scripts start", 20 | "build": "react-scripts build", 21 | "test": "react-scripts test", 22 | "eject": "react-scripts eject" 23 | }, 24 | "eslintConfig": { 25 | "extends": "react-app" 26 | }, 27 | "browserslist": { 28 | "production": [ 29 | ">0.2%", 30 | "not dead", 31 | "not op_mini all" 32 | ], 33 | "development": [ 34 | "last 1 chrome version", 35 | "last 1 firefox version", 36 | "last 1 safari version" 37 | ] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FaztWeb/react-crud-firestore/ead5a42de6cf443e565726302f5ef6d130ea8007/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 18 | 19 | 28 | React App 29 | 30 | 31 | 32 | 33 |
34 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FaztWeb/react-crud-firestore/ead5a42de6cf443e565726302f5ef6d130ea8007/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FaztWeb/react-crud-firestore/ead5a42de6cf443e565726302f5ef6d130ea8007/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/FaztWeb/react-crud-firestore/ead5a42de6cf443e565726302f5ef6d130ea8007/screenshot.png -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import { WebsiteList } from "./components/WebsiteList"; 2 | import { WebsiteForm } from "./components/WebsiteForm"; 3 | import { BrowserRouter, Routes, Route } from "react-router-dom"; 4 | import Layout from "./components/Layout"; 5 | 6 | function App() { 7 | return ( 8 | 9 | 10 | }> 11 | } /> 12 | } /> 13 | } /> 14 | Not Found} /> 15 | 16 | 17 | 18 | ); 19 | } 20 | 21 | export default App; 22 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /src/components/Layout.js: -------------------------------------------------------------------------------- 1 | import { Navbar } from "./Navbar"; 2 | import { ToastContainer } from "react-toastify"; 3 | import "react-toastify/dist/ReactToastify.css"; 4 | import { Outlet } from "react-router-dom"; 5 | 6 | function Layout() { 7 | return ( 8 | <> 9 | 10 |
11 |
12 | 13 |
14 | 15 |
16 | 17 | ); 18 | } 19 | 20 | export default Layout; 21 | -------------------------------------------------------------------------------- /src/components/Navbar.js: -------------------------------------------------------------------------------- 1 | import { Link } from "react-router-dom"; 2 | import { AiOutlineSave } from "react-icons/ai"; 3 | import { SiFirebase } from "react-icons/si"; 4 | 5 | export const Navbar = () => ( 6 | 38 | ); 39 | -------------------------------------------------------------------------------- /src/components/WebsiteCard.js: -------------------------------------------------------------------------------- 1 | import { deleteWebsite } from "../firebase/api"; 2 | import { toast } from "react-toastify"; 3 | import { useNavigate } from "react-router-dom"; 4 | 5 | export function WebsiteCard({ link }) { 6 | const navigate = useNavigate(); 7 | 8 | const onDeleteLink = async (id) => { 9 | if (window.confirm("are you sure you want to delete this link?")) { 10 | await deleteWebsite(id); 11 | toast("Link Removed Successfully", { 12 | type: "error", 13 | autoClose: 2000, 14 | }); 15 | } 16 | }; 17 | 18 | return ( 19 |
navigate(`/edit/${link.id}`)} 23 | > 24 |
25 |
26 |

{link.name}

27 | 36 |
37 |

{link.description}

38 | 39 | Go to Website 40 | 41 |
42 |
43 | ); 44 | } 45 | -------------------------------------------------------------------------------- /src/components/WebsiteForm.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from "react"; 2 | import { toast } from "react-toastify"; 3 | import { saveWebsite, getWebsite, updateWebsite } from "../firebase/api"; 4 | import { useParams, useNavigate } from "react-router-dom"; 5 | 6 | const initialState = { 7 | url: "", 8 | name: "", 9 | description: "", 10 | }; 11 | export const WebsiteForm = (props) => { 12 | const [website, setWebsite] = useState(initialState); 13 | const params = useParams(); 14 | const navigate = useNavigate(); 15 | 16 | const handleInputChange = ({ target: { name, value } }) => 17 | setWebsite({ ...website, [name]: value }); 18 | 19 | const validURL = (str) => { 20 | var pattern = new RegExp( 21 | "^(https?:\\/\\/)?" + // protocol 22 | "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name 23 | "((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address 24 | "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path 25 | "(\\?[;&a-z\\d%_.~+=-]*)?" + // query string 26 | "(\\#[-a-z\\d_]*)?$", 27 | "i" 28 | ); // fragment locator 29 | return !!pattern.test(str); 30 | }; 31 | 32 | const handleSubmit = async (e) => { 33 | e.preventDefault(); 34 | 35 | if (!validURL(website.url)) 36 | return toast("invalid url", { type: "warning", autoClose: 1000 }); 37 | 38 | if (!params.id) { 39 | await saveWebsite(website); 40 | toast("New Link Added", { 41 | type: "success", 42 | }); 43 | } else { 44 | await updateWebsite(params.id, website); 45 | toast("Updated", { 46 | type: "success", 47 | }); 48 | } 49 | 50 | // Clean Form 51 | setWebsite(initialState); 52 | navigate("/"); 53 | }; 54 | 55 | const getLinkById = async (id) => { 56 | try { 57 | const doc = await getWebsite(id); 58 | setWebsite({ ...doc.data() }); 59 | } catch (error) { 60 | console.error(error); 61 | } 62 | }; 63 | 64 | useEffect(() => { 65 | if (params.id) { 66 | getLinkById(params.id); 67 | } 68 | }, [params.id]); 69 | 70 | return ( 71 |
72 |
73 | 74 |
75 |
76 | insert_link 77 |
78 | 86 |
87 | 88 | 89 |
90 |
91 | create 92 |
93 | 101 |
102 | 103 | 104 | 112 | 113 | 119 |
120 |
121 | ); 122 | }; 123 | -------------------------------------------------------------------------------- /src/components/WebsiteList.js: -------------------------------------------------------------------------------- 1 | import { useEffect, useState } from "react"; 2 | import { getWebsites } from "../firebase/api"; 3 | import { WebsiteCard } from "./WebsiteCard"; 4 | 5 | export const WebsiteList = () => { 6 | const [websites, setWebsites] = useState([]); 7 | 8 | const getLinks = async () => { 9 | const querySnapshot = await getWebsites(); 10 | // onGetLinks((querySnapshot) => { 11 | const docs = []; 12 | querySnapshot.forEach((doc) => { 13 | docs.push({ ...doc.data(), id: doc.id }); 14 | }); 15 | setWebsites(docs); 16 | // }); 17 | }; 18 | 19 | useEffect(() => { 20 | getLinks(); 21 | }, []); 22 | 23 | return ( 24 | <> 25 | {websites.map((link) => ( 26 |
27 | 28 |
29 | ))} 30 | 31 | ); 32 | }; 33 | -------------------------------------------------------------------------------- /src/firebase/api.js: -------------------------------------------------------------------------------- 1 | import { 2 | collection, 3 | addDoc, 4 | updateDoc, 5 | onSnapshot, 6 | deleteDoc, 7 | doc, 8 | getDoc, 9 | getDocs, 10 | } from "firebase/firestore"; 11 | import { db } from "./config"; 12 | 13 | const collectionName = "websites"; 14 | 15 | export const saveWebsite = (newLink) => 16 | addDoc(collection(db, collectionName), newLink); 17 | 18 | export const updateWebsite = (id, updatedFields) => 19 | updateDoc(doc(db, collectionName, id), updatedFields); 20 | 21 | export const onGetLinks = (callback) => { 22 | const unsub = onSnapshot(collection(db, collectionName), callback); 23 | return unsub; 24 | }; 25 | 26 | export const getWebsites = () => getDocs(collection(db, collectionName)); 27 | 28 | export const deleteWebsite = (id) => deleteDoc(doc(db, collectionName, id)); 29 | 30 | export const getWebsite = (id) => getDoc(doc(db, collectionName, id)); 31 | -------------------------------------------------------------------------------- /src/firebase/config.js: -------------------------------------------------------------------------------- 1 | import { initializeApp } from "firebase/app"; 2 | import { getFirestore } from "firebase/firestore"; 3 | 4 | // Your web app's Firebase configuration 5 | var firebaseConfig = { 6 | // Paste Your keys here 7 | }; 8 | // Initialize Firebase 9 | initializeApp(firebaseConfig); 10 | 11 | export const db = getFirestore(); 12 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | 15 | .card-website:hover { 16 | background: #121212; 17 | cursor: pointer; 18 | } 19 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import "./index.css"; 4 | import App from "./App"; 5 | import "bootswatch/dist/darkly/bootstrap.min.css"; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById("root") 12 | ); 13 | --------------------------------------------------------------------------------