I’m working on a Phoenix Liveview project and in this project I have a “Post” that contains
Now, this post is publically available and can be edited by any authenticated users. However I want to allow only one person to edit the post at a time. So if someone is currently editing the post, other users won’t be able to edit the Post(I’ll show a “Someone is editing” message).
But I’m not exactly sure how to approach this problem. I have thought of two possible solutions:
Disable editing on database level: I’m using ecto and postgres to save the post. I can set an
truewhen someone is editing and, on UI side I’ll check for this flag and disable/enable editing. However, it has one shortcoming. I’m not sure when to set this flag back to
false. Let’s say someone is currently editing the form, and they just close the tab(without saving or anything) then how will the
editflag be set to
Using Phoenix presence: I can use Phoenix presence to track the user when they edit the form. I can then use
Presence.listto know if someone is editing the post or not. This will handle user suddenly dropping off without saving anything. However I’m not sure if this solution is viable or not?
I wanted to ask the community on how they would approach this problem? And some possible solutions to this problem?
Thanks and have a nice day!