dynamic defaultValues
I am implementing a CMS. We have two tabs : "Draft" and "Published". They are showing different fields of the TS Form:
draft.content
, draft.title
and published.content
, published.title
.
On the "Draft" tab there are "Publish" and "Save draft" buttons.
- Publish button is submitting the data as "published" entry, and then deletes the "draft" entry, and resets the draft.xxx
fields, and resets the default values for the published.xxx
fileds.
- Save draft button is saving the values as "draft" entry (idk if the form should be reset in this case?...)
On the "Published" tab there are "Save as Draft" and "Update published" buttons.
- Save as Draft button would create a "draft" entry in db and reset the published.xxx
fields back to initial values (that were fetched from API)
- Update published button would update a "published" entry in db (same as with "Save draft" button, idk, if the form should be reset in this case)
My current solution is to use useEffect
every time the fetched data changes and manually reset the form content to the updated data..
Does anybody did something similar? I would appreciate any ideas5 Replies
passive-yellow•2w ago
how different are the properties between published and draft? Would it make sense to share one state (content, title) and have a
status: 'draft' | 'published'
?correct-apricotOP•2w ago
Yes, the content types are identical, with only different
status
field value ('draft' | 'published'
)passive-yellow•2w ago
in that case, I would not separate the properties and simply add the
status
field into the list
deletes the "draft" entryturns into changing
status
to published
resets theturns into resetting the form to the draft valuesdraft.xxx
fields and resets the defaultValues for thepublished.xxx
fields
correct-apricotOP•4d ago
Having one state mean that user won't be able to edit say, draft, for a while, then for a moment switch back to "published" to check something, then switch back to draft? because that would reset the draft to default values, right?
passive-yellow•4d ago
no? because that would just change the
status
field twice
oh, I see what you mean. So the published form uses the defaults of the draft, while the draft starts out as empty