|
1 | 1 |
|
2 | 2 | import React from 'react';
|
3 | 3 | import { gql, useMutation } from '@apollo/client';
|
| 4 | +import PropTypes from 'prop-types'; |
4 | 5 |
|
5 | 6 |
|
| 7 | +const Form = ({ description, id, likes }) => { |
6 | 8 |
|
7 |
| -const Form = (props) => { |
8 |
| - // const TEST = gql`mutation UpdateTest { |
9 |
| - // updateTest (id:"6041a075cf29434bf8e4552e", name: "first entry with HEC", likes: 23) { |
10 |
| - // description |
11 |
| - // id |
12 |
| - // likes |
13 |
| - // } |
14 |
| - // }`; |
15 | 9 | const ADD_LIKE = gql`
|
16 | 10 | mutation UpdateTest($id: ID, $name: String, $likes: Int) {
|
17 | 11 | updateTest(id: $id, name: $name, likes: $likes )
|
18 |
| - { |
19 |
| - description |
20 |
| - id |
21 |
| - likes |
| 12 | + { |
| 13 | + description |
| 14 | + id |
| 15 | + likes |
22 | 16 | }
|
23 |
| - } |
24 |
| - `; |
25 |
| - const [ updateTest, { data }] = useMutation(ADD_LIKE); |
26 |
| - // console.log('mutation returned data >>> ', data); |
| 17 | + }`; |
| 18 | + |
| 19 | + const [updateTest] = useMutation(ADD_LIKE); |
27 | 20 |
|
28 | 21 | // go to bed
|
29 | 22 | function handleClick(e) {
|
30 | 23 | e.preventDefault();
|
31 | 24 | // IMPORTANT: DO NOT ADD extra comma to the last line of the variable object, the query will not work
|
32 |
| - const myVar = { variables: { |
33 |
| - id: props.id, |
34 |
| - name: "testname", |
35 |
| - likes: (props.likes + 1) |
36 |
| - } |
| 25 | + const myVar = { |
| 26 | + variables: |
| 27 | + { |
| 28 | + id, |
| 29 | + name: 'testname', |
| 30 | + likes: likes + 1, |
| 31 | + }, |
37 | 32 | };
|
| 33 | + // send Mutation |
38 | 34 | updateTest(myVar);
|
39 | 35 | }
|
40 | 36 |
|
41 | 37 | return (
|
42 | 38 | <div className = 'form'>
|
43 |
| - <h2>{ props.description }</h2> |
44 |
| - <button id={ props.id } onClick={ handleClick }>heart</button> |
45 |
| - <h3>Likes: { props.likes }</h3> |
| 39 | + <h2>{ description }</h2> |
| 40 | + <button id={ id } onClick={ handleClick }>heart</button> |
| 41 | + <h3>Likes: { likes }</h3> |
46 | 42 | </div>
|
47 | 43 | );
|
48 | 44 | };
|
49 | 45 |
|
| 46 | +// Variable validation using propTypes |
| 47 | +Form.propTypes = { |
| 48 | + description: PropTypes.string.isRequired, |
| 49 | + id: PropTypes.id.isRequired, |
| 50 | + likes: PropTypes.number.isRequired, |
| 51 | +}; |
| 52 | + |
| 53 | + |
50 | 54 | export default Form;
|
0 commit comments