forked from appwrite/demo-todo-with-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from vivjamba/shoppinglist+fridge
Shoppinglist+fridge
- Loading branch information
Showing
7 changed files
with
182 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import api from "../../api/api"; | ||
import { Server } from "../../utils/config"; | ||
import { deleteButton } from "../icons"; | ||
|
||
const FridgeItem = ({ item, setStale }) => { | ||
|
||
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"> | ||
<div | ||
//puts line through item if not bought | ||
className={`capitalize ml-3 text-md font-medium ${ | ||
!item["isBought"] ? "line-through" : "" | ||
}`} | ||
> | ||
{item["content"]} | ||
</div> | ||
</div> | ||
<div | ||
//puts line through item if not bought | ||
className={`capitalize ml-3 text-md font-medium ${ | ||
!item["isBought"] ? "line-through" : "" | ||
}`} | ||
> | ||
{item["boughtDate"]} | ||
</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 FridgeItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 FridgeItem from "./FridgeItem"; | ||
|
||
const Fridge = ({ user, dispatch }) => { | ||
const [stale, setStale] = useState({ stale: false }); | ||
const [{ todos, 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 /> 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> | ||
{todos.filter((item) => item["isBought"]).map((item) => ( | ||
<FridgeItem 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 Fridge; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.