import React, {useState} from "react"; import { Button, HStack, Menu, MenuButton, MenuItem, MenuList, Spinner, Stack, Table, Tbody, Td, Text, Thead, Tr, useToast, } from "@chakra-ui/react"; import {gql, useMutation, useQuery} from "@apollo/client"; import {formatTimeStamp} from "./../utils/TransformText"; import {ChevronDownIcon} from "@chakra-ui/icons"; import ConfirmDialog from "../Custom/ConfirmDialog"; import TopBar from "./TopBar"; const Data = [ { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" }, { title: "Learning to stand", category: "Category X", publish_date: "2021-10-10" } ]; export const GET_POSTS = gql` query { posts(order_by: {date: desc}) { body date id title post_category { name id } category_id } } `; export const DELETE_POST = gql`mutation ($id: Int!) { delete_posts_by_pk(id: $id) { id } } `; const BlogsTable = (props: any) => { const {setIsSelected, setSelectedPost, selectedPost} = props; const { loading: postsLoading, error: postsError, data: postsData } = useQuery(GET_POSTS); const [isDialogOpen, setDialogOpen] = useState(false); const [deletePost] = useMutation(DELETE_POST); const toast = useToast(); function openEditor(post: any) { setIsSelected(true); setSelectedPost(post); } if (postsLoading) { return ; } if (postsError) { return {postsError}; } const handleEdit = (post: any) => { setIsSelected(true); setSelectedPost(post); }; const handleDelete = (post: any) => { setDialogOpen(true); setSelectedPost(post); }; const deleteCallBack = (isConfirm: boolean) => { if (isConfirm) { const options = { variables: { id: selectedPost.id, }, refetchQueries: [ {query: GET_POSTS}, ] }; deletePost(options).then(data => { toast({ title: 'Post deleted', status: 'success', duration: 3000, isClosable: true, }); }).catch(error => { toast({ title: error, status: 'error', duration: 3000, isClosable: true, }); }); } setDialogOpen(false); }; return ( {postsData?.posts?.map((item: any) => { return ( ); })}
Category Title Publish Date Actions
{item.post_category?.name} {item.title} {formatTimeStamp(item.date)} }> Actions handleEdit(item)}>Edit handleDelete(item)}>Delete
); }; export default BlogsTable;