-
-
Save ericwooley/ba9e893c06b0e69786fd27eb7633d132 to your computer and use it in GitHub Desktop.
add todo mutation
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
import React from 'react'; | |
import { | |
useMyListsQuery, | |
useCreateListMutation, | |
useAddTodoItemMutation, | |
} from '@the-wooley-devbox/graphql-sdk'; | |
export const TodoList = () => { | |
const [createList] = useCreateListMutation(); | |
const [addTodoItem] = useAddTodoItemMutation(); | |
const { data, loading, error, refetch } = useMyListsQuery({ ssr: false }); | |
if (loading || !data) return <h3>Loading...</h3>; | |
if (error) | |
return ( | |
<h3> | |
Uh oh, something went wrong! | |
<hr /> | |
<button onClick={() => refetch()}>Try again</button> | |
</h3> | |
); | |
return ( | |
<div> | |
<h3> | |
My Lists ({`${data.myLists.length}`}){' '} | |
<button | |
onClick={() => { | |
createList({ | |
variables: { | |
name: | |
prompt('What do you want to name your list?') || | |
'Untitled List', | |
}, | |
}).then(() => refetch()); | |
}} | |
> | |
+ | |
</button> | |
: | |
</h3> | |
{data.myLists.length === 0 && ( | |
<h4>Looks like you don't have any lists yet.</h4> | |
)} | |
{data.myLists.map((list) => ( | |
<div key={list.id}> | |
<h4>{list.name} </h4> | |
{list.items | |
?.slice() | |
.sort((a, b) => { | |
return parseInt(a.id, 10) - parseInt(b.id, 10); | |
}) | |
.map((todo) => ( | |
<div key={todo.id}> | |
<input type="checkbox" checked={todo.done} /> | |
| |
<strong>{todo.name}</strong> | |
</div> | |
))} | |
<button | |
onClick={() => { | |
addTodoItem({ | |
variables: { | |
listId: parseInt(list.id, 10), | |
todoName: | |
prompt('What do you want to name your todo?') || | |
'Untitled Todo', | |
}, | |
}).then(() => refetch()); | |
}} | |
> | |
+ Add Todo Item | |
</button> | |
<hr /> | |
</div> | |
))} | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment