-
Notifications
You must be signed in to change notification settings - Fork 473
feat: Add user profile picture upload functionality #676
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
Open
aj47
wants to merge
1
commit into
AllYourBot:main
Choose a base branch
from
aj47:feature/user-profile-pictures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ gem "redis", ">= 4.0.1" | |
| gem "bcrypt", "~> 3.1.7" | ||
|
|
||
| # Windows does not include zoneinfo files, so bundle the tzinfo-data gem | ||
| gem "tzinfo-data", platforms: %i[windows jruby] | ||
| gem "tzinfo-data", platforms: %i[mswin mswin64 mingw x64_mingw jruby] | ||
|
|
||
| # Reduces boot times through caching; required in config/boot.rb | ||
| gem "bootsnap", require: false | ||
|
|
@@ -57,7 +57,7 @@ gem "omniauth-rails_csrf_protection", "~> 1.0.2" | |
|
|
||
| group :development, :test do | ||
| # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem | ||
| gem "debug", platforms: %i[mri windows] | ||
| gem "debug", platforms: %i[mri mswin mswin64 mingw x64_mingw] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
| gem "timecop" | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
app/javascript/stimulus/profile_picture_upload_controller.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| export default class extends Controller { | ||
| static targets = ["fileInput", "currentAvatar", "previewContainer", "previewImage"] | ||
|
|
||
| connect() { | ||
| // Initialize the controller | ||
| } | ||
|
|
||
| chooseFile() { | ||
| this.fileInputTarget.click() | ||
| } | ||
|
|
||
| previewImage(event) { | ||
| const file = event.target.files[0] | ||
| if (!file) return | ||
|
|
||
| // Validate file type | ||
| if (!file.type.match(/^image\/(jpeg|jpg|png|gif|webp)$/)) { | ||
| alert("Please select a valid image file (JPEG, PNG, GIF, or WebP)") | ||
| this.clearFileInput() | ||
| return | ||
| } | ||
|
|
||
| // Validate file size (5MB limit) | ||
| if (file.size > 5 * 1024 * 1024) { | ||
| alert("File size must be less than 5MB") | ||
| this.clearFileInput() | ||
| return | ||
| } | ||
|
|
||
| // Create preview | ||
| const reader = new FileReader() | ||
| reader.onload = (e) => { | ||
| this.previewImageTarget.src = e.target.result | ||
| this.showPreview() | ||
| } | ||
| reader.readAsDataURL(file) | ||
| } | ||
|
|
||
| removeImage() { | ||
| if (confirm("Are you sure you want to remove your profile picture?")) { | ||
| this.clearFileInput() | ||
| this.hidePreview() | ||
|
|
||
| // Create a hidden input to signal removal | ||
| const removeInput = document.createElement('input') | ||
| removeInput.type = 'hidden' | ||
| removeInput.name = 'person[personable_attributes][remove_profile_picture]' | ||
| removeInput.value = '1' | ||
| this.element.appendChild(removeInput) | ||
| } | ||
| } | ||
|
|
||
| showPreview() { | ||
| this.previewContainerTarget.classList.remove("hidden") | ||
| } | ||
|
|
||
| hidePreview() { | ||
| this.previewContainerTarget.classList.add("hidden") | ||
| } | ||
|
|
||
| clearFileInput() { | ||
| this.fileInputTarget.value = "" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,31 @@ | ||
| <%# locals: (user:, size:, classes: nil) -%> | ||
| <% text_size = size > 6 ? "text-sm" : "text-xs" %> | ||
|
|
||
| <picture | ||
| class=" | ||
| rounded-full | ||
| w-<%= size %> h-<%= size %> | ||
| bg-green-200 text-black | ||
| flex justify-center items-center | ||
| <%= text_size %> | ||
| <%= classes %> | ||
| " | ||
| > | ||
| <%= at_most_two_initials(user.name.initials)&.upcase %> | ||
| </picture> | ||
| <% if user.has_profile_picture? %> | ||
| <picture | ||
| class=" | ||
| rounded-full | ||
| w-<%= size %> h-<%= size %> | ||
| overflow-hidden | ||
| border border-gray-200 dark:border-gray-600 | ||
| <%= classes %> | ||
| " | ||
| > | ||
| <%= image_tag user.profile_picture_url(:small), | ||
| alt: "#{user.name.full}'s profile picture", | ||
| class: "w-full h-full object-cover" %> | ||
| </picture> | ||
| <% else %> | ||
| <picture | ||
| class=" | ||
| rounded-full | ||
| w-<%= size %> h-<%= size %> | ||
| bg-green-200 text-black | ||
| flex justify-center items-center | ||
| <%= text_size %> | ||
| <%= classes %> | ||
| " | ||
| > | ||
| <%= at_most_two_initials(user.name.initials)&.upcase %> | ||
| </picture> | ||
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| This is a text document for testing invalid file uploads |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Nitpicking here: I would prefer these kind of infrastructure changes at least into a separate commit within this PR.