-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from alimranahmed/187-cosmetic-changes-in-adm…
…in-panel #187: User photo and favicon can also be changed
- Loading branch information
Showing
17 changed files
with
201 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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,91 @@ | ||
<?php | ||
|
||
namespace App\Livewire\Backend\Config; | ||
|
||
use App\Models\Config; | ||
use Exception; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Support\Facades\Storage; | ||
use Livewire\Attributes\Validate; | ||
use Livewire\Component; | ||
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile; | ||
use Livewire\WithFileUploads; | ||
|
||
class ImageForm extends Component | ||
{ | ||
use WithFileUploads; | ||
|
||
#[Validate('nullable|file|mimes:ico,png,svg||max:200')] // 200MB | ||
public ?TemporaryUploadedFile $favicon = null; | ||
|
||
#[Validate('nullable|image|max:1024')] // 1MB | ||
public ?TemporaryUploadedFile $user_photo = null; | ||
|
||
public array $existingPaths; | ||
|
||
public function mount(): void | ||
{ | ||
$this->existingPaths = [ | ||
Config::FAVICON => Config::getPath(Config::FAVICON), | ||
Config::USER_PHOTO => Config::getPath(Config::USER_PHOTO), | ||
]; | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
public function saveConfigFile(string $configName): void | ||
{ | ||
if ($this->{$configName} === null) { | ||
return; | ||
} | ||
|
||
$this->validate(); | ||
|
||
if (! in_array($configName, [Config::FAVICON, Config::USER_PHOTO], true)) { | ||
throw new Exception("Invalid config name: \"$configName\" for upload file."); | ||
} | ||
|
||
$this->deleteConfigMedia($configName); | ||
|
||
$path = $this->{$configName}->storeAs( | ||
path: 'configs', | ||
name: "$configName.".$this->{$configName}->getClientOriginalExtension() | ||
); | ||
|
||
$this->insertConfig($configName, $path); | ||
|
||
$this->redirect(route('backend.config.index')); | ||
} | ||
|
||
public function resetConfigFile(string $configName): void | ||
{ | ||
Config::query()->where('name', $configName)->delete(); | ||
$this->deleteConfigMedia($configName); | ||
$this->redirect(route('backend.config.index')); | ||
} | ||
|
||
private function deleteConfigMedia(string $configName): void | ||
{ | ||
/** @var Config $config */ | ||
$config = Config::query()->where('name', $configName)->first(); | ||
if ($config) { | ||
Storage::delete($config->value); | ||
} | ||
} | ||
|
||
private function insertConfig($configName, string $path): void | ||
{ | ||
Config::query()->updateOrCreate([ | ||
'name' => $configName, | ||
], [ | ||
'value' => $path, | ||
'is_media' => true, | ||
]); | ||
} | ||
|
||
public function render(): View | ||
{ | ||
return view('livewire.backend.config.image-form'); | ||
} | ||
} |
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 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,6 +1,6 @@ | ||
@props(['type' => 'submit']) | ||
<button type="{{$type}}" | ||
wire:loading.attr="disabled" wire:loading.class="cursor-wait" | ||
wire:loading.attr="disabled" wire:loading.class="cursor-wait animate-pulse" | ||
{{$attributes->merge(['class' => 'block rounded-md bg-indigo-600 px-3 py-2 text-center text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600'])}}> | ||
{{$slot}} | ||
</button> |
This file contains 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 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 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 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 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 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 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
39 changes: 39 additions & 0 deletions
39
resources/views/livewire/backend/config/image-form.blade.php
This file contains 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,39 @@ | ||
@php use App\Models\Config;use Illuminate\Support\Str; @endphp | ||
<div> | ||
<div class="flex"> | ||
@foreach([Config::FAVICON, Config::USER_PHOTO] as $file_name) | ||
<form class="mr-3" wire:submit="saveConfigFile('{{$file_name}}')"> | ||
<div class="text-center">{{Str::title($file_name)}}</div> | ||
<img src="{{${$file_name} ? ${$file_name}->temporaryUrl() : $existingPaths[$file_name] }}" class="w-32 h-32 bg-indigo-100 border p-1 rounded mb-2" alt="Favicon"> | ||
|
||
<label for="{{$file_name}}" class="cursor-pointer mt-1"> | ||
<input id="{{$file_name}}" type="file" wire:model="{{$file_name}}" class="sr-only"/> | ||
<span class="text-slate-900 rounded px-2 leading-6 text-sm bg-indigo-300 hover:bg-indigo-400 py-1">Change</span> | ||
</label> | ||
|
||
@if(${$file_name} && !$errors->has('file_name')) | ||
<button class="text-slate-900 rounded px-2 leading-6 text-sm bg-green-300 hover:bg-green-400" | ||
wire:target="saveFavicon" | ||
wire:loading.attr="disabled" | ||
wire:loading.class="cursor-wait animate-pulse">Save | ||
</button> | ||
@else | ||
<span class="text-slate-900 rounded px-2 leading-6 text-sm bg-red-300 hover:bg-red-400 py-1 cursor-pointer" | ||
wire:target="resetConfigFile" | ||
wire:loading.attr="disabled" | ||
wire:loading.class="cursor-wait animate-pulse" | ||
wire:click="resetConfigFile('{{$file_name}}')" | ||
>Reset</span> | ||
@endif | ||
</form> | ||
@endforeach | ||
</div> | ||
|
||
@error('favicon') | ||
<div class="text-red-500 text-sm italic">{{ $message }}</div> | ||
@enderror | ||
|
||
@error('user_photo') | ||
<div class="text-red-500 text-sm italic">{{ $message }}</div> | ||
@enderror | ||
</div> |
This file contains 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