├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html └── manifest.json └── src ├── App.js ├── Firebase.js ├── index.js └── registerServiceWorker.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # See https://help.github.com/ignore-files/ for more about ignoring files. 61 | 62 | # dependencies 63 | /node_modules 64 | 65 | # testing 66 | /coverage 67 | 68 | # production 69 | /build 70 | 71 | # misc 72 | .DS_Store 73 | .env.local 74 | .env.development.local 75 | .env.test.local 76 | .env.production.local 77 | 78 | npm-debug.log* 79 | yarn-debug.log* 80 | yarn-error.log* 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Oscar Barajas Tavares 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-form-firebase 2 | Simple form with Firebase 3 | 4 | ![React Form Firebase](https://s3.amazonaws.com/chewiekie/react-form-firebase.jpg) 5 | 6 | 7 | ### Install Dependencies 8 | ``` 9 | npm install 10 | ``` 11 | 12 | ### Starting the Server 13 | Run the next command to launch the development server. 14 | ``` 15 | npm start 16 | ``` 17 | 18 | ### Building for Production 19 | To build an optimized bundle, run 20 | ``` 21 | npm run build 22 | ``` 23 | 24 | ## Demo 25 | [demo](https://gndx-projects.firebaseapp.com/) 26 | 27 | ## Info 28 | [Create a contact Form with React + Firebase](https://gndx.co/formulario-de-contacto-con-react-firebase/) 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-form-firebase", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "firebase": "^4.6.2", 7 | "react": "^16.1.0", 8 | "react-dom": "^16.1.0", 9 | "react-scripts": "1.0.17" 10 | }, 11 | "scripts": { 12 | "start": "react-scripts start", 13 | "build": "react-scripts build", 14 | "test": "react-scripts test --env=jsdom", 15 | "eject": "react-scripts eject" 16 | }, 17 | "description": "Simple form with Firebase", 18 | "main": "index.js", 19 | "devDependencies": {}, 20 | "repository": { 21 | "type": "git", 22 | "url": "git+https://github.com/gndx/react-form-firebase.git" 23 | }, 24 | "keywords": [ 25 | "react", 26 | "firebase", 27 | "gndx", 28 | "contact", 29 | "form" 30 | ], 31 | "author": "Oscar Barajas ", 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/gndx/react-form-firebase/issues" 35 | }, 36 | "homepage": "https://github.com/gndx/react-form-firebase#readme" 37 | } 38 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gndx/react-form-firebase/71a44f79c38083403751bc97e09e50c9f0f2b227/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 11 | 12 | 13 | 14 | 15 | 24 | Formulario de Contacto con React + Firebase 25 | 26 | 27 | 30 |
31 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /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 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react'; 2 | import firebaseConf from './Firebase'; 3 | 4 | class App extends Component { 5 | 6 | constructor(props) { 7 | super(props); 8 | this.state = { 9 | form: [], 10 | alert: false, 11 | alertData: {} 12 | }; 13 | } 14 | 15 | showAlert(type, message) { 16 | this.setState({ 17 | alert: true, 18 | alertData: { type, message } 19 | }); 20 | setTimeout(() => { 21 | this.setState({ alert: false }); 22 | }, 4000) 23 | } 24 | 25 | resetForm() { 26 | this.refs.contactForm.reset(); 27 | } 28 | 29 | componentWillMount() { 30 | let formRef = firebaseConf.database().ref('form').orderByKey().limitToLast(6); 31 | formRef.on('child_added', snapshot => { 32 | const { name, email, city, phone, message } = snapshot.val(); 33 | const data = { name, email, city, phone, message }; 34 | this.setState({ form: [data].concat(this.state.form) }); 35 | }) 36 | } 37 | 38 | sendMessage(e) { 39 | e.preventDefault(); 40 | const params = { 41 | name: this.inputName.value, 42 | email: this.inputEmail.value, 43 | city: this.inputCity.value, 44 | phone: this.inputPhone.value, 45 | message: this.textAreaMessage.value 46 | }; 47 | if (params.name && params.email && params.phone && params.phone && params.message) { 48 | firebaseConf.database().ref('form').push(params).then(() => { 49 | this.showAlert('success', 'Your message was sent successfull'); 50 | }).catch(() => { 51 | this.showAlert('danger', 'Your message could not be sent'); 52 | }); 53 | this.resetForm(); 54 | } else { 55 | this.showAlert('warning', 'Please fill the form'); 56 | }; 57 | } 58 | 59 | render() { 60 | return ( 61 |
62 | {this.state.alert &&
63 |
64 | {this.state.alertData.message} 65 |
66 |
} 67 |
68 |
69 |
70 |

Contact Form

71 |
72 |
73 | 74 | this.inputName = name} /> 75 |
76 |
77 | 78 | this.inputEmail = email} /> 79 |
80 |
81 | 82 | 87 |
88 |
89 | 90 | this.inputPhone = phone} /> 91 |
92 |
93 | 94 | 95 |
96 | 97 |
98 |
99 |
100 |
101 | {this.state.form.map(form => 102 |
103 |
104 |
105 |

{form.name}

106 |
{form.city}
107 |

{form.message}

108 | {form.phone} 109 | {form.email} 110 |
111 |
112 |
)} 113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 | 124 |
125 |
126 |
127 |
128 | ); 129 | } 130 | } 131 | 132 | export default App; -------------------------------------------------------------------------------- /src/Firebase.js: -------------------------------------------------------------------------------- 1 | 2 | import firebase from 'firebase'; 3 | 4 | const config = { 5 | apiKey: "unreadablestuff", 6 | authDomain: "your-project-name.firebaseapp.com", 7 | databaseURL: "https://your-project-name.firebaseio.com", 8 | projectId: "your-project-name", 9 | storageBucket: "your-project-name.appspot.com", 10 | messagingSenderId: "0112358132134" 11 | }; 12 | 13 | const firebaseConf = firebase.initializeApp(config); 14 | 15 | export default firebaseConf; -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from './App'; 4 | import registerServiceWorker from './registerServiceWorker'; 5 | 6 | ReactDOM.render(, document.getElementById('root')); 7 | registerServiceWorker(); 8 | -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | } else { 39 | // Is not local host. Just register service worker 40 | registerValidSW(swUrl); 41 | } 42 | }); 43 | } 44 | } 45 | 46 | function registerValidSW(swUrl) { 47 | navigator.serviceWorker 48 | .register(swUrl) 49 | .then(registration => { 50 | registration.onupdatefound = () => { 51 | const installingWorker = registration.installing; 52 | installingWorker.onstatechange = () => { 53 | if (installingWorker.state === 'installed') { 54 | if (navigator.serviceWorker.controller) { 55 | // At this point, the old content will have been purged and 56 | // the fresh content will have been added to the cache. 57 | // It's the perfect time to display a "New content is 58 | // available; please refresh." message in your web app. 59 | console.log('New content is available; please refresh.'); 60 | } else { 61 | // At this point, everything has been precached. 62 | // It's the perfect time to display a 63 | // "Content is cached for offline use." message. 64 | console.log('Content is cached for offline use.'); 65 | } 66 | } 67 | }; 68 | }; 69 | }) 70 | .catch(error => { 71 | console.error('Error during service worker registration:', error); 72 | }); 73 | } 74 | 75 | function checkValidServiceWorker(swUrl) { 76 | // Check if the service worker can be found. If it can't reload the page. 77 | fetch(swUrl) 78 | .then(response => { 79 | // Ensure service worker exists, and that we really are getting a JS file. 80 | if ( 81 | response.status === 404 || 82 | response.headers.get('content-type').indexOf('javascript') === -1 83 | ) { 84 | // No service worker found. Probably a different app. Reload the page. 85 | navigator.serviceWorker.ready.then(registration => { 86 | registration.unregister().then(() => { 87 | window.location.reload(); 88 | }); 89 | }); 90 | } else { 91 | // Service worker found. Proceed as normal. 92 | registerValidSW(swUrl); 93 | } 94 | }) 95 | .catch(() => { 96 | console.log( 97 | 'No internet connection found. App is running in offline mode.' 98 | ); 99 | }); 100 | } 101 | 102 | export function unregister() { 103 | if ('serviceWorker' in navigator) { 104 | navigator.serviceWorker.ready.then(registration => { 105 | registration.unregister(); 106 | }); 107 | } 108 | } 109 | --------------------------------------------------------------------------------