-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edit Profile and Stimulus Modal #44
Conversation
class: 'fixed z-20 w-screen h-screen bg-gray-100/75', | ||
data: { controller: 'modal', | ||
modal_target: 'modal', | ||
action: 'turbo:submit-end->modal#submitEnd keydown.esc@window->modal#closeWithEscape mousedown@window->modal#closeWithBackgroundClick'} do %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤮
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤣 I know, it looks awful, and the connection with the actual controller actions is so tenuous.
I was thinking of writing a helper module for stuff like this to make it more readable. It would be really cool if we could make something like Phoenix's verified_routes. That way we wouldn't need to have magic strings. We could round up all the controller actions in the JS at compile time and set a global verified_stimulus_actions map. The tough part would be somehow generating all the valid event triggers to go with them... maybe we wouldn't have to though. Ending up with something like this:
$verified_stimulus_actions = { 'modal': ['closeEnd', 'submitEnd'] }
... then have a macro/resolver of some kind check if the action exists and fail more visibly if it does not:
module StimulusHelper
def verify_action (action)
check_for_action($verified_stimulus_actions)
end
end
...Something like that anyway.
In the meantime, I was thinking something like this just for readability:
module StimulusHelper
def action_list(actions)
actions.join(' ')
end
end
...
### modal_component.html.erb
<tag.div id:'modal-overlay;
data: {
controller: 'modal',
action: StimulusHelper.action_list(["turbo:submit-end->modal#submitEnd", "keydown.esc@window->modal#closeWithEscape"])
} do %>
<%= content %>
<% end %>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gonna make an issue for this actually. Don't want to hold up this PR any longer but the more I think about it the better an idea this seems. Maybe even a gem worth publishing, if I can't find one that does something similar 🤔
@@ -12,6 +12,7 @@ | |||
# | |||
class Profile < ApplicationRecord | |||
belongs_to :user | |||
accepts_nested_attributes_for :user |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
if update_result | ||
redirect_to profile_path(@user) | ||
if @profile.update!(profile_params) | ||
redirect_to profile_path(@user.unique_name), notice: 'Profile was successfully updated.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
During the rewiring I did to make things navigate to the unique name, I defined to_param
on the User
model:
Lines 46 to 48 in 8d4c3cb
def to_param | |
unique_name | |
end |
And also on the Profile
cuz they're basically the same thing:
Lines 21 to 23 in f15e2ed
def to_param | |
user.unique_name | |
end |
This is what all these Rails autogenerated _path
helpers call under the hood when an ActiveRecord
object is passed. The default behavior takes the id
off of the object, so here we customize that behavior to pluck the unique_name
instead.
Long story short, I think you can just do
redirect_to profile_path(@user.unique_name), notice: 'Profile was successfully updated.' | |
redirect_to profile_path(@user), notice: 'Profile was successfully updated.' |
it will call to_param
if you don't specify! This is how I avoided updating every single path reference in the app 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahhhhhhhh, that makes so much sense! I was having trouble understanding that change. Ty!
@@ -13,6 +13,7 @@ | |||
</head> | |||
|
|||
<body> | |||
<%= turbo_frame_tag 'modal'%> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<%= turbo_frame_tag 'modal'%> | |
<%= turbo_frame_tag 'modal' %> |
<%= render ModalComponent.new(title: "Editing Profile") do %> | ||
<%= render "form", profile: @profile, user: @user %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omfg
@@ -14,7 +12,7 @@ | |||
<div id='friend-request-button' class='absolute -top-4 -right-2 '> | |||
<span class='bg-blue-200 py-1 px-2 rounded-full opacity-50'>Request Friend</span> | |||
<% if current_user == @user %> | |||
<%= link_to 'Edit', edit_profile_path(@user), id: 'edit-profile-button', class: 'bg-blue-200 py-2 px-2 rounded-full w-9', data: { turbo_frame: 'edit-profile-modal' } %> | |||
<%= link_to 'Edit', edit_profile_path(@user), id: 'edit-profile-button', class: 'bg-blue-200 py-2 px-2 rounded-full w-9', data: { turbo_frame: 'modal' } %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the previous thing I mentioned is why this navigates via the unique_name, same thing
Honestly inspiring, I was def trying to do too many things at once. This is an awesome incremental change |
WIP Reason: along the way I broke the form! but it looks nice. Fixing shortlyAdds an 'edit' profile form!
Introduces ViewComponent for reusable component views!
Adds the modal component!
Fixes the logout bug!
Update: Still needs tests, but ready to go otherwise.