LogoutButton.tsx 712 B

1234567891011121314151617181920212223242526
  1. import { useMutation, useQuery } from '@apollo/react-hooks'
  2. import { USER_LOGOUT, CURRENT_USER } from './graphql'
  3. import { SyntheticEvent } from 'react'
  4. interface LogoutButtonProps {
  5. title?: string
  6. }
  7. const LogoutButton = ({ title }: LogoutButtonProps) => {
  8. const [logout, { loading, error }] = useMutation(USER_LOGOUT)
  9. const { refetch } = useQuery(CURRENT_USER)
  10. return (
  11. <button disabled={loading} onClick={async (event: SyntheticEvent) => {
  12. try {
  13. const data = await logout()
  14. console.log('LogoutButton', data)
  15. refetch()
  16. } catch (error) {
  17. console.log('LogoutButton', error)
  18. }
  19. }}>{title || 'Log out'}</button>
  20. )
  21. }
  22. export default LogoutButton