diff --git a/app/Http/Controllers/ActuController.php b/app/Http/Controllers/ActuController.php index 7ad8d5e6..36bdab9a 100644 --- a/app/Http/Controllers/ActuController.php +++ b/app/Http/Controllers/ActuController.php @@ -5,26 +5,36 @@ namespace App\Http\Controllers; use App\Enums\GeneralConfigType; use App\Models\Actu; use App\Models\Tag; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Cache; class ActuController extends Controller { + /** + * Display a listing of the resource. + * + * @return \Illuminate\Contracts\View\View + */ public function index() { - $tags = Tag::has('actus')->get(); - $actus = Actu::where('status', true) + /** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Tag> */ + $tags = Tag::query()->whereHas('actus', function (Builder $q) { + $q->where('status', true); + })->get(); + /** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Actu> */ + $actus = Actu::query()->where('status', true) ->with('tags:id') + ->orderBy('created_at', 'DESC') ->get(['id']) - ->mapWithKeys(function ($actu) { + ->map(function ($actu) { return [ - $actu->id => [ - 'id' => $actu->id, - 'tags' => $actu->tags->mapWithKeys(function ($tag) { - return [$tag->id => $tag->id]; - })->all() - ] + 'id' => $actu->id, + 'tags' => $actu->tags->map(function ($tag) { + return $tag->id; + })->all() ]; })->all(); + $generalConfig = app()->view->getShared()['GeneralConfigGlobal'] ?? null; $generalConfig = $generalConfig ? $generalConfig->where('type', GeneralConfigType::actu())->first() : null; return view('front.actu.index', [ @@ -35,6 +45,12 @@ class ActuController extends Controller ]); } + /** + * Display a news + * + * @param \App\Models\Actu $actu + * @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory + */ public function view(Actu $actu) { $relatedActus = Actu::whereHas('tags', function ($q) use ($actu) { @@ -50,6 +66,12 @@ class ActuController extends Controller ]); } + /** + * Render a news for JS usage (ajax) + * + * @param \App\Models\Actu $actu + * @return string Base64 rendered view. + */ public function renderActu(Actu $actu) { if (!request()->wantsJson()) { diff --git a/resources/js/components/fo/ActuPaginatorComponent.vue b/resources/js/components/fo/ActuPaginatorComponent.vue index b1170cf4..adc916da 100644 --- a/resources/js/components/fo/ActuPaginatorComponent.vue +++ b/resources/js/components/fo/ActuPaginatorComponent.vue @@ -8,18 +8,18 @@ - -
+
-
+
@@ -51,7 +51,7 @@
  • -
  • +
  • {{ pageNumber }}
  • @@ -80,12 +80,12 @@ export default { props: ["pageLinks", "actusData", "tagsData", "paginateData", "currentTag"], data() { return { - links: {}, - actus: {}, - showedActus: {}, - tags: {}, + actus: [], + showedActus: [], + tags: [], maxPagesToShow: 2, - paginate: 10, + /** Articles per page */ + paginate: 3, firstPage: 1, lastPage: 1, currentPage: 1, @@ -139,41 +139,43 @@ export default { }, loadPage(page) { // reset showed actu - this.$set(this, 'showedActus', {}) + this.$set(this, 'showedActus', []) this.$set(this, 'currentPage', page) + let actusPage = this.filterActuByTag() + // Per page let perPage = this.paginate - let start = perPage * page - perPage + 1 + let start = perPage * page - perPage let stop = perPage * page - let actus = this.filterActuByTag() + console.log(start) + console.log(stop) - let k = 1 - for (let id in actus) { - if(k < start) { - k++ + let k = 0 + for (let actu of actusPage) { + if(k++ < start) { continue } if(k > stop) { break } - let actu = Object.assign({}, actus[id]) actu.loading = true; - this.$set(this.showedActus, k++, actu) + this.showedActus.push(actu) this.loadActu(actu); } - this.lastPage = Math.ceil(Object.keys(actus).length / this.paginate) + this.lastPage = Math.ceil(actusPage.length / this.paginate) this.genPagesNumbers() }, filterActuByTag() { - let actus = {} - for(let id in this.actus) { - let actu = this.actus[id] - let containTag = Object.values(actu.tags).filter(tag => { return tag === this.selectedTag}).length + let actusFiltered = [] + for(let actu of this.actus) { + let containTag = actu.tags.filter(tagId => { + return tagId === this.selectedTag + }).length if(!this.selectedTag || containTag) { - actus[id] = Object.assign({}, actu) + actusFiltered.push(Object.assign({}, actu)) } } - return actus + return actusFiltered }, loadActu(actu) { axios