diff --git a/Part_8/library-frontend/src/components/Authors.js b/Part_8/library-frontend/src/components/Authors.js index 2467ccf..dae328f 100644 --- a/Part_8/library-frontend/src/components/Authors.js +++ b/Part_8/library-frontend/src/components/Authors.js @@ -1,5 +1,6 @@ import { useQuery } from '@apollo/client' import { ALL_AUTHORS } from '../queries' +import BirthYearForm from './BirthYearForm' const Authors = () => { const result = useQuery(ALL_AUTHORS) @@ -33,6 +34,8 @@ const Authors = () => { ))} + + ) } diff --git a/Part_8/library-frontend/src/components/BirthYearForm.js b/Part_8/library-frontend/src/components/BirthYearForm.js new file mode 100644 index 0000000..53f89c6 --- /dev/null +++ b/Part_8/library-frontend/src/components/BirthYearForm.js @@ -0,0 +1,55 @@ +import { useMutation } from '@apollo/client' +import { useState } from 'react' +import { ALL_AUTHORS, UPDATE_AUTHOR } from '../queries' + +const BirthYearForm = ({ authors }) => { + const [selectedAuthor, setSelectedAuthor] = useState(null) + const [birthyear, setBirthYear] = useState(null) + + const [ editAuthor ] = useMutation(UPDATE_AUTHOR, { + refetchQueries: [ { query: ALL_AUTHORS }] + }) + + const handleSelect = event => { + event.preventDefault() + + console.log(event.target.value) + } + + const handleSubmit = event => { + event.preventDefault() + + editAuthor({ variables: { + name: selectedAuthor, + born: parseInt(birthyear) + } }) + console.log('Author Name:', selectedAuthor) + console.log('Birth Year:', birthyear) + } + + return ( +
+

Set Birthyear

+
+
+

+ Name:  + +

+

+ Birth Year:  + setBirthYear(e.target.value)}/> +

+

+
+
+
+ ) +} + +export default BirthYearForm \ No newline at end of file diff --git a/Part_8/library-frontend/src/queries.js b/Part_8/library-frontend/src/queries.js index b2c4e49..f9204cc 100644 --- a/Part_8/library-frontend/src/queries.js +++ b/Part_8/library-frontend/src/queries.js @@ -35,4 +35,16 @@ mutation addBook($title: String!, $published: Int!, $author: String!, $genres: [ genres } } +` + +export const UPDATE_AUTHOR = gql` +mutation editAuthor($name: String!, $born: Int!) { + editAuthor( + name: $name, + setBornTo: $born + ) { + name + born + } +} ` \ No newline at end of file diff --git a/Part_8/library-graphql/index.js b/Part_8/library-graphql/index.js index b06c561..b48b0df 100644 --- a/Part_8/library-graphql/index.js +++ b/Part_8/library-graphql/index.js @@ -105,7 +105,7 @@ const typeDefs = ` type Mutation { addBook(title: String!, published: Int!, author: String!, genres: [String!]!): Book! - editAuthor(name: String!, setBornTo: Int!): Author! + editAuthor(name: String!, setBornTo: Int!): Author } ` @@ -170,10 +170,6 @@ const resolvers = { const updatedAuthor = { ...author, born: setBornTo } authors = authors.map(a => a.name === name ? updatedAuthor : a) - const booksByAuthor = books.filter(b => b.author === name) - booksByAuthor.forEach(b => (b.author = updatedAuthor)) - updatedAuthor.bookCount = booksByAuthor.length - return updatedAuthor } }