Skip to content

Commit

Permalink
Add a new step allowing to validate the secret repartition (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyrech authored Oct 23, 2024
1 parent 61f0609 commit e660e27
Show file tree
Hide file tree
Showing 13 changed files with 296 additions and 41 deletions.
57 changes: 53 additions & 4 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ textarea {
.user-list.hide-not-matching .user-item.not-matching {
display: none;
}
.user-list .user-item:hover {
.user-list:not(.no-hover) .user-item:hover {
background-color: var(--color-gray-light);
cursor: pointer;
}
Expand Down Expand Up @@ -871,6 +871,19 @@ textarea {
.checked-users-summary .user-summary span[data-uncheck-user] {
cursor: pointer;
}
.validate-step .user-summary {
display: flex;
align-items: center;
width: 30%;
}
.validate-step .text {
display: flex;
justify-content: center;
width: 20%;
}
.validate-step .shuffle-button {
margin: 2rem;
}

/**
* Result block
Expand Down Expand Up @@ -1289,18 +1302,54 @@ textarea {
list-style: none;
padding-left: 0;
}
.faq summary {
position: relative;
padding-right: 2em;
}
.faq-anchor {
position: absolute;
right: 0;
}
details {
margin-bottom: 2em;
border: 1px solid var(--color-gray);
border-radius: var(--border-radius);
padding: 1em 1em 0;
}
summary {
padding-top: 2px;
details > summary::before {
margin-right: 1ch;
display: inline-block;
float: left;
content: '▶️';
transition: 0.2s;
}
details[open] > summary {
list-style-type: '🔽';
border-bottom: 1px solid var(--color-gray);
margin-bottom: 1em;
}
details[open] > summary::before {
transform: rotate(90deg);
}
details > summary {
margin: -1em -1em 0;
padding: 1em;
cursor: pointer;
font-weight: bold;
color: var(--color-primary);
}
summary i {
details > summary i {
margin: 0 10px;
}
details > summary h3,
details > summary p {
margin: 0;
}

details[open] {
#padding: 1em;
}


/** Override Font Awesome because the logo are not up to date **/
.custom-fa-slack, .custom-fa-discord, .custom-fa-zoom, .custom-fa-webex {
Expand Down
78 changes: 70 additions & 8 deletions src/Controller/SantaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
use JoliCode\SecretSanta\Statistic\StatisticCollector;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\SubmitButton;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -76,7 +78,7 @@ public function run(Request $request, string $application): Response
}

#[Route('/participants/{application}', name: 'participants', methods: ['GET', 'POST'])]
public function participants(Request $request, string $application): Response
public function participants(Rudolph $rudolph, Request $request, string $application): Response
{
$application = $this->getApplication($application);

Expand All @@ -102,6 +104,7 @@ public function participants(Request $request, string $application): Response
$form->handleRequest($request);

if ($form->isSubmitted()) {
$config->setShuffledUsers($rudolph->associateUsers($config->getSelectedUsers()));
$this->saveConfig($request, $config);
if ($form->isValid()) {
return $this->redirectToRoute('message', ['application' => $application->getCode()]);
Expand All @@ -118,7 +121,7 @@ public function participants(Request $request, string $application): Response
}

#[Route('/message/{application}', name: 'message', methods: ['GET', 'POST'])]
public function message(Rudolph $rudolph, FormFactoryInterface $formFactory, Request $request, Spoiler $spoiler, string $application): Response
public function message(FormFactoryInterface $formFactory, Request $request, string $application): Response
{
$application = $this->getApplication($application);

Expand Down Expand Up @@ -156,7 +159,68 @@ public function message(Rudolph $rudolph, FormFactoryInterface $formFactory, Req
$this->saveConfig($request, $config);

if ($form->isValid()) {
$secretSanta = $this->prepareSecretSanta($rudolph, $request, $config);
return $this->redirectToRoute('validate', ['application' => $application->getCode()]);
}

$errors = array_map(function (FormError $error) {
return $error->getMessage();
}, iterator_to_array($form->getErrors(true, false)));

if ($errors) {
$errors = array_unique($errors);
}
}

$content = $this->twig->render('santa/application/message_' . $application->getCode() . '.html.twig', [
'application' => $application->getCode(),
'admin' => $application->getAdmin(),
'config' => $config,
'errors' => $errors,
'form' => $form->createView(),
]);

return new Response($content);
}

#[Route('/validate/{application}', name: 'validate', methods: ['GET', 'POST'])]
public function validate(Rudolph $rudolph, FormFactoryInterface $formFactory, Spoiler $spoiler, Request $request, string $application): Response
{
$application = $this->getApplication($application);

if (!$application->isAuthenticated()) {
return new RedirectResponse($this->router->generate($application->getAuthenticationRoute()));
}

$errors = [];

$config = $this->getConfigOrThrow404($request);

if (!$config->getShuffledUsers()) {
if (count($config->getSelectedUsers()) < 2) {
return new RedirectResponse($this->router->generate('participants', ['application' => $application->getCode()]));
}
$config->setShuffledUsers($rudolph->associateUsers($config->getSelectedUsers()));
}

$form = $formFactory->createBuilder()
->add('shuffle', SubmitType::class)
->add('submit', SubmitType::class)
->getForm()
;

$form->handleRequest($request);

if ($form->isSubmitted()) {
if ($form->isValid()) {
$shuffleButton = $form->get('shuffle');
if ($shuffleButton instanceof SubmitButton && $shuffleButton->isClicked()) {
$config->setShuffledUsers($rudolph->associateUsers($config->getSelectedUsers()));
$this->saveConfig($request, $config);

return $this->redirectToRoute('validate', ['application' => $application->getCode()]);
}

$secretSanta = $this->prepareSecretSanta($config);
$session = $request->getSession();
$session->set(
$this->getSecretSantaSessionKey(
Expand Down Expand Up @@ -185,7 +249,7 @@ public function message(Rudolph $rudolph, FormFactoryInterface $formFactory, Req
}
}

$content = $this->twig->render('santa/application/message_' . $application->getCode() . '.html.twig', [
$content = $this->twig->render('santa/application/validate_' . $application->getCode() . '.html.twig', [
'application' => $application->getCode(),
'admin' => $application->getAdmin(),
'config' => $config,
Expand Down Expand Up @@ -408,11 +472,9 @@ private function getSecretSantaSessionKey(string $hash): string
return \sprintf('secret-santa-%s', $hash);
}

private function prepareSecretSanta(Rudolph $rudolph, Request $request, Config $config): SecretSanta
private function prepareSecretSanta(Config $config): SecretSanta
{
$selectedUsersAsArray = $config->getSelectedUsers();

$associatedUsers = $rudolph->associateUsers($selectedUsersAsArray);
$associatedUsers = $config->getShuffledUsers();

$hash = md5(serialize($associatedUsers));

Expand Down
23 changes: 23 additions & 0 deletions src/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Config
private array $groups = [];
/** @var string[] */
private array $selectedUsers = [];
/** @var string[] */
private array $shuffledUsers = [];
private ?string $message = '';
/** @var array<int, string> */
private array $notes = [];
Expand Down Expand Up @@ -79,6 +81,22 @@ public function setAvailableUsers(array $availableUsers): void
$this->availableUsers = $availableUsers;
}

/**
* @return string[]
*/
public function getShuffledUsers(): array
{
return $this->shuffledUsers;
}

/**
* @param string[] $shuffledUsers
*/
public function setShuffledUsers(array $shuffledUsers): void
{
$this->shuffledUsers = $shuffledUsers;
}

public function getMessage(): ?string
{
return $this->message;
Expand Down Expand Up @@ -136,4 +154,9 @@ public function getGroups(): array
{
return $this->groups;
}

public function getUser(string $identifier): ?User
{
return $this->availableUsers[$identifier] ?? null;
}
}
2 changes: 1 addition & 1 deletion src/Model/SecretSanta.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getUserCount(): int

public function getUser(string $identifier): ?User
{
return $this->config->getAvailableUsers()[$identifier] ?? null;
return $this->config->getUser($identifier);
}

public function getUserNote(string $identifier): string
Expand Down
Loading

0 comments on commit e660e27

Please sign in to comment.