The app is nicely crafted and could be used free, even better I've learned afterward, there is an hosted version! That's the one I wanted to use but we were missing one key feature: sending email to inform speaker of the deliberations and provide a way for speakers to confirm their venue.
π‘π‘π‘Open Source Project? Let's make the world π better by contributing...
On a first look, conference-hall is a web app deployed on Google Cloud Platform. The SPA is deployed using Firebase tooling and make use of firestore database. By contributing to the project, I get acquainted to Firebase. Learning something new is cool, sharing it is even better π€ π€©
Time to start a series of blogs post on the FaaS subject. I'd like to explore Google functions as a service but also go broader and see how it is implemented in open source world.
In this first article, I'll share with your how to get started configuring a project from scratch in Firebase and how to deploy it to get a ground project to introduce cloud functions in the next post. Let's get started step by step...
Step 1️⃣: Initialise firebase project
Go to Firebase console and Create a firebase project, let's name ittest
Step 2️⃣: Use Firestore
- In left-hand side menu select Database tab, then click
Create Database
. Follow Firestore documentation if in trouble. The Firebase console UI is quite easy to follow. Note Firestore is still beta at the time of writing. - Choose
Start in test mode
then clickEnabled
button.
items
as below:
Step 3️⃣: Boostrap app
We use create-react-app to get an initial react appnpx create-react-app test-crud
cd test-crud
npm install --save firebase
and then we've added firebase SDK.
Insert firebase config
- We use react-script env variable support
- In
env.local
copy variable from firebase console - In
src/firebase/firebase.js
, read env variable and initialiseconst config = { apiKey: process.env.REACT_APP_API_KEY, authDomain: process.env.REACT_APP_AUTH_DOMAIN, databaseURL: process.env.REACT_APP_DATABASE_URL, projectId: process.env.REACT_APP_PROJECT_ID, storageBucket: process.env.REACT_APP_STARAGE_BUCKET, messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID, }; firebase.initializeApp(config); firebase.firestore().settings(settings);
Step 4️⃣: Add routing
npm install --save react-router-dom
mkdir src/components
touch src/components/create.js
And define route in src/index.js
ReactDOM.render(
<Router>
<div>
<Route exact path='/' component={App} />
<Route path='/create' component={Create} />
</div>
</Router>,
document.getElementById('root')
);
In the root path, we'll display the list of items. In the the Create
component we'll define a form component to add new items to the list.
Step 5️⃣: Access Firestore in the app
Let's define the content ofCreate
component in src/component/index.js
class Create extends Component {
constructor() {
super();
this.ref = firebase.firestore().collection('items'); // [1] retrieve items reference
this.state = {
field1: '',
field2: '',
};
}
onChange = (e) => {
const state = this.state;
state[e.target.name] = e.target.value;
this.setState(state);
};
onSubmit = (e) => {
e.preventDefault();
const { field1, field2 } = this.state;
this.ref.add({ // [2] Insert by using firestore SDK
field1,
field2,
}).then((docRef) => {
this.setState({
field1: '',
field2: '',
});
this.props.history.push("/")
}).catch((error) => {
console.error("Error adding document: ", error);
});
};
render() {
const { field1, field2 } = this.state;
return (
<div>
<div>
<div>
<h3>
Add Item
</h3>
</div>
<div>
<h4>Link to="/" >Items List</Link></h4>
<form onSubmit={this.onSubmit}>
<div>
<label htmlFor="title">field1:</label>
<input type="text" name="field1" value={field1} onChange={this.onChange} />
</div>
<div>
<label htmlFor="title">field2:</label>
<input type="text" name="field2" value={field2} onChange={this.onChange} />
</div>
<button type="submit">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
export default Create;
It seems a lot of code but the key points are [1] and [2] where we use the firestore SDK to add a new item in the database directly from the client app. the call in [2] is going to be revisited in next blog post to make usage of cloud function.
Step 6️⃣: Deploy on firebase
So we build a small test app accessing firestore DB let's deploy it on the cloud with Firebase tooling π !- Start running a production build
$ npm run build
- Install firebase tools
$ npm install -g firebase-tools $ firebase login
- Init function
$ firebase init
- Step 1: Select the Firebase features you want to use: Firestore Hosting. For now we focus only on deploying ie: hosting the app
- Step 2: Firebase command-line interface will pull up your list of Firebase projects, where you pick firebase-crud.
- Step 3: Keep the default for the Database Rules file name and just press enter.
- Step 4: Pay attention to the question about public directory, which is the directory that will be deployed and served by Firebase.
In our case it is
build
, which is the folder where our production build is located. Type “build” and proceed. - Step 5: Firebase will ask you if you want the app to be configured as a single-page app. Say "yes".
- Step 6: Firebase will warn us that we already have
build/index.html
. All fine!
- deploy!
$ firebase deploy ... ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/test-83c1a/overview Hosting URL: https://test-83c1a.firebaseapp.com
Where to go from there?
In this blog post you've seen how to configure and deploy an SPA on firebase and how to set up a Firestore DB. Next blog post, you'll see how to write you first Google Cloud Function. Stay tuned.Tweet
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.