{"size": 1344, "ext": "js", "max_stars_repo_path": "src/components/updateBookmark.js", "max_stars_repo_name": "donaldboulton/Gatsby-FaunaDB-GraphQL", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/components/updateBookmark.js", "max_issues_repo_name": "donaldboulton/Gatsby-FaunaDB-GraphQL", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/components/updateBookmark.js", "max_forks_repo_name": "donaldboulton/Gatsby-FaunaDB-GraphQL", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.0597014925, "max_line_length": 80, "alphanum_fraction": 0.484375}
import React from "react"
import gql from "graphql-tag"
import { useMutation } from "@apollo/react-hooks"

const CREATE_BOOKMARK = gql`
  mutation updateBookmark(
    $id: ID!
    $private: Boolean!
    $userID: ID!
    $courseID: ID!
  ) {
    updateBookmark(
      id: $id
      data: {
        private: $private
        user: { connect: $userID }
        course: { connect: $courseID }
      }
    ) {
      _id
      private
    }
  }
`

const UpdateBookmark = ({
  bookmarkID,
  userID,
  courseID,
  text = "Bookmark",
  privateBookmark = false,
}) => {
  const [updateBookmark, { loading, error }] = useMutation(CREATE_BOOKMARK)

  return (
    <>
      {error && (
        <div style={{ marginLeft: `1rem`, color: `gray` }}>{error.message}</div>
      )}
      {loading ? (
        <span style={{ marginLeft: `1rem`, color: `gray` }}>Loading...</span>
      ) : (
        <a
          href="/"
          style={{ marginLeft: `.5rem`, color: `blue` }}
          onClick={event => {
            event.preventDefault()

            updateBookmark({
              variables: {
                id: bookmarkID,
                private: privateBookmark,
                userID,
                courseID,
              },
            })
          }}
        >
          {text}
        </a>
      )}
    </>
  )
}

export default UpdateBookmark
