Skip to content
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

[1.x] Add delete profile photo button #110

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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