Public
Code
HTTP
/** @jsxImportSource https://esm.sh/react */
import React, { useState, useEffect, useRef } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
import Chart from "https://esm.sh/chart.js/auto";
function App() {
const [polls, setPolls] = useState([]);
const [newPollQuestion, setNewPollQuestion] = useState("");
const [newPollOptions, setNewPollOptions] = useState(["", ""]);
const chartRefs = useRef({});
useEffect(() => {
fetchPolls();
}, []);
useEffect(() => {
polls.forEach((poll) => {
if (chartRefs.current[poll.id]) {
updateChart(poll);
} else {
createChart(poll);
}
});
}, [polls]);
const fetchPolls = async () => {
const response = await fetch("/polls");
const data = await response.json();
setPolls(data);
};
const handleCreatePoll = async (e) => {
e.preventDefault();
const response = await fetch("/polls", {
method: "POST",
headers: { "Content-Type": "application/json" },
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
laur3n-debatepollapp.web.val.run
Updated: October 2, 2024