How to refresh form data?

I have a Game object which can be controlled via the GamesResource. The first screenshot shows the section with actions and status messages inside the form. The second screenshot shows part of my Game model. Currently everything works (nearly) as desired: The status messages show the correct status, the buttons apply the correct changes to the Game object. But clicking a button, for example "Next round", needs me to refresh the page manually in order to see the change of status from "Current round: 1" to "Current round: 2". How do I achieve an automatic refresh, either of the page, or the section or the whole form? I stumbled upon EditRecord::refreshFormData() as you can see, but couldn't really make it work properly, because funnily enough, the current solution works for Starting and Finishing the game, but not for Next Round.
No description
No description
12 Replies
LeandroFerreira
LeandroFerreira2mo ago
maybe
$livewire->form->fill($livewire->getRecord()->attributesToArray());
$livewire->form->fill($livewire->getRecord()->attributesToArray());
?
sparmin
sparminOP2mo ago
Thanks for the quick reply! Where exactly would I put that? Haven't had much Livewire experience so far
LeandroFerreira
LeandroFerreira2mo ago
instead of $livewire->refreshFormData..
sparmin
sparminOP2mo ago
Oh.. right.. of course🙈 Doesn't work unfortunately. I've even tried solving it quick&dirty by forcing a reload via JS but that also didn't work, the page reloaded but the status message stayed the same: const gameControl = document.getElementById("data.game-control"); if (gameControl) { const button = gameControl.querySelector("button"); if (button) { button.addEventListener("click", function () { location.reload(true); }); } }
LeandroFerreira
LeandroFerreira2mo ago
the page reloaded but the status message stayed the same status message from db?
sparmin
sparminOP2mo ago
Nope, the status of my game control, which I try to refresh automatically, i.e. "Current round: ..."
LeandroFerreira
LeandroFerreira2mo ago
where do you save this info? how are you doing this?
sparmin
sparminOP2mo ago
The Game model has several attributes is_running, is_finished and current_round. Depending on the combination of these attributes I display status messages like "The game has not started yet" or "Current round: ..." including the according buttons "Start game" or "Next round". These buttons are actions which make changes to the Game object. You can see more details in the screenshots above.
LeandroFerreira
LeandroFerreira2mo ago
Depending on the combination of these attributes I display status messages from db, right? Are you updating these attributes in the actions?
sparmin
sparminOP2mo ago
Yes, the Game object is also a db table, so the attributes are saved there. If that was the question. And yes, the actions update the attributes
LeandroFerreira
LeandroFerreira2mo ago
does it work if you force the refresh on the browser?
sparmin
sparminOP2mo ago
Yes. The attributes are correctly updated, the logic works just as intended. And I think I found the reason for this behavior, although I don't fully get it. So the Game has the attribute current_round, which is a foreign key and references the Round object. The Game object also has the method Game::currentRound() though which calls return $this->hasOne(Round::class, 'id', 'current_round'); So when I display the status message 'Current round' . $game->currentRound->sort I call the referenced Round object and the status message does not refresh. But if I change it to 'Current round' . $game->current_round which is the actual attribute of the Game object it does refresh automatically (although it shows the id of the referenced Round object instead of the desired Round::sort attribute. So I assume there's a delay or so by calling the referenced object..
Want results from more Discord servers?
Add your server