/ app / Http / Livewire / Jsons / Index.php
Index.php
  1  <?php
  2  
  3  namespace App\Http\Livewire\Jsons;
  4  
  5  use App\Http\Services\JsonImporterService;
  6  use App\Models\Dossier;
  7  use App\Models\JsonDraft;
  8  use App\Traits\WithPerPagePagination;
  9  use Livewire\Component;
 10  use Livewire\Redirector;
 11  use Livewire\WithPagination;
 12  
 13  class Index extends Component
 14  {
 15      use WithPagination, WithPerPagePagination;
 16  
 17      public bool $readyToLoad = false;
 18  
 19      public array $filters = [
 20          'created_at_since' => null,
 21          'created_at_to' => null,
 22          'updated_at_since' => null,
 23          'updated_at_to' => null,
 24          'withTrashed' => false,
 25      ];
 26  
 27      public string $search = '';
 28  
 29      public bool $jsonDraft = false;
 30  
 31      public string $sortField = 'updated_at';
 32  
 33      public string $sortDirection = 'desc';
 34  
 35      protected $rules = [
 36          'filters.created_at_since' => 'nullable|date',
 37          'filters.created_at_to' => 'nullable|date|after:filters.created_at_since',
 38          'filters.updated_at_since' => 'nullable|date',
 39          'filters.updated_at_to' => 'nullable|date|after:filters.updated_at_since',
 40  
 41      ];
 42  
 43      public function fetchData()
 44      {
 45          $this->readyToLoad = true;
 46      }
 47  
 48      public function updated($propertyName)
 49      {
 50          $this->validateOnly($propertyName);
 51      }
 52  
 53      protected function getListeners()
 54      {
 55          return [
 56              'refresh' => '$refresh',
 57          ];
 58      }
 59  
 60      public function updatingSearch(): void
 61      {
 62          $this->resetPage();
 63      }
 64  
 65      public function updatedFilters(): void
 66      {
 67          $this->getJsonsProperty();
 68      }
 69  
 70      public function sortBy(string $field): void
 71      {
 72          $this->sortField === $field
 73              ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc'
 74              : $this->sortDirection = 'asc';
 75  
 76          $this->sortField = $field;
 77      }
 78  
 79      public function getRowsQueryProperty()
 80      {
 81          return JsonImporterService::search($this->search, $this->sortField, $this->sortDirection, $this->filters)
 82              ->where('completed', $this->jsonDraft)->with('dossier')->get(['id','dossier_id', 'updated_at', 'schema_name']);
 83      }
 84  
 85      public function getJsonsProperty()
 86      {
 87          return $this->readyToLoad ? $this->applyPagination($this->rowsQuery) : [];
 88      }
 89  
 90      public function viewRow(int $rowId): Redirector
 91      {
 92          $json = JsonDraft::find($rowId);
 93          return redirect()->to(route('jsons.draft-view', ['id' => $json]));
 94      }
 95  
 96      public function redirectToDossier(int $dossierId)
 97      {
 98          $dossier = Dossier::findOrFail($dossierId);
 99          return redirect()->to(route('dossiers.dossier.view',['dossier' => $dossier]));
100      }
101  
102      public function render()
103      {
104          return view('livewire.jsons.index')
105              ->layoutData([
106                  'title' => __('jsons.title'),
107                  'entity' => null,
108                  'breadcrumb' => 'jsons.index',
109              ]);
110      }
111  }