API for a hook that can manage resource creation and subsequent edits
evert opened this issue · 6 comments
A common pattern in react apps is 'creation of a new resource' and 'subsequent edits'. In our case the component structure for the 'new case' is effectively identical to the 'update' case, except in the 'new item case', we do a POST request.
For demo purposes, I'm going to talk about the creation of a blog article.
This is the general flow:
- User clicks 'create article'
- User goes to an blank 'article' page.
- User writes article and hits 'save'.
- POST request gets sent to some (collection) resource, which creates a new article resource. Server sends back a
Location
header to the new resource and201
created. - User makes more edits
- User hits 'save' again
- PUT request gets sent to the article resource
Later on, the user might visit this article again and make subsequent edits.
This is one suggestion on how this might be called:
type Article = {
title: string,
body: string
}
// A new resource
const {
loading,
error,
resourceState,
setResourceState,
saveResourceState
} = useResourceLifecycle({
parent: parentResource, // what we will use for POST,
initialData: {
title: 'New article',
body: ''
}
});
// An existing resource
const {
loading,
error,
resourceState,
setResourceState,
saveResourceState
} = useResourceLifecycle({
resource: resource, // what we will use for PUT,
});
What is the loading variable intended for?
This seems good for me, I'll have more comments after ive had time to use it in a live environment
What is the difference between setResourceState
and saveResourceState
?
@simistern 'set' is for local changes as the user interacts with the resource. Save actually sends it to the server
I'm not calling it put or post here because it can be either. Maybe 'submit' is a nice name though