|
| 1 | +import React, {useEffect, useState} from 'react'; |
| 2 | +import logo from './logo.svg'; |
| 3 | +import './App.css'; |
| 4 | +import {Layout, Row, Col, Button, Spin, List, Checkbox, Input} from "antd"; |
| 5 | +import { WalletSelector } from "@aptos-labs/wallet-adapter-ant-design"; |
| 6 | +import "@aptos-labs/wallet-adapter-ant-design/dist/index.css"; |
| 7 | +import { Aptos } from "@aptos-labs/ts-sdk"; |
| 8 | +import { |
| 9 | + useWallet, |
| 10 | + InputTransactionData, |
| 11 | +} from "@aptos-labs/wallet-adapter-react"; |
| 12 | +import {CheckboxChangeEvent} from "antd/es/checkbox"; |
| 13 | + |
| 14 | + |
| 15 | +const aptos = new Aptos(); |
| 16 | + |
| 17 | +type Task={ |
| 18 | + address: string; |
| 19 | + completed: boolean; |
| 20 | + content: string; |
| 21 | + task_id: string; |
| 22 | +} |
| 23 | + |
| 24 | +function App() { |
| 25 | + const [accountHasList, setAccountHasList] = useState<boolean>(false); |
| 26 | + // a local state to keep tract whether a transaction is in progress |
| 27 | + const [transactionInProgress, setTransactionInProgress] = |
| 28 | + useState<boolean>(false); |
| 29 | + // Extract the account object from the wallet adapter |
| 30 | + const { account,signAndSubmitTransaction }=useWallet(); |
| 31 | + const [tasks, setTasks]=useState<Task[]>([]); |
| 32 | + const [newTask, setNewTask] = useState<string>(""); |
| 33 | + |
| 34 | + // add a hook to update the account resource changes |
| 35 | + useEffect(() => { |
| 36 | + fetchList(); |
| 37 | + }, [account?.address]); |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | + const moduleAddress = "0xa85172e8c0f2125ca6b11b2774b45f3c39540c2b799583eb6771ae29c7ef2939"; |
| 42 | + |
| 43 | + // onWriteTask function will get called whenever a user types something in the input text |
| 44 | + const onWriteTask = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 45 | + const value = event.target.value; |
| 46 | + setNewTask(value); |
| 47 | + }; |
| 48 | + |
| 49 | + const fetchList = async () => { |
| 50 | + if (!account) return []; |
| 51 | + try { |
| 52 | + const todoListResource = await aptos.getAccountResource( |
| 53 | + { |
| 54 | + accountAddress:account?.address, |
| 55 | + resourceType: `${moduleAddress}::notebook::TodoList` |
| 56 | + } |
| 57 | + ); |
| 58 | + setAccountHasList(true); |
| 59 | + |
| 60 | + // tasks table handle |
| 61 | + const tableHandle = (todoListResource as any).tasks.handle; |
| 62 | + // tasks table counter |
| 63 | + const taskCounter = (todoListResource as any).task_counter; |
| 64 | + |
| 65 | + let tasks=[]; |
| 66 | + let counter=1; |
| 67 | + while(counter <= taskCounter) { |
| 68 | + const tableItem = { |
| 69 | + key_type: "u64", |
| 70 | + value_type: `${moduleAddress}::notebook::Task`, |
| 71 | + key: `${counter}`, |
| 72 | + }; |
| 73 | + const task = await aptos.getTableItem<Task>({handle:tableHandle, data:tableItem}); |
| 74 | + tasks.push(task); |
| 75 | + counter++; |
| 76 | + } |
| 77 | + setTasks(tasks); |
| 78 | + } catch (e: any){ |
| 79 | + setAccountHasList(false); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const addNewList = async () => { |
| 84 | + if (!account) return []; |
| 85 | + setTransactionInProgress(true); |
| 86 | + // call the todolist function |
| 87 | + const transaction:InputTransactionData = { |
| 88 | + data: { |
| 89 | + function:`${moduleAddress}::notebook::create_list`, |
| 90 | + functionArguments:[] |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + try { |
| 95 | + const response = await signAndSubmitTransaction(transaction); |
| 96 | + // wait for transaction |
| 97 | + await aptos.waitForTransaction({transactionHash:response.hash}); |
| 98 | + setAccountHasList(true); |
| 99 | + } catch (e) { |
| 100 | + setAccountHasList(false); |
| 101 | + } finally { |
| 102 | + setTransactionInProgress(false); |
| 103 | + } |
| 104 | + }; |
| 105 | + |
| 106 | + //submit task text onto blockchain |
| 107 | + const onTaskAdded = async() => { |
| 108 | + // check for connected account |
| 109 | + if(!account) return; |
| 110 | + setTransactionInProgress(true); |
| 111 | + const transaction:InputTransactionData={ |
| 112 | + data:{ |
| 113 | + function:`${moduleAddress}::notebook::create_task`, |
| 114 | + functionArguments:[newTask] |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + //hold the latest task.task_id from our local state |
| 119 | + const latestId = tasks.length > 0 ? parseInt(tasks[tasks.length - 1].task_id) + 1 : 1; |
| 120 | + // build a newTaskToPush object into our local state |
| 121 | + const newTaskToPush = { |
| 122 | + address: account.address, |
| 123 | + completed: false, |
| 124 | + content: newTask, |
| 125 | + task_id: latestId + "", |
| 126 | + }; |
| 127 | + |
| 128 | + try { |
| 129 | + // sign and submit transaction to chain |
| 130 | + const response = await signAndSubmitTransaction(transaction); |
| 131 | + // wait for transaction |
| 132 | + await aptos.waitForTransaction({transactionHash:response.hash}); |
| 133 | + |
| 134 | + // Create a new array based on current state; |
| 135 | + let newTasks = [...tasks]; |
| 136 | + |
| 137 | + // Add item to the tasks array |
| 138 | + newTasks.push(newTaskToPush); |
| 139 | + //Set state |
| 140 | + setTasks(newTasks); |
| 141 | + //clear input text |
| 142 | + setNewTask(""); |
| 143 | + } catch (e) { |
| 144 | + console.log("error", e); |
| 145 | + } finally { |
| 146 | + setTransactionInProgress(false); |
| 147 | + } |
| 148 | + }; |
| 149 | + |
| 150 | + const onCheckboxChange = async ( |
| 151 | + event: CheckboxChangeEvent, |
| 152 | + taskId: string |
| 153 | + )=> { |
| 154 | + if (!account) return; |
| 155 | + if (!event.target.checked) return; |
| 156 | + setTransactionInProgress(true); |
| 157 | + const transaction:InputTransactionData = { |
| 158 | + data:{ |
| 159 | + function:`${moduleAddress}::notebook::complete_task`, |
| 160 | + functionArguments:[taskId] |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + try { |
| 165 | + // sign and submit transaction to chain |
| 166 | + const response = await signAndSubmitTransaction(transaction); |
| 167 | + // wait for transaction |
| 168 | + await aptos.waitForTransaction({transactionHash:response.hash}); |
| 169 | + |
| 170 | + setTasks((prevState) => { |
| 171 | + const newState = prevState.map((obj) => { |
| 172 | + // if task_id equals the checked taskId, update completed property |
| 173 | + if (obj.task_id === taskId) { |
| 174 | + return { ...obj, completed: true }; |
| 175 | + } |
| 176 | + |
| 177 | + // otherwise return object as is |
| 178 | + return obj; |
| 179 | + }); |
| 180 | + |
| 181 | + return newState; |
| 182 | + }); |
| 183 | + } catch (error: any) { |
| 184 | + console.log("error", error); |
| 185 | + } finally { |
| 186 | + setTransactionInProgress(false); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return ( |
| 191 | + <> |
| 192 | + <Layout> |
| 193 | + <Row align="middle"> |
| 194 | + <Col span={10} offset={2}> |
| 195 | + <h1>Our todolist</h1> |
| 196 | + </Col> |
| 197 | + <Col span={12} style={{ textAlign: "right", paddingRight: "200px" }}> |
| 198 | + <WalletSelector /> |
| 199 | + </Col> |
| 200 | + </Row> |
| 201 | + </Layout> |
| 202 | + <Spin spinning = {transactionInProgress}> |
| 203 | + { |
| 204 | + !accountHasList ? ( |
| 205 | + <Row gutter={[0, 32]} style={{ marginTop: "2rem" }}> |
| 206 | + <Col span={8} offset={8}> |
| 207 | + <Button |
| 208 | + disabled={!account} |
| 209 | + block |
| 210 | + onClick={addNewList} |
| 211 | + type="primary" |
| 212 | + style={{ height: "40px", backgroundColor: "#3f67ff" }} |
| 213 | + > |
| 214 | + Add new list |
| 215 | + </Button> |
| 216 | + </Col> |
| 217 | + </Row> |
| 218 | + ) : ( |
| 219 | + <Row gutter={[0, 32]} style={{ marginTop: "2rem" }}> |
| 220 | + <Col span={8} offset={8}> |
| 221 | + <Input.Group compact> |
| 222 | + <Input |
| 223 | + onChange={(event) => onWriteTask(event)} |
| 224 | + style={{ width: "calc(100% - 60px)" }} |
| 225 | + placeholder="Add a Task" |
| 226 | + size="large" |
| 227 | + value={newTask} |
| 228 | + /> |
| 229 | + <Button |
| 230 | + onClick={onTaskAdded} |
| 231 | + type="primary" |
| 232 | + style={{ height: "40px", backgroundColor: "#3f67ff" }} |
| 233 | + > |
| 234 | + Add |
| 235 | + </Button> |
| 236 | + </Input.Group> |
| 237 | + </Col> |
| 238 | + <Col span={8} offset={8}> |
| 239 | + {tasks && ( |
| 240 | + <List |
| 241 | + size="small" |
| 242 | + bordered |
| 243 | + dataSource={tasks} |
| 244 | + renderItem={(task: any) => ( |
| 245 | + <List.Item actions={[ |
| 246 | + <div> |
| 247 | + {task.completed ? ( |
| 248 | + <Checkbox defaultChecked={true} disabled /> |
| 249 | + ) : ( |
| 250 | + <Checkbox |
| 251 | + onChange={(event) => |
| 252 | + onCheckboxChange(event, task.task_id) |
| 253 | + } |
| 254 | + /> |
| 255 | + )} |
| 256 | + </div>, |
| 257 | + ]}> |
| 258 | + <List.Item.Meta |
| 259 | + title={task.content} |
| 260 | + description={ |
| 261 | + <a |
| 262 | + href={`https://explorer.aptoslabs.com/account/${task.address}/`} |
| 263 | + target="_blank" |
| 264 | + >{`${task.address.slice(0, 6)}...${task.address.slice(-5)}`}</a> |
| 265 | + } |
| 266 | + /> |
| 267 | + </List.Item> |
| 268 | + )} |
| 269 | + /> |
| 270 | + )} |
| 271 | + </Col> |
| 272 | + </Row> |
| 273 | + ) |
| 274 | + } |
| 275 | + </Spin> |
| 276 | + </> |
| 277 | + ); |
| 278 | +} |
| 279 | + |
| 280 | +export default App; |
0 commit comments