Skip to content

Commit

Permalink
[EM-763][EVENTS] add image field to event creation and update (#5685)
Browse files Browse the repository at this point in the history
* [EVENTS] add image field to event creation and update

* [EVENTS] fix review feedbacks
  • Loading branch information
elvino29 committed May 6, 2021
1 parent 820deb8 commit be45b99
Show file tree
Hide file tree
Showing 33 changed files with 560 additions and 256 deletions.
4 changes: 2 additions & 2 deletions front/app.js
Expand Up @@ -353,9 +353,9 @@ class App {
});
}

runImageCropper(inputFileElement, inputCroppedImageElement) {
runImageCropper(inputFileElement, inputCroppedImageElement, options = { a: 1, w: 500, h: 500 }) {
System.import('services/utils/imageCropper').catch((error) => { throw error; }).then((module) => {
module.default(inputFileElement, inputCroppedImageElement);
module.default(inputFileElement, inputCroppedImageElement, options);
});
}

Expand Down
116 changes: 61 additions & 55 deletions front/services/utils/imageCropper.js
Expand Up @@ -8,44 +8,45 @@ let cropper;
let fileElement;
let croppedImageElement;

export default (inputFileElement, inputCroppedImageElement) => {
fileElement = inputFileElement;
croppedImageElement = inputCroppedImageElement;
function updatePreviewImages(src, circle = false) {
const previewContainer = dom('.image-uploader--preview');

const files = inputFileElement.files;
const containerElement = find(previewContainer, '.preview-image--container');
containerElement.style.backgroundImage = `url(${src})`;

if (!files || 1 > files.length) {
return;
if (false === circle) {
addClass(containerElement, 'preview-image--container-rectangle');
}
show(previewContainer);
hide(dom('.image-uploader--label'));
}

const file = files[0];
async function handleCropAction(options) {
const canvas = cropper.getCroppedCanvas({
width: options.w,
height: options.h,
});

if (URL) {
displayCropperModal(URL.createObjectURL(file));
} else if (FileReader) {
const reader = new FileReader();
reader.onload = () => displayCropperModal(reader.result);
reader.readAsDataURL(file);
}
};
const dataUrl = await canvas.toDataURL();

function displayCropperModal(url) {
modal = render(
<Modal
key={url}
contentCallback={() => getModalContent(url)}
withClose={false}
/>,
dom('#modal-wrapper')
);
updatePreviewImages(dataUrl, options.w === options.h);

cropper = new Cropper(dom('.image-cropper--container img'), {
aspectRatio: 1,
viewMode: 2,
});
croppedImageElement.value = dataUrl;
fileElement.value = '';

modal.hideModal();
}

function getModalContent(url) {
function handleCancelAction() {
cropper.destroy();
cropper = null;

fileElement.value = '';

modal.hideModal();
}

function getModalContent(url, options) {
return (
<div>
<div className={'image-cropper--container'}>
Expand All @@ -54,41 +55,46 @@ function getModalContent(url) {

<div className="b__nudge--top-15 text--center">
<a className={'btn'} onClick={handleCancelAction}>Annuler</a>
<a className="btn btn--blue b__nudge--left-small" onClick={handleCropAction}>Valider</a>
<a className="btn btn--blue b__nudge--left-small" onClick={ () => handleCropAction(options) }>Valider</a>
</div>
</div>
);
}

function handleCropAction() {
const canvas = cropper.getCroppedCanvas({
width: 500,
height: 500,
});

const dataUrl = canvas.toDataURL();

updatePreviewImages(dataUrl);

croppedImageElement.value = dataUrl;
fileElement.value = '';
function displayCropperModal(url, options) {
modal = render(
<Modal
key={url}
contentCallback={() => getModalContent(url, options)}
withClose={false}
/>,
dom('#modal-wrapper')
);

modal.hideModal();
cropper = new Cropper(dom('.image-cropper--container img'), {
aspectRatio: options.a,
viewMode: 2,
});
}

function handleCancelAction() {
cropper.destroy();
cropper = null;

fileElement.value = '';
export default (inputFileElement, inputCroppedImageElement, options = {}) => {
fileElement = inputFileElement;
croppedImageElement = inputCroppedImageElement;

modal.hideModal();
}
const files = inputFileElement.files;

function updatePreviewImages(src) {
const previewContainer = dom('.image-uploader--preview');
if (!files || 1 > files.length) {
return;
}

find(previewContainer, '.preview-image--container').style.backgroundImage = `url(${src})`;
show(previewContainer);
hide(dom('.image-uploader--label'));
}
const file = files[0];

if (URL) {
displayCropperModal(URL.createObjectURL(file), options);
} else if (FileReader) {
const reader = new FileReader();
reader.onload = () => displayCropperModal(reader.result, options);
reader.readAsDataURL(file);
}
};
2 changes: 1 addition & 1 deletion front/style/layout/_forms-em.scss
Expand Up @@ -272,7 +272,7 @@
box-sizing: border-box;
font-family: $font-roboto;
position: relative;
height: 140px;
min-height: 140px;
border: dashed 1px $lines-grey;
width: 100%;
display: flex;
Expand Down
6 changes: 6 additions & 0 deletions front/style/voting-platform/_candidacy.scss
Expand Up @@ -132,6 +132,12 @@
color: $blue;
font-style: italic;
text-align: center;

&-rectangle {
width: 680px;
height: 368px;
border-radius: 0;
}
}

.candidate-gender {
Expand Down
9 changes: 8 additions & 1 deletion src/Controller/EnMarche/CitizenActionManagerController.php
Expand Up @@ -110,7 +110,13 @@ public function editAction(
CitizenActionCommandHandler $handler
): Response {
$command = CitizenActionCommand::createFromCitizenAction($action);
$form = $this->createForm(CitizenActionCommandType::class, $command)
$form = $this->createForm(
CitizenActionCommandType::class,
$command,
[
'image_path' => $action->getImagePath(),
]
)
->handleRequest($request)
;

Expand All @@ -127,6 +133,7 @@ public function editAction(
return $this->render('citizen_action_manager/edit.html.twig', [
'citizen_project' => $project,
'project_hosts' => $citizenProjectManager->getCitizenProjectAdministrators($project),
'event' => $action,
'citizen_action_form' => $form->createView(),
]);
}
Expand Down
Expand Up @@ -76,7 +76,7 @@ public function candidateAction(UserInterface $adherent, Committee $committee, R
$isCreation = null === $candidacy->getId();

$form = $this
->createCandidacyForm($candidacy)
->createCandidacyForm($candidacy, ['image_path' => $candidacy->getImagePath()])
->handleRequest($request)
;

Expand Down
8 changes: 7 additions & 1 deletion src/Controller/EnMarche/CommitteeEventManagerController.php
Expand Up @@ -56,7 +56,13 @@ public function __construct(EventRegistrationRepository $eventRegistrationReposi
*/
public function editAction(Request $request, CommitteeEvent $event, EventCommandHandler $handler): Response
{
$form = $this->createForm(EventCommandType::class, $command = EventCommand::createFromEvent($event));
$form = $this->createForm(
EventCommandType::class,
$command = EventCommand::createFromEvent($event),
[
'image_path' => $event->getImagePath(),
]
);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
Expand Down
Expand Up @@ -98,7 +98,13 @@ public function eventsCreateAction(
public function editAction(Request $request, BaseEvent $event, EventCommandHandler $handler): Response
{
$form = $this
->createForm(EventCommandType::class, $command = EventCommand::createFromEvent($event))
->createForm(
EventCommandType::class,
$command = EventCommand::createFromEvent($event),
[
'image_path' => $event->getImagePath(),
]
)
->handleRequest($request)
;

Expand All @@ -113,6 +119,7 @@ public function editAction(Request $request, BaseEvent $event, EventCommandHandl
}

return $this->renderTemplate('event_manager/event_edit.html.twig', [
'event' => $event,
'form' => $form->createView(),
]);
}
Expand Down
Expand Up @@ -81,7 +81,10 @@ public function institutionalEventsEditAction(
->createForm(
InstitutionalEventCommandType::class,
$command = InstitutionalEventCommand::createFromInstitutionalEvent($institutionalEvent),
['view' => InstitutionalEventCommandType::EDIT_VIEW]
[
'view' => InstitutionalEventCommandType::EDIT_VIEW,
'image_path' => $institutionalEvent->getImagePath(),
]
)
->handleRequest($request)
;
Expand All @@ -95,6 +98,7 @@ public function institutionalEventsEditAction(
}

return $this->renderTemplate('institutional_events/create.html.twig', [
'event' => $institutionalEvent,
'form' => $form->createView(),
]);
}
Expand Down
Expand Up @@ -69,7 +69,13 @@ public function editCandidatureAction(Request $request, UserInterface $adherent)
}

$form = $this
->createForm(TerritorialCouncilCandidacyType::class, $candidacy)
->createForm(
TerritorialCouncilCandidacyType::class,
$candidacy,
[
'image_path' => $candidacy->getImagePath(),
]
)
->handleRequest($request)
;

Expand Down Expand Up @@ -248,7 +254,7 @@ public function acceptInvitationAction(
$acceptedBy->setIsPublicFaithStatement($invitedBy->isPublicFaithStatement());

$form = $this
->createForm(TerritorialCouncilCandidacyType::class, $acceptedBy)
->createForm(TerritorialCouncilCandidacyType::class, $acceptedBy, ['image_path' => $acceptedBy->getImagePath()])
->handleRequest($request)
;

Expand Down
4 changes: 4 additions & 0 deletions src/Entity/ImageOwnerInterface.php
Expand Up @@ -17,4 +17,8 @@ public function getImage(): ?UploadedFile;
public function setImage(?UploadedFile $image): void;

public function getImagePath(): string;

public function isRemoveImage(): bool;

public function setRemoveImage(bool $value): void;
}
12 changes: 12 additions & 0 deletions src/Entity/ImageTrait.php
Expand Up @@ -18,6 +18,8 @@ trait ImageTrait
*/
protected $imageName;

private $removeImage = false;

public function setImageName(?UploadedFile $image): void
{
$this->imageName = null === $image ? null :
Expand Down Expand Up @@ -58,4 +60,14 @@ public function getImagePath(): string
{
return $this->getImageName();
}

public function isRemoveImage(): bool
{
return $this->removeImage;
}

public function setRemoveImage(bool $value): void
{
$this->removeImage = $value;
}
}
12 changes: 0 additions & 12 deletions src/Entity/VotingPlatform/Designation/BaseCandidacy.php
Expand Up @@ -81,8 +81,6 @@ abstract class BaseCandidacy implements CandidacyInterface, AlgoliaIndexedEntity
*/
protected $image;

private $removeImage = false;

/**
* @var CandidacyInvitationInterface[]|Collection
*/
Expand Down Expand Up @@ -156,16 +154,6 @@ public function getImagePath(): string
;
}

public function isRemoveImage(): bool
{
return $this->removeImage;
}

public function setRemoveImage(bool $value): void
{
$this->removeImage = $value;
}

public function getFirstName(): string
{
return $this->getAdherent()->getFirstName();
Expand Down
4 changes: 0 additions & 4 deletions src/Entity/VotingPlatform/Designation/CandidacyInterface.php
Expand Up @@ -29,10 +29,6 @@ public function getBiography(): ?string;

public function setBiography(?string $biography): void;

public function isRemoveImage(): bool;

public function setRemoveImage(bool $value): void;

public function getType(): string;

public function getAdherent(): Adherent;
Expand Down

0 comments on commit be45b99

Please sign in to comment.