T
TanStack2w ago
correct-apricot

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 ideas
5 Replies
passive-yellow
passive-yellow2w 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-apricot
correct-apricotOP2w ago
Yes, the content types are identical, with only different status field value ('draft' | 'published')
passive-yellow
passive-yellow2w ago
in that case, I would not separate the properties and simply add the status field into the list
deletes the "draft" entry
turns into changing status to published
resets the draft.xxx fields and resets the defaultValues for the published.xxx fields
turns into resetting the form to the draft values
correct-apricot
correct-apricotOP4d 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
passive-yellow4d 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

Did you find this page helpful?