Closed Qsppl closed 9 months ago
I tried to solve the problem through a single model descriptor that does not have an Id and is always in Draft mode. It didn't work out either. All components without a defined model receive the same model after the first model is created in one of the components:
https://github.com/hybridsjs/hybrids/blob/main/src/store.js#L1528
The draft model when used without an id falls back to the undefined
id - so, indeed, if you have multiple components using a draft mode of the same original model, and not set id, they will all have the same "draft" instance. That behavior is very rare to have multiple "create" forms of the same model at once on the screen. Otherwise, the store would have to create id on the fly - but how it could then say, that it is not the normal id of the original model - so it would be very complicated to make it work like this.
I think what you can do, is to add the id
setting to your draftComment
- so then if it is "edit" mode - the component has set commentId
property.
{
commentId: '',
draftComment: store(Comment, { draft: true, id: 'commentId' }),
comment: store(Comment, { id: 'commentId' }),
}
I'll try to make a working example tomorrow.
This is the case from the editable table
I've started building an example, and I think I finally understand your case. If I am correct, you have some sort of rows, and you can add a comment to each, but it is a "create" form - and in your case, a user can open multiple forms at once.
Unfortunately, the current implementation of the draft model fails in that case, as I wrote earlier, you can't have multiple instances of the draft without passing id.
I'll try to rethink what can be done for you. In the meantime, I hope that you have some workaround of the issue, and it does not block you.
EDIT: The easiest workaround could be to not use draft when adding a comment, just use store.set()
directly with new data when the form is submitted. I know that then you miss validation from the store (if you use it).
I solved the problem. I'm showing only one on-demand creation form.
But it is very inconvenient to control the components so that only one form is open at a time.
I also had to create two components:
contact-comment-remind.component.ts
contact-comment-remind-create.component.ts
Instead of one:
contact-comment-remind.component.ts
Duplicate html and css markup. This becomes a problem when there are more than 10 models in the table. Each model requires two identical components instead of one.
But this solution supports Store validation.
Good to know that you have temporary solution. I'll try to rethink it as soon as possible, and back with more general way to handle that case.
After all, I figured out, that the store can create a local temporary ID for the draft model, as it is very unlikely that it might hit an existing ID of the storage (for UUID v4 the collision is negligible).
If it is possible, please test it out before merging and release, using codesandbox build:
"dependencies": {
"hybrids": "https://pkg.csb.dev/hybridsjs/hybrids/commit/e530588f/hybrids"
},
You can use it directly in your code or using codesandbox (link in the PR mentioned above).
Please try v8.2.7
where you can safely use multiple instances of the component that uses store draft without id. It should work now as you expected.
Please try
v8.2.7
where you can safely use multiple instances of the component that uses store draft without id. It should work now as you expected.
Thank you very much, I didn’t expect you to add this functionality so quickly. I'll try to use this in the next similar case. The task that I solved was already in production.
Example:
https://codepen.io/qsppl/pen/mdvKrWP?editors=1011
We create several forms with the model in “Draft” mode.![image](https://github.com/hybridsjs/hybrids/assets/22717464/98437fa4-d771-4445-881a-5d88f1167ef6)
Let's try to save them.![image](https://github.com/hybridsjs/hybrids/assets/22717464/cf9aa485-fcfd-4b03-985b-3a677bddd205)
We see a bug:
I tried to write a solution without linking Draft to "id", but it didn't work:
https://codepen.io/qsppl/pen/wvNEjBw?editors=1011