Movie year selection control #20
+2
-2
@@ -93,8 +93,8 @@
|
|||||||
</p>
|
</p>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="active_oscar_year" class="form-label">Année active</label>
|
<label for="active_oscar_year" class="form-label">Année active</label>
|
||||||
<input type="number" class="form-control" id="active_oscar_year" placeholder="2026" min="1900" max="3000">
|
<select class="form-select" id="active_oscar_year"></select>
|
||||||
<div class="form-text">Ex: mets 2025 → tout le monde voit les stats/films 2025.</div>
|
<div class="form-text">La liste est basée sur les années présentes dans les films.</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="d-flex justify-content-end">
|
<div class="d-flex justify-content-end">
|
||||||
<button type="button" id="save-active-year" class="btn btn-warning">Appliquer l’année</button>
|
<button type="button" id="save-active-year" class="btn btn-warning">Appliquer l’année</button>
|
||||||
|
|||||||
@@ -542,6 +542,10 @@ window.onload = async function () {
|
|||||||
try {
|
try {
|
||||||
const yearsRes = await fetch('/api/movies/years');
|
const yearsRes = await fetch('/api/movies/years');
|
||||||
const years = yearsRes.ok ? await yearsRes.json() : [];
|
const years = yearsRes.ok ? await yearsRes.json() : [];
|
||||||
|
const cleanedYears = (Array.isArray(years) ? years : [])
|
||||||
|
.map((y) => Number(y))
|
||||||
|
.filter((y) => Number.isInteger(y) && y >= 1900 && y <= 3000)
|
||||||
|
.sort((a, b) => b - a);
|
||||||
|
|
||||||
// Rebuild manage dropdown from scratch (so removed years disappear)
|
// Rebuild manage dropdown from scratch (so removed years disappear)
|
||||||
const previousSelection = manageYearSelect.value;
|
const previousSelection = manageYearSelect.value;
|
||||||
@@ -551,19 +555,19 @@ window.onload = async function () {
|
|||||||
allOpt.textContent = 'Toutes';
|
allOpt.textContent = 'Toutes';
|
||||||
manageYearSelect.appendChild(allOpt);
|
manageYearSelect.appendChild(allOpt);
|
||||||
|
|
||||||
years.forEach((y) => {
|
cleanedYears.forEach((y) => {
|
||||||
const opt = document.createElement('option');
|
const opt = document.createElement('option');
|
||||||
opt.value = String(y);
|
opt.value = String(y);
|
||||||
opt.textContent = String(y);
|
opt.textContent = String(y);
|
||||||
manageYearSelect.appendChild(opt);
|
manageYearSelect.appendChild(opt);
|
||||||
});
|
});
|
||||||
|
|
||||||
const stillExists = years.map(String).includes(previousSelection);
|
const stillExists = cleanedYears.map(String).includes(previousSelection);
|
||||||
manageYearSelect.value = stillExists ? previousSelection : '';
|
manageYearSelect.value = stillExists ? previousSelection : '';
|
||||||
|
|
||||||
// Prefer defaulting UI to active year (if selection is empty/invalid).
|
// Prefer defaulting UI to active year (if selection is empty/invalid).
|
||||||
const hasSelection = !!manageYearSelect.value;
|
const hasSelection = !!manageYearSelect.value;
|
||||||
if (!hasSelection && activeYear && years.map(String).includes(String(activeYear))) {
|
if (!hasSelection && activeYear && cleanedYears.map(String).includes(String(activeYear))) {
|
||||||
manageYearSelect.value = String(activeYear);
|
manageYearSelect.value = String(activeYear);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -571,8 +575,44 @@ window.onload = async function () {
|
|||||||
if (!yearInput.value) {
|
if (!yearInput.value) {
|
||||||
if (activeYear) {
|
if (activeYear) {
|
||||||
yearInput.value = String(activeYear);
|
yearInput.value = String(activeYear);
|
||||||
} else if (years.length > 0) {
|
} else if (cleanedYears.length > 0) {
|
||||||
yearInput.value = String(years[0]);
|
yearInput.value = String(cleanedYears[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild active-year dropdown from distinct movie years
|
||||||
|
if (activeYearInput && saveActiveYearBtn) {
|
||||||
|
const previousActiveSelection = String(activeYearInput.value || '');
|
||||||
|
activeYearInput.innerHTML = '';
|
||||||
|
|
||||||
|
if (cleanedYears.length === 0) {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = '';
|
||||||
|
opt.textContent = 'Aucune année (ajoute un film)';
|
||||||
|
opt.disabled = true;
|
||||||
|
opt.selected = true;
|
||||||
|
activeYearInput.appendChild(opt);
|
||||||
|
activeYearInput.disabled = true;
|
||||||
|
saveActiveYearBtn.disabled = true;
|
||||||
|
} else {
|
||||||
|
cleanedYears.forEach((y) => {
|
||||||
|
const opt = document.createElement('option');
|
||||||
|
opt.value = String(y);
|
||||||
|
opt.textContent = String(y);
|
||||||
|
activeYearInput.appendChild(opt);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Keep current selection if still valid, else use activeYear, else latest.
|
||||||
|
const desired =
|
||||||
|
(previousActiveSelection && cleanedYears.map(String).includes(previousActiveSelection))
|
||||||
|
? previousActiveSelection
|
||||||
|
: (activeYear && cleanedYears.map(String).includes(String(activeYear)))
|
||||||
|
? String(activeYear)
|
||||||
|
: String(cleanedYears[0]);
|
||||||
|
|
||||||
|
activeYearInput.disabled = false;
|
||||||
|
saveActiveYearBtn.disabled = false;
|
||||||
|
activeYearInput.value = desired;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
@@ -860,8 +900,7 @@ window.onload = async function () {
|
|||||||
|
|
||||||
// Initial load
|
// Initial load
|
||||||
activeYear = await fetchActiveYear();
|
activeYear = await fetchActiveYear();
|
||||||
if (activeYearInput && activeYear) {
|
if (activeYear) {
|
||||||
activeYearInput.value = String(activeYear);
|
|
||||||
document.title = `Pool Oscars ${activeYear} - Admin`;
|
document.title = `Pool Oscars ${activeYear} - Admin`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user