Skip to content

Commit

Permalink
Add delete photo button
Browse files Browse the repository at this point in the history
  • Loading branch information
miguilimzero committed Sep 9, 2020
1 parent 65ce882 commit c6c7298
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions routes/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
Route::delete('/user', [CurrentUserController::class, 'destroy'])
->name('current-user.destroy');

Route::delete('/user/profile-photo', [CurrentUserController::class, 'deleteProfilePhoto'])
->name('current-user-photo.destroy');

// API...
if (Jetstream::hasApiFeatures()) {
Route::get('/user/api-tokens', [ApiTokenController::class, 'index'])->name('api-tokens.index');
Expand Down
14 changes: 14 additions & 0 deletions src/HasProfilePhoto.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ public function updateProfilePhoto(UploadedFile $photo)
});
}

/**
* Delete the user's profile photo.
*
* @return void
*/
public function deleteProfilePhoto()
{
Storage::disk($this->profilePhotoDisk())->delete($this->profile_photo_path);

$this->forceFill([
'profile_photo_path' => null,
])->save();
}

/**
* Get the URL to the user's profile photo.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Http/Controllers/Inertia/CurrentUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@

class CurrentUserController extends Controller
{
/**
* Delete the current user profile photo.
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteProfilePhoto(Request $request)
{
$request->user()->deleteProfilePhoto();

return back()->with('status', 'profile-photo-deleted');
}

/**
* Delete the current user.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Http/Livewire/UpdateProfileInformationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ public function updateProfileInformation(UpdatesUserProfileInformation $updater)
$this->emit('saved');
}

/**
* Delete user's profile photo.
*
* @param \Laravel\Fortify\Contracts\UpdatesUserProfileInformation $updater
*
* @return void
*/
public function deleteProfilePhoto(UpdatesUserProfileInformation $updater)
{
Auth::user()->deleteProfilePhoto();

return redirect()->route('profile.show');
}

/**
* Get the current user of the application.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
Select A New Photo
</jet-secondary-button>

<jet-button type="button" @click.native.prevent="deleteProfilePhoto" v-if="$page.user.profile_photo_path">
Delete Photo
</jet-button>

<jet-input-error :message="form.error('photo')" class="mt-2" />
</div>

Expand Down Expand Up @@ -103,6 +107,14 @@
},
methods: {
deleteProfilePhoto() {
this.$inertia.delete('/user/profile-photo', {
preserveScroll: true,
}).then(() => {
this.photoPreview = null
});
},
updateProfileInformation() {
if (this.$refs.photo) {
this.form.photo = this.$refs.photo.files[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
Select A New Photo
</x-jet-secondary-button>

@if($this->user->profile_photo_path)
<x-jet-button type="button" wire:click="deleteProfilePhoto">
Delete Photo
</x-jet-button>
@endif

<x-jet-input-error for="photo" class="mt-2" />
</div>
@endif
Expand Down

0 comments on commit c6c7298

Please sign in to comment.