T
TanStack2mo ago
distinguished-blush

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
vicious-gold
vicious-gold2mo 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'?
distinguished-blush
distinguished-blushOP2mo ago
Yes, the content types are identical, with only different status field value ('draft' | 'published')
vicious-gold
vicious-gold2mo 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
distinguished-blush
distinguished-blushOP2mo 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?
vicious-gold
vicious-gold2mo 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?