I am building a project using next js for my basketball league. The goal is to keep track of games, players, stats, etc. I am having trouble implementing the submission of a game. I have a table of all the player in the particular game and I want the user to be able to edit the stats of the game to keep track of points, assists, rebounds, etc. The issue lies in the fact that the user will be editing these fields constantly and I am worried that if the user refreshes the page or loses connection then the page will refresh and the current game stats that the user was keeping track of will be lost. I am storing the seasonal stats in a database and was planning to have an onSubmit for the game so when the game is over, the game stats are sent to the season stats database and updated accordingly. I am just not sure how to handle the form so that the game stats the user is updating won't be lost.
One thought I had was to set an interval every n seconds inside a useEffect to send the gameStats to the database.
Any other ideas?
Thank you in advance!
It would go something like this...
const GameForm = ({players}) => {
// players is a list of objects from a database coming from getServerSideProps, where each document stores player id, player name, //points, assists... the points, rebounds, assists, etc. are from the entire season so what I am doing is //setting them all to zero and when I post them to the database again I will simply increment the seasonal //stats with the game stats.
const gamePlayers = players.map(player => {...player, points:0,assists:0, rebounds: 0, blocks: 0, steals:0, turnovers:0})
const [playerStats, setPlayerStats] = useState(gamePlayers)
useEffect(() => {
setInterval(async () => {
// increments the player season stats by the stats the player had in the game
const res = await fetch('/players', {method: 'PATCH', body: JSON.stringify(playerStats)})
}, 60000)
})
}