Refresh account on update #476
Reference in New Issue
Block a user
Delete Branch "feature/refresh-account"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
/claim #475
Uses the new refresh feature as part of Turbo 8 to broadcast refreshes and morph page updates into the page, avoiding the need to manually refresh the page, or have to build manual updates to parts of the page.
Video attached below, I did the whole setup as demonstrated in the original issue so the main change features about half-way through.
https://github.com/maybe-finance/maybe/assets/1793797/9f324c44-dba4-4a44-bb72-c4f053a1cc14
@@ -1,4 +1,5 @@class Account < ApplicationRecordbroadcasts_refreshes❤️
Nice solution here! Just a few small questions/comments:
1. State after broadcast
Small nitpick, but any chance we could get this to reset to the default "closed" state after the refresh broadcast?
2. ActionCable Adapter
To keep things simple for local development, I was thinking we'd use the
asyncadapter for Action Cable, but I realized thatturbo-railsdeliberately updates this setting to Redis. I'm still getting up to speed with the Rails/Turbo ecosystem, so wondering if there was a rationale behind this published somewhere?Furthermore, since we're using Postgres to back GoodJob, was wondering if we could also get away with Postgres for Action Cable in prod. Curious to hear thoughts on the viability of that to avoid the Redis dep?
Ah good questions!
I've solved the reset, this was happening because the turbo frame was only being cleared - it still had the src set which means when the refresh happened it triggered it to refetch the contents of the frame.
As for the adapter, there's very little documented about it. The docs simply say that the async adapter doesn't support Turbo Stream broadcasting. It doesn't say anything about the Postgres adapter though so that might be worth a try.
I'm in and out at the moment, but I'll have a play with Postgres as the adapter later today.
@JoshAntBrown awesome, thanks for the fix on that!
And regarding Postgres, I'm kind of sitting in the camp of, "let's see how far we can get with it". That way, we avoid the Redis dep for now and only add it if Postgres causes significant problems. @Shpigford any preference here?
@zachgoll My hunch is we're gonna end up using Redis regardless (even if just as a cache), but I'm fine with a limited bit of exploration. I don't think it's worth going to great lengths to avoid it, though.
I've updated the adapter for development, it just uses the Active Record's connection pool so nothing else to configure. I think for caching you can probably use Solid Cache if you want to avoid Redis completely.
Btw, I think we can probably further simplify how the
valuation#createaction works.We could remove valuations/create.turbo_stream.erb and replace it with a simple redirect. This would help reduce a chunk of complexity since we can just have the
accounts#showpage render the current state and then have Turbo refresh it either as part of the redirect or by broadcasting it's changes.The only blocker to doing that right now is how to render the 'Syncing' message. The
account#showview uses@account.status == "SYNCING"to determine whether the account is currently syncing. However, this flag isn't set until the job starts and the job completes almost immediately. Therefore, the user would never see this message.To work around this, we could update the status on the account as part of the
valuation#createaction. I feel like this might be the wrong approach though, and just adds complexity elsewhere.Instead, it might be better if we had a way to query any pending or active sync jobs. This could perhaps be a status on each valuation that gets updated once the valuation has synced, or another model that tracks syncing jobs. It feels a bit beyond the scope of this PR though, especially since a refactoring of the syncing mentioned in #474, so perhaps something to consider at a later date?
@JoshAntBrown I agree that the valuation CRUD flows could be simplified. You're more than welcome to move things around if you see a clear path with that. Most of what is on
mainis a "sketch" that we'll need to tidy up once all the core stuff is in there, so improvements are definitely welcome!Regarding the sync status message—I think what we have here is okay for now even though the user will likely just see a "flash" due to how fast the job is. That message is mostly a UI placeholder until the official designs are completed for how to show these "sync" states.
Thanks, and yeah makes sense. I'll have a think about it and may follow up with a separate PR at some point if I can think of a nice way of tidying up some of the sync management. Hopefully what we have should be good enough for now.
@JoshAntBrown sounds great! I have a PR coming shortly that adds a barebones
Transactionmodel and updates the sync job to incorporate transactions. Once that's in, I think we'll start to get a much clearer picture of how this sync job,Account,Valuation,Transaction, andAccountBalanceshould all be working together.