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.
changed todo file names, added shoppinglist
- Loading branch information
Showing
7 changed files
with
205 additions
and
13 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
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,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; |
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 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 /> 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; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.