Created
October 10, 2022 08:13
-
-
Save PaulieScanlon/26ebbeb0b0885a8605fb38fe5538a0a7 to your computer and use it in GitHub Desktop.
Adding the "click" handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
... | |
const Page = () => { | |
... | |
+ const handleClick = async (id) => { | |
+ setIsSubmitting(true); | |
+ try { | |
+ const response = await fetch(`/api/create-vote?id=${id}`); | |
+ if (!response.ok) { | |
+ throw new Error(response.statusText); | |
+ } | |
+ const result = await response.json(); | |
+ setResults(result); | |
+ setIsSubmitting(false); | |
+ setHasVoted(true); | |
+ } catch (error) { | |
+ setIsSubmitting(false); | |
+ setError({ | |
+ error: true, | |
+ message: error.message | |
+ }); | |
+ } | |
+ }; | |
return null; | |
}; | |
export default Page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment