wip: filter twig component pre-select options

This commit is contained in:
2025-04-29 23:40:27 -05:00
parent 8a1a89f17d
commit 3971cf3260
4 changed files with 49 additions and 36 deletions

View File

@@ -20,6 +20,8 @@ export default class extends Controller {
"provider": "", "provider": "",
} }
userPreferences = []
static outlets = ['movie-results', 'tv-results'] static outlets = ['movie-results', 'tv-results']
static targets = ['resolution', 'codec', 'language', 'provider', 'season', 'selectAll', 'downloadSelected'] static targets = ['resolution', 'codec', 'language', 'provider', 'season', 'selectAll', 'downloadSelected']
static values = { static values = {
@@ -28,22 +30,11 @@ export default class extends Controller {
} }
async connect() { async connect() {
await fetch('/api/preferences/media') this.userPreferences = await(await fetch('/api/user/preferences/values')).json();
.then(response => response.json()) if (this.mediaTypeValue === "tvshows") {
.then(response => { this.activeFilter['season'] = 1;
this.addResolutions(response['resolution']); }
this.addCodecs(response['codec']); await this.filter();
});
await fetch('/api/user/preferences/values')
.then(response => response.json())
.then(response => {
this.activeFilter = response;
if (this.mediaTypeValue === "tvshows") {
this.activeFilter['season'] = 1;
}
console.log(this.activeFilter);
})
} }
async movieResultsOutletConnected(outlet) { async movieResultsOutletConnected(outlet) {
@@ -67,14 +58,24 @@ export default class extends Controller {
addResolutions(resolutions) { addResolutions(resolutions) {
this.resolutionTarget.innerHTML = '<option value="">n/a</option>'; this.resolutionTarget.innerHTML = '<option value="">n/a</option>';
this.resolutionTarget.innerHTML += resolutions.preferenceOptions this.resolutionTarget.innerHTML += resolutions.preferenceOptions
.map((resolution) => '<option value="'+resolution.name.toLowerCase()+'">'+resolution.name+'</option>') .map((resolution) => {
if ('resolution' in this.userPreferences) {
return '<option value="' + resolution.name.toLowerCase() + '" selected>' + resolution.name + '</option>';
}
return '<option value="' + resolution.name.toLowerCase() + '">' + resolution.name + '</option>';
})
.join(); .join();
} }
addCodecs(codecs) { addCodecs(codecs) {
this.codecTarget.innerHTML = '<option value="">n/a</option>'; this.codecTarget.innerHTML = '<option value="">n/a</option>';
this.codecTarget.innerHTML += codecs.preferenceOptions this.codecTarget.innerHTML += codecs.preferenceOptions
.map((codec) => '<option value="'+codec.name.toLowerCase()+'">'+codec.name+'</option>') .map((codec) => {
if ('codec' in this.userPreferences) {
return '<option value="' + codec.name.toLowerCase() + '" selected>' + codec.name + '</option>';
}
return '<option value="' + codec.name.toLowerCase() + '">' + codec.name + '</option>'
})
.join(); .join();
} }

View File

@@ -26,7 +26,8 @@ export default class extends Controller {
} }
async setOptions() { async setOptions() {
if (true === this.activeValue) { if (true === this.activeValue && this.optionsLoaded === false) {
this.optionsLoaded = true;
await fetch(`/torrentio/tvshows/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`) await fetch(`/torrentio/tvshows/${this.tmdbIdValue}/${this.imdbIdValue}/${this.seasonValue}/${this.episodeValue}`)
.then(res => res.text()) .then(res => res.text())
.then(response => { .then(response => {
@@ -38,7 +39,6 @@ export default class extends Controller {
} else { } else {
this.episodeSelectorTarget.disabled = true; this.episodeSelectorTarget.disabled = true;
} }
this.optionsLoaded = true;
this.loadingIconOutlet.increaseCount(); this.loadingIconOutlet.increaseCount();
}); });
} }

View File

@@ -2,6 +2,9 @@
namespace App\Twig\Components; namespace App\Twig\Components;
use Aimeos\Map;
use App\User\Framework\Repository\PreferencesRepository;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\DefaultActionTrait; use Symfony\UX\LiveComponent\DefaultActionTrait;
@@ -9,4 +12,21 @@ use Symfony\UX\LiveComponent\DefaultActionTrait;
final class Filter final class Filter
{ {
use DefaultActionTrait; use DefaultActionTrait;
public array $preferences = [];
public array $userPreferences = [];
public function __construct(
private readonly PreferencesRepository $preferencesRepository,
private readonly Security $security,
) {
$this->preferences = Map::from($this->preferencesRepository->findEnabled())
->rekey(fn($element) => $element->getId())
->map(fn($element) => $element->getPreferenceOptions()->toArray())
->toArray();
$this->userPreferences = Map::from($this->security->getUser()->getUserPreferenceValues())
->toArray();
// dd($this->userPreferences);
}
} }

View File

@@ -13,27 +13,19 @@
class="px-1 py-0.5 bg-stone-100 text-gray-800 rounded-md" class="px-1 py-0.5 bg-stone-100 text-gray-800 rounded-md"
value="{{ app.user.userPreferenceValues["resolution"] }}" value="{{ app.user.userPreferenceValues["resolution"] }}"
> >
{# <option {{ filter.resolution == "n/a" ? "selected" }}#} <option value="">n/a</option>
{# value="">n/a</option>#} {% for option in this.preferences['resolution'] %}
{# <option {{ filter.resolution == "720p" ? "selected" }}#} <option value="{{ option.name|lower }}">{{ option.name }}</option>
{# value="720p">720p</option>#} {% endfor %}
{# <option {{ filter.resolution == "1080p" ? "selected" }}#}
{# value="1080p">1080p</option>#}
{# <option {{ filter.resolution == "2160p" ? "selected" }}#}
{# value="2160p">2160p</option>#}
</select> </select>
</label> </label>
<label for="codec"> <label for="codec">
Codec Codec
<select id="codec" data-result-filter-target="codec" class="px-1 py-0.5 bg-stone-100 text-sm text-gray-800 rounded-md"> <select id="codec" data-result-filter-target="codec" class="px-1 py-0.5 bg-stone-100 text-sm text-gray-800 rounded-md">
{# <option {{ filter.codec == "n/a" ? "selected" }}#} <option value="">n/a</option>
{# value="">n/a</option>#} {% for option in this.preferences['codec'] %}
{# <option {{ filter.codec == "-" ? "selected" }}#} <option value="{{ option.name|lower }}">{{ option.name }}</option>
{# value="-">-</option>#} {% endfor %}
{# <option {{ filter.codec == "h264" ? "selected" }}#}
{# value="h264">h264</option>#}
{# <option {{ filter.codec == "h265" ? "selected" }}#}
{# value="h265">h265/HEVC</option>#}
</select> </select>
</label> </label>
<label for="language"> <label for="language">