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

He rb new list setup #47

Merged
merged 7 commits into from
Feb 11, 2020
Merged
12 changes: 0 additions & 12 deletions src/AddItem/AddItem.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/AddItem/AddItem.module.css

This file was deleted.

24 changes: 19 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import React, { useState } from "react";
import { Switch, Route, BrowserRouter } from "react-router-dom";
import "./App.css";
import List from "./List/List";
import AddItem from "./AddItem/AddItem";
import Footer from "./Footer/Footer";
import Header from "./Header/Header";
import Item from "./Item";
import ItemList from "./List/ItemList";
import getToken from "./lib/token";
import Form from "./Form/Form";

function App() {
const [isListSelected, setIsListSelected] = useState(true);
const [isAddSelected, setIsAddSelected] = useState(false);
const [token, setToken] = useState("");

const selectListHandler = () => {
setIsListSelected(true);
Expand All @@ -21,16 +22,29 @@ function App() {
setIsAddSelected(true);
};

const generateTokenHandler = () => {
let newToken = getToken();
setToken(newToken);
localStorage.setItem(newToken, newToken);
};

const enterTokenHandler = e => {
setToken(e.target.value);
};

return (
<BrowserRouter>
<Header isAddSelected={isAddSelected} onListSelect={selectListHandler} />
<div className="App">
<Switch>
<Route exact path="/">
<List />
<button onClick={generateTokenHandler}>Get your list token!</button>
<p>{token}</p>
<p>Or enter an existing token to see your list below.</p>
<ItemList token={token} onEnterToken={enterTokenHandler} />
</Route>
<Route exact path="/add">
<AddItem />
<Form token={token} onEnterToken={enterTokenHandler} />
</Route>
</Switch>
</div>
Expand Down
29 changes: 23 additions & 6 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import React, { useState } from "react";
import classes from "./Form.module.css";
import firebase from "firebase/app";
import * as firebase from "../lib/firebase";

const Form = () => {
const Form = props => {
const [name, setName] = useState("");
const [frequency, setFrequency] = useState(0);
const [date, setDate] = useState("");

const handleSubmit = e => {
e.preventDefault();
let db = firebase.firestore();
let db = firebase.fb.firestore();
let data = {
name,
frequency,
date
};
db.collection("userThree").add(data);
// userThree will be replaced by user token
// a new doc is created with each submission.
db.collection(props.token)
.add(data)
.then(function() {
alert("Item added!");
})
.catch(function(error) {
alert("Something went wrong!");
console.error("Error writing document: ", error);
RachaelSkye marked this conversation as resolved.
Show resolved Hide resolved
});
};

return (
<form className={classes.form} onSubmit={e => handleSubmit(e)}>
<h3 className={classes.formTitle}>Add New Item</h3>
<input
className={classes.inputName}
type="text"
name="token"
placeholder="List Token"
value={props.token}
onChange={e => {
props.onEnterToken(e);
}}
required
/>
<input
className={classes.inputName}
type="text"
Expand Down
69 changes: 0 additions & 69 deletions src/Item.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/List/ItemList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState, useEffect } from "react";
import "firebase/firestore";
import * as firebase from "../lib/firebase";

const Items = props => {
const [dbItems, setdbItems] = useState([]);

useEffect(() => {
if (props.token) {
let db = firebase.fb.firestore();
db.collection(props.token)
.get()
.then(querySnapshot => {
const data = querySnapshot.docs.map(doc => doc.data());
setdbItems(data);
});
}
}, [props.token]);
RachaelSkye marked this conversation as resolved.
Show resolved Hide resolved

return (
<div>
<input
type="text"
name="token"
placeholder="enter token"
onChange={e => {
props.onEnterToken(e);
}}
value={props.token}
/>
<ul>
{dbItems.map((item, index) => {
return <li key={index}>{item.name}</li>;
RachaelSkye marked this conversation as resolved.
Show resolved Hide resolved
})}
</ul>
</div>
);
};

export default Items;
7 changes: 0 additions & 7 deletions src/List/List.js

This file was deleted.

Loading