import type { DeletePostMutation, DeletePostMutationVariables, FindPosts, } from 'types/graphql' import { Link, routes } from '@redwoodjs/router' import { useMutation } from '@redwoodjs/web' import type { TypedDocumentNode } from '@redwoodjs/web' import { toast } from '@redwoodjs/web/toast' import { QUERY } from 'src/components/Post/PostsCell' import { timeTag, truncate } from 'src/lib/formatters' const DELETE_POST_MUTATION: TypedDocumentNode< DeletePostMutation, DeletePostMutationVariables > = gql` mutation DeletePostMutation($id: Int!) { deletePost(id: $id) { id } } ` const PostsList = ({ posts }: FindPosts) => { const [deletePost] = useMutation(DELETE_POST_MUTATION, { onCompleted: () => { toast.success('Post deleted') }, onError: (error) => { toast.error(error.message) }, // This refetches the query on the list page. Read more about other ways to // update the cache over here: // https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates refetchQueries: [{ query: QUERY }], awaitRefetchQueries: true, }) const onDeleteClick = (id: DeletePostMutationVariables['id']) => { if (confirm('Are you sure you want to delete post ' + id + '?')) { deletePost({ variables: { id } }) } } return (
{posts.map((post) => ( ))}
Id Title Body Created at Updated at  
{truncate(post.id)} {truncate(post.title)} {truncate(post.body)} {timeTag(post.createdAt)} {timeTag(post.updatedAt)}
) } export default PostsList