Skip to content

Commit

Permalink
🚸 Refactor profile picture upload process to use a Livewire component
Browse files Browse the repository at this point in the history
  • Loading branch information
betsyecastro committed Nov 19, 2024
1 parent 671cf4a commit fd28698
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/ProfilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function update(Profile $profile, string $section, ProfileUpdateRequest $

public function updateImage(Profile $profile, ProfileImageRequest $request): RedirectResponse
{
return redirect()->route('profiles.edit', [$profile->slug, 'information'])->with('flash_message', $profile->processImage($request->file('image'), 'images'));
return redirect()->route('profiles.show', [$profile->slug, 'information'])->with('flash_message', $profile->processImage($request->file('image'), 'images'));
}

/**
Expand Down
40 changes: 40 additions & 0 deletions app/Http/Livewire/ProfilePictureEditorModal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Livewire;

use App\Profile;
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Http\Requests\Concerns\HasImageUploads;

class ProfilePictureEditorModal extends Component
{
use WithFileUploads;
use HasImageUploads;
public Profile $profile;
public $image;
public $user;

public function mount()
{
$this->user = auth()->user();
}

public function updatedImage()
{
$this->validate([
'image' => $this->uploadedImageRules(),
]);
}

public function submit()
{
$msg = $this->profile->processImage($this->image);
return redirect(route('profiles.show', $this->profile))->with('flash_message', $msg, 'images');
}

public function render()
{
return view('livewire.profile-picture-editor-modal');
}
}
37 changes: 37 additions & 0 deletions app/Http/Requests/ProfilePictureUpdateRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Requests;

use App\Http\Requests\Concerns\HasImageUploads;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProfilePictureUpdateRequest extends FormRequest
{
use HasImageUploads;

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return ['data.*.image' => $this->uploadedImageRules()];
// return ['image' => 'required|image|max:220'];
}

/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'image.max' => $this->uploadedImageMessages('max'),
];
}


}
12 changes: 0 additions & 12 deletions public/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/js/app.js": "/js/app.js?id=d764e6cb74d3a3a53a24d6b53f91e9c0",
"/js/app.js": "/js/app.js?id=f1170fe3e2888e47851541584fd2b3e9",
"/js/manifest.js": "/js/manifest.js?id=dc9ead3d7857b522d7de22d75063453c",
"/css/app.css": "/css/app.css?id=bff46e69abe7a97b008296b99e4abadd",
"/js/vendor.js": "/js/vendor.js?id=4d3313683b3a2faf8ca0278ce47f3880"
Expand Down
12 changes: 0 additions & 12 deletions resources/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ var profiles = (function ($, undefined) {
$(file_input).siblings('.invalid-feedback').removeClass('d-block');
};

/**
* Submits img update
*
* @param {Event} event the triggered event
*/
const submit_img_update = function (event) {
const file_input = event.target;
$(file_input).closest('form').submit();
};

/**
* Reindex numbers in field ids, names, and labels to match sort order,
* e.g. if the first item's name is "thing[3]", rename it to "thing[0]"
Expand Down Expand Up @@ -557,7 +547,6 @@ var profiles = (function ($, undefined) {
deobfuscate_mail_links: deobfuscate_mail_links,
on_list_updated: on_list_updated,
preview_selected_image: preview_selected_image,
submit_img_update : submit_img_update,
replace_icon: replace_icon,
registerTagEditors: registerTagEditors,
registerProfilePickers: registerProfilePickers,
Expand All @@ -582,7 +571,6 @@ $(function() {
//show preview of uploaded image
$('input[type="file"]').on('change', function(e) {
profiles.preview_selected_image(e);
profiles.submit_img_update(e);
});

// enable drag and drop sorting for items with sortable class
Expand Down
36 changes: 36 additions & 0 deletions resources/views/livewire/profile-picture-editor-modal.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<div id="profile_picture_editor" class="modal fade" tabindex="-1" role="dialog" aria-hidden="false" wire:ignore.self>
<div class="modal-dialog modal-dialog-scrollable modal-md">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mt-0"><i class="fas fa-camera"></i> Update Profile Picture</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" wire:click="closeModal">
<span aria-hidden="true">&times;</span>
</button>
</div>

<div class="modal-body">
<div class="container-md">
<form wire:submit.prevent="submit" enctype="multipart/form-data" method="GET">
@csrf
<img id="file-img" src="{{ $profile->imageUrl }}" wire:ignore/>
<br>
<br>
<div class="control-group">
<div class="controls">
{!! Form::file('image', ['id' => 'file', 'name' => 'image', 'required' => 'true', 'accept' => 'image/*', 'wire:model' => 'image', 'class' => 'd-none form-control']) !!}
@error('image') <span class="error">{{ $message }}</span> @enderror
<label for="file" class="btn btn-secondary btn-block"><i class="fas fa-plus"></i> Select Image</label>
{!! Form::inlineErrors('image') !!}
</div>
</div>
<button type="submit" class="btn btn-primary btn-block" data-toggle="replace-icon" data-newicon="fas fa-sync fa-spin" data-inputrequired="#file">
<i class="fas fa-upload"></i> Save Image
</button>
</form>
<br>
<br>
</div>
</div>
</div>
</div>
</div>
22 changes: 5 additions & 17 deletions resources/views/profiles/edit/information.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,12 @@
<div class="row">
@foreach($data as $info)
<div class="col col-md-4">
{!! Form::open(['url' => route('profiles.update-image', [$profile->slug]), 'method' => 'POST', 'files' => true]) !!}
<label for="file">Icon</label>
<img id="file-img" class="profile_photo" src="{{ $profile->imageUrl }}" />
<br />
<br />
<div class="control-group">
<div class="controls">
{!! Form::file('image', ['id' => 'file', 'name' => 'image', 'required' => 'true', 'accept' => 'image/*', 'class' => 'd-none form-control']) !!}
<label for="file" class="btn btn-secondary btn-block"><i class="fas fa-plus"></i> Select Image</label>
{!! Form::inlineErrors('image') !!}
</div>
</div>
<button type="submit" class="btn btn-primary btn-block" data-toggle="replace-icon" data-newicon="fas fa-sync fa-spin" data-inputrequired="#file">
<i class="fas fa-upload"></i> Replace Image
</button>
{!! Form::close() !!}
<br>
<br>
<img class="profile_photo" src="{{ $profile->image_url }}" alt="{{ $profile->full_name }}">
<a class="btn-sm btn-info btn" href="#" data-target="#profile_picture_editor" data-toggle="modal" role="button">
<i class="fas fa-camera"></i> Edit
</a>
<livewire:profile-picture-editor-modal :profile="$profile">
{!! Form::open(['url' => route('profiles.update-banner', [$profile->slug]), 'method' => 'POST', 'files' => true]) !!}
<label for="banner">Banner</label>
<img id="banner-img" class="profile_photo" src="{{ $profile->banner_url }}" />
Expand Down
4 changes: 4 additions & 0 deletions resources/views/profiles/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
@if(!$information->fancy_header)
<div class="col-md-5 col-sm-6">
<img class="profile_photo" src="{{ $profile->image_url }}" alt="{{ $profile->full_name }}">
<a class="btn-sm btn-info btn" href="#" data-target="#profile_picture_editor" data-toggle="modal" role="button">
<i class="fas fa-camera"></i> Edit
</a>
<livewire:profile-picture-editor-modal :profile="$profile">
</div>
@endif
<div class="@if($information->fancy_header)col-lg-5 @else col-md-7 col-sm-6 @endif">
Expand Down

0 comments on commit fd28698

Please sign in to comment.