-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3af308
commit 5a69ef3
Showing
4 changed files
with
114 additions
and
2 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,58 @@ | ||
<?php | ||
|
||
namespace App\Twig; | ||
|
||
use App\Entity\Book; | ||
use App\Service\BookFileSystemManager; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
use Symfony\UX\LiveComponent\Attribute\LiveAction; | ||
use Symfony\UX\LiveComponent\Attribute\LiveProp; | ||
use Symfony\UX\LiveComponent\ComponentToolsTrait; | ||
use Symfony\UX\LiveComponent\DefaultActionTrait; | ||
use Symfony\UX\LiveComponent\ValidatableComponentTrait; | ||
|
||
#[AsLiveComponent()] | ||
class UploadBookPicture extends AbstractController | ||
{ | ||
use DefaultActionTrait; | ||
use ValidatableComponentTrait; | ||
use ComponentToolsTrait; | ||
|
||
#[LiveProp()] | ||
public Book $book; | ||
|
||
#[LiveProp()] | ||
public bool $isEditing = false; | ||
|
||
public ?string $flashMessage = null; | ||
|
||
#[LiveAction] | ||
public function activateEditing(): void | ||
{ | ||
$this->isEditing = true; | ||
} | ||
|
||
#[LiveAction] | ||
public function uploadFiles(Request $request, BookFileSystemManager $fileSystemManager, EntityManagerInterface $entityManager): void | ||
{ | ||
|
||
/** @var UploadedFile $symfonyFile */ | ||
$symfonyFile = $request->files->getIterator()->current(); | ||
|
||
$book = $fileSystemManager->uploadBookCover($symfonyFile, $this->book); | ||
|
||
$entityManager->persist($book); | ||
$entityManager->flush(); | ||
$this->dispatchBrowserEvent('manager:flush'); | ||
$this->isEditing = false; | ||
|
||
$this->flashMessage = ' book updated'; | ||
|
||
$this->dispatchBrowserEvent('manager:flush'); | ||
|
||
} | ||
} |
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,25 @@ | ||
<span {{ attributes.defaults(stimulus_controller('inline-edit-book')) }}> | ||
{% if isEditing %} | ||
<form class="btn-group"> | ||
|
||
<input type="file" name="cover" class="form-control"/> | ||
<button data-action="live#action" class="btn btn-sm btn-outline-dark" data-action-name="files|uploadFiles"> | ||
Upload | ||
</button> | ||
</form> | ||
{% else %} | ||
|
||
<button | ||
data-action="live#action" | ||
data-action-name="activateEditing" | ||
class="btn btn-sm btn-outline-dark" | ||
title="Click to upload cover!" | ||
> | ||
<i class="bi bi-upload"></i> Upload new cover | ||
</button> | ||
|
||
{% if flashMessage %} | ||
<i class="bi bi-check alert-remove"></i> {{ flashMessage }} | ||
{% endif %} | ||
{% endif %} | ||
</span> |