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

Issue #1 | Connect app to Firebase #44

Merged
merged 4 commits into from
Dec 22, 2019
Merged
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"firebase": "^7.6.1",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"prettier": "^1.19.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-firestore": "^1.0.1",
"react-scripts": "3.3.0"
},
"scripts": {
Expand Down
24 changes: 6 additions & 18 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import React from 'react';
import logo from './logo.svg';
import './App.css';
import List from './pages/List';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a nitpick: I'm not sure I get the folder structure here. We have two modules that are in pages but they both make up the same page. I would expect a Home component to live in pages, and then AddItem and List would be components.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're totally right. Per the scope of this issue, the folder structure should be much more like you're mentioning.

I was working ahead and trying to work in the routing code that the other pair wrote but hasn't yet merged. In that PR they are routing to two separate pages, so I set this up so that we could have the List and AddItem components be separate pages.

I think lets leave this for now since it'll help give direction to the merge conflicts today.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok, that makes sense 👍

import AddItem from './pages/AddItem';

function App() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
<main>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more bathtub soup 😆

<AddItem />
<List />
</main>
);
}

Expand Down
7 changes: 7 additions & 0 deletions src/components/Loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

const Loading = () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusable loading state ❤️

return <p>loading...</p>;
};

export default Loading;
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { FirestoreProvider } from 'react-firestore';
import { fb as firebase } from './lib/firebase.js';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));
ReactDOM.render(
<FirestoreProvider firebase={firebase}>
<App />
</FirestoreProvider>,
document.getElementById('root'),
);

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down
40 changes: 40 additions & 0 deletions src/pages/AddItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState } from 'react';
import { withFirestore } from 'react-firestore';

const AddItem = ({ firestore }) => {
const [name, setName] = useState('');

// Send the new item to Firebase
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these comments just to bring participants up to speed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. In the past, since many participants are newer to react, we've been really generous with comments so that when they jump in to work on the code, they get a better idea of what each piece does.

const addItem = name => {
firestore.collection('items').add({ name });
};

// The state every time an event happens
const handleChange = event => {
setName(event.target.value);
};

// Handle the click of the Add Item button on the form
const handleSubmit = event => {
event.preventDefault();
addItem(name);
setName('');
};

return (
<form onSubmit={handleSubmit}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice accessible form 💯

<label>
Add Item:
<input
value={name}
placeholder="apples"
type="text"
onChange={handleChange}
/>
</label>
<input type="submit" value="Add Item" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use input vs button for submission?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally using a button by then when I was putzing around in the React docs, I saw their example was using an input and wanted to keep it close to the available docs in case participants wanted to investigate. What would you prefer?

React form docs: https://reactjs.org/docs/forms.html#controlled-components

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would normally say button, but I get where you're coming from. Mirroring the docs will lead to less confusion 👍

</form>
);
};
// Wrap this component in the higher order component withFirestore to directly access the database
export default withFirestore(AddItem);
31 changes: 31 additions & 0 deletions src/pages/List.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react';
import { FirestoreCollection } from 'react-firestore';
import Loading from '../components/Loading';

const List = () => {
return (
<section>
<FirestoreCollection
// Specify the path to the collection you're pulling data from
path="items"
// Sort the data
sort="name"
// isLoading = is a Boolean that represents the loading status for the firebase query. true until an initial payload from Firestore is received.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As earlier, are these comments to be breadcrumbs for the next team picking up the next issue?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! ❤️

// data = an Array containing all of the documents in the collection. Each item will contain an id along with the other data contained in the document.
render={({ isLoading, data }) => {
return isLoading ? (
<Loading />
) : (
<ul>
{data.map(item => (
<li key={item.id}>{item.name}</li>
))}
</ul>
);
}}
/>
</section>
);
};

export default List;
Loading