Skip to content

Commit

Permalink
changed todo file names, added shoppinglist
Browse files Browse the repository at this point in the history
  • Loading branch information
vivjamba committed Nov 12, 2022
1 parent 373e21d commit d7a0b5a
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 13 deletions.
47 changes: 43 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BrowserRouter, Redirect, Route, Switch } from "react-router-dom";
import Todo from "./pages/Todo/Todo";
import Fridge from "./pages/Todo/FridgeList";
import Shopping from "./pages/Todo/ShoppingList";
import Login from "./pages/Login/Login";
import Landing from "./pages/Landing/Landing";
import { useGetUser } from "./hooks";
Expand All @@ -11,11 +12,14 @@ function App() {
return (
<BrowserRouter>
<Switch>
<Route path="/todos">
{user ? <Todo user={user} dispatch={dispatch} /> : <Redirect to="/login" />}
<Route path="/fridge">
{user ? <Fridge user={user} dispatch={dispatch} /> : <Redirect to="/login" />}
</Route>
<Route path="/shoppinglist">
{user ? <Shopping user={user} dispatch={dispatch} /> : <Redirect to="/login" />}
</Route>
<Route path="/login">
{user ? <Redirect to="/todos" /> : <Login dispatch={dispatch}/>}
{user ? <Redirect to="/fridge" /> : <Login dispatch={dispatch}/>}
</Route>
<Route exact path="/">
<Landing />
Expand All @@ -26,4 +30,39 @@ function App() {
);
}

// import { BrowserRouter, Redirect, Route, Switch } from "react-router-dom";
// import Fridge from "./pages/Todo/FridgeList";
// import Shopping from "./pages/Todo/ShoppingList";
// import Login from "./pages/Login/Login";
// import Landing from "./pages/Landing/Landing";
// import { useGetUser } from "./hooks";

// function App() {
// // eslint-disable-next-line
// const [{ user, isLoading, isError }, dispatch] = useGetUser();

// return (
// <BrowserRouter>
// <Switch>
// <Route path="/fridgelist">
// {user ? <Fridge user={user} dispatch={dispatch} /> : <Redirect to="/login" />}
// </Route>
// <Route path="/shoppinglist">
// {user ? <Shopping user={user} dispatch={dispatch} /> : <Redirect to="/login" />}
// </Route>
// <Route path="/login">
// {user ? <Redirect to="/shoppinglist" /> : <Login dispatch={dispatch}/>}
// </Route>
// <Route exact path="/">
// <Landing />
// </Route>
// <Redirect to="/" />
// </Switch>
// </BrowserRouter>
// );
// }

// export default App;


export default App;
2 changes: 1 addition & 1 deletion src/pages/Landing/Landing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Landing = () => {
const history = useHistory();

const handleClick = () => {
history.push("/todos");
history.push("/fridge");
};

const links = [
Expand Down
4 changes: 2 additions & 2 deletions src/pages/Todo/TodoItem.js → src/pages/Todo/FridgeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { deleteButton } from "../icons";
const current = new Date();
// const date = `${current.getDate()}/${current.getMonth()+1}/${current.getFullYear()}`;

const TodoItem = ({ item, setStale }) => {
const FridgeItem = ({ item, setStale }) => {
const handleComplete = async (e, item) => {
console.log("Marking Todo as complete");
let data = {
Expand Down Expand Up @@ -66,4 +66,4 @@ const TodoItem = ({ item, setStale }) => {
);
};

export default TodoItem;
export default FridgeItem;
12 changes: 6 additions & 6 deletions src/pages/Todo/Todo.js → src/pages/Todo/FridgeList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import api from "../../api/api";
import { FetchState, useGetTodos } from "../../hooks";
import { Server } from "../../utils/config";
import Alert from "../Alert/Alert";
import TodoItem from "./TodoItem";
import FridgeItem from "./FridgeItem";

const Todo = ({ user, dispatch }) => {
const Fridge = ({ user, dispatch }) => {
const [stale, setStale] = useState({ stale: false });
const [{ todos, isLoading, isError }] = useGetTodos(stale);
const [{ items, isLoading, isError }] = useGetTodos(stale);
const [currentTodo, setCurrentTodo] = useState("");

const handleAddTodo = async (e) => {
Expand Down Expand Up @@ -65,8 +65,8 @@ const Todo = ({ user, dispatch }) => {
{isLoading && <h1> Loading .... </h1>}

<ul>
{todos.map((item) => (
<TodoItem key={item["$id"]} item={item} setStale={setStale} />
{items.map((item) => (
<FridgeItem key={item["$id"]} item={item} setStale={setStale} />
))}
</ul>
</div>
Expand All @@ -81,4 +81,4 @@ const Todo = ({ user, dispatch }) => {
);
};

export default Todo;
export default Fridge;
69 changes: 69 additions & 0 deletions src/pages/Todo/ShoppingItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import api from "../../api/api";
import { Server } from "../../utils/config";
import { deleteButton } from "../icons";

const current = new Date();
// const date = `${current.getDate()}/${current.getMonth()+1}/${current.getFullYear()}`;

const ShoppingItem = ({ item, setStale }) => {
const handleComplete = async (e, item) => {
console.log("Marking Todo as complete");
let data = {
isBought: !item["isBought"],
boughtDate : current
};
try {
console.log(item)
await api.updateDocument(
Server.collectionID,
item["$id"],
data,
item["$read"],
item["$write"]
);
setStale({ stale: true });
} catch (e) {
console.log("Error in marking todo as complete");
}
};

const handleDelete = async (e, item) => {
console.log("Deleting Todo");
try {
await api.deleteDocument(Server.collectionID, item["$id"]);
setStale({ stale: true });
} catch (e) {
console.log("Error in deleting todo");
}
};

return (
<li className="flex justify-between items-center mt-4 px-4">
<div className="flex">
<input
type="checkbox"
className="h-6 w-6 text-green-500 rounded-md border-4 border-green-200 focus:ring-0 transition duration-75 ease-in-out transform hover:scale-125"
//checked takes a boolean
checked={item["isBought"]}
onChange={(e) => handleComplete(e, item)}
/>
<div
//puts line through item if not bought
className={`capitalize ml-3 text-md font-medium ${
item["isBought"] ? "line-through" : ""
}`}
>
{item["content"]}
</div>
</div>
<button
onClick={(e) => handleDelete(e, item)}
className="focus:outline-none transition duration-75 ease-in-out transform hover:scale-125"
>
{deleteButton}
</button>
</li>
);
};

export default ShoppingItem;
84 changes: 84 additions & 0 deletions src/pages/Todo/ShoppingList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { useState } from "react";
import api from "../../api/api";
import { FetchState, useGetTodos } from "../../hooks";
import { Server } from "../../utils/config";
import Alert from "../Alert/Alert";
import ShoppingItem from "./FridgeItem";

const Shopping = ({ user, dispatch }) => {
const [stale, setStale] = useState({ stale: false });
const [{ items, isLoading, isError }] = useGetTodos(stale);
const [currentTodo, setCurrentTodo] = useState("");

const handleAddTodo = async (e) => {
e.preventDefault();
console.log("Adding Todo");
const data = {
content: currentTodo,
isBought: false,
};
console.log(data, user);
try {
await api.createDocument(
Server.collectionID,
data,
[`user:${user["$id"]}`],
[`user:${user["$id"]}`]
);
setStale({ stale: true });
setCurrentTodo("");
} catch (e) {
console.log("Error in adding todo");
}
};


const handleLogout = async (e) => {
dispatch({ type: FetchState.FETCH_INIT });
try {
await api.deleteCurrentSession();
dispatch({ type: FetchState.FETCH_SUCCESS, payload: null });
} catch (e) {
dispatch({ type: FetchState.FETCH_FAILURE });
}
}

return (
<>
<section className="container h-screen max-h-screen px-3 max-w-xl mx-auto flex flex-col">
{isError && <Alert color="red" message="Something went wrong..." />}
<div className="my-auto p-16 rounded-lg text-center">
<div className="font-bold text-3xl md:text-5xl lg:text-6xl">
📝 <br /> &nbsp; toTooooDoooos
</div>

<form onSubmit={handleAddTodo}>
<input
type="text"
className="w-full my-8 px-6 py-4 text-xl rounded-lg border-0 focus:ring-2 focus:ring-gray-800 transition duration-200 ease-in-out transform hover:-translate-y-1 hover:scale-110 hover:shadow-xl shadow-md"
placeholder="🤔 What to do today?"
value={currentTodo}
onChange={(e) => setCurrentTodo(e.target.value)}
></input>
</form>

{isLoading && <h1> Loading .... </h1>}

<ul>
{items.map((item) => (
<ShoppingItem key={item["$id"]} item={item} setStale={setStale} />
))}
</ul>
</div>
</section>

<section className="absolute bottom-0 right-0 py-3 px-6 mr-8 mb-8">
<button onClick={handleLogout} className="mx-auto mt-4 py-3 px-12 font-semibold text-md rounded-lg shadow-md bg-white text-gray-900 border border-gray-900 hover:border-transparent hover:text-white hover:bg-gray-900 focus:outline-none">
Logout 👋
</button>
</section>
</>
);
};

export default Shopping;
Binary file added src/static/bag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d7a0b5a

Please sign in to comment.