Design patterns for (optimistic) updates of items in a list #2376
Replies: 1 comment
-
BTW I thought it might be helpful to document the two patterns I'm currently using: Pattern 1:
Front-end submits the update via Inertia: // Submit using Inertia (relies on redirect back to the page to reload (all) updated content items)
await router.put(`/review-songs/${id}`, formData, {
only: ['songReviews'], // when redirected back to the (index) page, only reload the song reviews (other properties haven't changed)
preserveState: true, // preserve the state of the page (so the songs are still loaded)
preserveScroll: true, // preserve the scroll position of the page (so the user doesn't have to scroll back to the top)
onSuccess: () => {
// any success followup logic
},
onError: (errors) => {
// any error handling logic
}
}) Back-end updates the database then redirects back to the (index) page: // update the database
...
// redirect back to to the (index) page (the index page will reload all content items)
return back() Pattern 2:
Front-end uses a non-Inertia axios call and directly mutates Inertia's page props with the updated item that is returned: // Non-Inertia axios call:
const response = await axios.put(`/content/${id}`, data)
// Update front-end state by finding and mutating the relevant content item in Inertia's usePage props.content.items
const contentItems = usePage().props.content.items
const index = contentItems.findIndex((i: Content) => i.id === updatedItem.id)
if (index !== -1) {
contentItems[index] = { ...contentItems[index], ...response.data }
} Back-end returns a non-Inertia JSON response containing the updated item: // update the database
...
// return the updated content item
return response()->json($content->only(['id', 'content', 'series', 'guests', 'tags'])); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In Taylor Otwell's Keynote at Laracon US 2024 he demoed Inertia V2 asynchronous updates using a list of switch UI elements.
I'm struggling to find any documentation, demos, tutorials, etc for a design pattern to implement something like this in practice.
Here's my current assumptions:
<Link ... as="checkbox">
checked
property in thedata: { checked: !checked }
property definitionMy actual use-case is more complicated than the simple toggle switches demonstrated by Taylor. It doesn't require optimistic UI updates, but will likely require the use of the manual visits using
router.put()
methods, combined with partial-reload features such as 'only', 'preserveState', and 'preserveScroll', etc.The most useful discussion I've found on the topic was a Laracasts thread.
I'm currently thinking of using two patterns...
...but neither of these patterns are ideal, and the second pattern doesn't feel particularly clean.
I'm keen to find any sample code showing how Taylor's demo (or something similar) should be implemented... and any other elegant patterns others may have found/developed for these types of use-cases.
Beta Was this translation helpful? Give feedback.
All reactions