Skip to content

Commit e4d37c6

Browse files
committed
implement variable validation with propType in Form.jsx
1 parent 832567e commit e4d37c6

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

app/src/Dashboard/Form.jsx

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11

22
import React from 'react';
33
import { gql, useMutation } from '@apollo/client';
4+
import PropTypes from 'prop-types';
45

56

7+
const Form = ({ description, id, likes }) => {
68

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-
// }`;
159
const ADD_LIKE = gql`
1610
mutation UpdateTest($id: ID, $name: String, $likes: Int) {
1711
updateTest(id: $id, name: $name, likes: $likes )
18-
{
19-
description
20-
id
21-
likes
12+
{
13+
description
14+
id
15+
likes
2216
}
23-
}
24-
`;
25-
const [ updateTest, { data }] = useMutation(ADD_LIKE);
26-
// console.log('mutation returned data >>> ', data);
17+
}`;
18+
19+
const [updateTest] = useMutation(ADD_LIKE);
2720

2821
// go to bed
2922
function handleClick(e) {
3023
e.preventDefault();
3124
// 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+
},
3732
};
33+
// send Mutation
3834
updateTest(myVar);
3935
}
4036

4137
return (
4238
<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>
4642
</div>
4743
);
4844
};
4945

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+
5054
export default Form;

app/src/Dashboard/FormsContainer.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const FormsContainer = () => {
1212
// define the graphQL query string
1313
const GET_TESTS = gql`query {readAllTests { description id likes }}`;
1414
// useQuery hook abstracts fetch request
15-
const { loading, error, data } = useQuery(GET_TESTS);
15+
const { loading, error, data } = useQuery(GET_TESTS, { pollInterval: 1000 } );
1616
if (loading) return <p>Loading...</p>;
1717
if (error) return <p>Error :{error}</p>;
1818
// based on resolver(readAllTests) for this query, the data is stored in the data object with the key 'readAllTests'

app/src/components/top/NavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export default function NavBar(props) {
168168
</Typography>
169169

170170
{/* ==================================Dashboard Button================================================== */}
171-
<Link to='/dashboard'>
171+
<Link to='/dashboard' style ={ {textDecoration: 'none'} }>
172172
<Button
173173
variant="contained"
174174
color="primary"

0 commit comments

Comments
 (0)