-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-to-google-sheets.js
76 lines (69 loc) · 1.77 KB
/
post-to-google-sheets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, { useState } from 'react';
import axios from 'axios';
const Page = () => {
const [userAnswer, setUserAnswer] = useState(null);
const [response, setResponse] = useState('');
const [isSubmitting, setIsSubmitting] = useState(false);
const handleSubmit = async (event) => {
event.preventDefault();
setIsSubmitting(true);
setResponse('⏳ brb...');
try {
const response = await axios.post('/api/post-to-google-sheets', {
userAnswer: userAnswer
});
setResponse(response.data.message);
setIsSubmitting(false);
} catch (error) {
setIsSubmitting(false);
setResponse(error.response.data.message);
}
};
const handleChange = (event) => {
setResponse('');
setUserAnswer(event.target.value);
};
return (
<main>
<h1>Store data in Google Sheets</h1>
<h2>What came first?</h2>
<form onSubmit={handleSubmit}>
<label htmlFor="chicken">
<input
id="chicken"
type="radio"
value="chicken"
name="answer"
onChange={handleChange}
/>
Chicken
</label>
<label htmlFor="egg">
<input
id="egg"
type="radio"
value="egg"
name="answer"
onChange={handleChange}
/>
Egg
</label>
<label htmlFor="disco">
<input
id="disco"
type="radio"
value="disco"
name="answer"
onChange={handleChange}
/>
Disco
</label>
<button type="submit" disabled={isSubmitting || !userAnswer}>
Submit
</button>
</form>
<small>{response}</small>
</main>
);
};
export default Page;