Skip to content

Commit 48a60da

Browse files
committed
Collections: Choose first if applicable
1 parent a599b62 commit 48a60da

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

assets/controllers/puzzle_add_form_controller.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default class extends Controller {
1515
'commonSection', // Comment, photo (Speed + Relax)
1616
'collectionSection', // Collection fields (Collection only)
1717
'newCollectionFields', // New collection name/visibility (Collection, when creating new)
18+
'collectionInput', // The collection tom-select input
1819
];
1920

2021
static values = {
@@ -30,6 +31,8 @@ export default class extends Controller {
3031
const collectionInput = this.collectionSectionTarget.querySelector('input[type="text"], select');
3132
if (collectionInput) {
3233
collectionInput.addEventListener('change', this.handleCollectionChange.bind(this));
34+
// Listen for tom-select initialization to auto-select single option
35+
collectionInput.addEventListener('autocomplete:connect', this._autoSelectSingleOption.bind(this));
3336
}
3437
}
3538
}
@@ -101,4 +104,16 @@ export default class extends Controller {
101104
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-7][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
102105
return uuidRegex.test(value);
103106
}
107+
108+
_autoSelectSingleOption(event) {
109+
const tomselect = event.target.tomselect;
110+
if (!tomselect || event.target.value) {
111+
return; // Already has value or no tomselect
112+
}
113+
114+
const optionKeys = Object.keys(tomselect.options);
115+
if (optionKeys.length === 1) {
116+
tomselect.setValue(optionKeys[0]);
117+
}
118+
}
104119
}

assets/controllers/puzzle_collection_form_controller.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ export default class extends Controller {
8888
return;
8989
}
9090

91+
const tomselect = this.collectionTarget.tomselect;
92+
93+
// Auto-select if only one option is available and nothing is selected
94+
if (tomselect && !this.collectionTarget.value) {
95+
const optionKeys = Object.keys(tomselect.options);
96+
if (optionKeys.length === 1) {
97+
tomselect.setValue(optionKeys[0]);
98+
return; // Value is now set, no need for further checks
99+
}
100+
}
101+
91102
const currentValue = this.collectionTarget.value;
92103
if (currentValue && !this.uuidRegex.test(currentValue) && currentValue !== this.systemIdValue) {
93104
// If there's an initial value that's not a UUID or system collection, show new collection fields

src/Controller/Collections/AddPuzzleToCollectionController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public function __invoke(
8787
// Set default collection to system collection for users without membership
8888
if ($hasActiveMembership === false) {
8989
$formData->collection = Collection::SYSTEM_ID;
90+
} elseif (count($collectionChoices) === 1) {
91+
// Pre-fill if only one collection is available (saves a click)
92+
$formData->collection = array_first($collectionChoices);
9093
}
9194

9295
$form = $this->createForm(CollectionPuzzleActionFormType::class, $formData, [

src/Controller/PuzzleAddController.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,24 @@ public function __invoke(
134134
$initialMode = 'relax';
135135
}
136136

137-
// For non-members in collection mode, force system collection
137+
// Get player collections for form options (include system collection)
138138
$hasActiveMembership = $userProfile->activeMembership;
139-
if ($data->mode === PuzzleAddMode::Collection && $hasActiveMembership === false) {
139+
$collections = [];
140+
$systemCollectionName = $this->translator->trans('collections.system_name');
141+
$collections[$systemCollectionName] = Collection::SYSTEM_ID;
142+
143+
foreach ($this->getPlayerCollections->byPlayerId($userProfile->playerId) as $collection) {
144+
if ($collection->collectionId !== null) {
145+
$collections[$collection->name] = $collection->collectionId;
146+
}
147+
}
148+
149+
// For non-members, force system collection
150+
if ($hasActiveMembership === false) {
140151
$data->collection = Collection::SYSTEM_ID;
152+
} elseif (count($collections) === 1 && $data->collection === null) {
153+
// Pre-fill if only one collection is available (saves a click)
154+
$data->collection = array_first($collections);
141155
}
142156

143157
/** @var array<string> $groupPlayers */
@@ -151,17 +165,6 @@ public function __invoke(
151165
}
152166
}
153167

154-
// Get player collections for form options (include system collection)
155-
$collections = [];
156-
$systemCollectionName = $this->translator->trans('collections.system_name');
157-
$collections[$systemCollectionName] = Collection::SYSTEM_ID;
158-
159-
foreach ($this->getPlayerCollections->byPlayerId($userProfile->playerId) as $collection) {
160-
if ($collection->collectionId !== null) {
161-
$collections[$collection->name] = $collection->collectionId;
162-
}
163-
}
164-
165168
$addTimeForm = $this->createForm(PuzzleAddFormType::class, $data, [
166169
'active_puzzle' => $activePuzzle,
167170
'collections' => $collections,

src/Controller/PuzzleReport/PendingProposalsModalController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ public function __construct(
2323
path: [
2424
'cs' => '/puzzle/{puzzleId}/cekajici-navrhy',
2525
'en' => '/en/puzzle/{puzzleId}/pending-proposals',
26+
'es' => '/es/puzzle/{puzzleId}/propuestas-pendientes',
27+
'ja' => '/ja/puzzle/{puzzleId}/pending-proposals',
28+
'fr' => '/fr/puzzle/{puzzleId}/propositions-en-attente',
29+
'de' => '/de/puzzle/{puzzleId}/ausstehende-vorschlaege',
2630
],
2731
name: 'puzzle_pending_proposals',
2832
methods: ['GET'],

0 commit comments

Comments
 (0)