infinite scroll

This commit is contained in:
Alexis Gousseau 2022-10-27 12:41:41 +02:00
parent e4d407e7e8
commit 5f81653f03
10 changed files with 187 additions and 199 deletions

View file

@ -11,6 +11,7 @@ use Kwaadpepper\Enum\BaseEnumRoutable;
* @method static self none()
* @method static self loue()
* @method static self legaulois()
* @method static self legauloispro()
* @method static self marie()
* @method static self tregalette()
* @method static self esprirestauration()
@ -29,6 +30,7 @@ class ProductBrand extends BaseEnumRoutable
'none' => 'Sans marque',
'loue' => 'Loué',
'legaulois' => 'Le Gaulois',
'legauloispro' => 'Le Gaulois Professionnel',
'marie' => 'Marie',
'tregalette' => 'Trégalette',
'esprirestauration' => 'Espri Restauration',

View file

@ -95,9 +95,9 @@ class ProductsController extends Controller
if (!empty($filter)) {
$products = Product::where('status', 1)
->where('name', 'like', '%' . $filter . '%')
->paginate(12);
->paginate(3);
} else {
$products = Product::where('status', 1)->paginate(12);
$products = Product::where('status', 1)->paginate(3);
}
$search_title = 'Recherche : ' . $filter;
@ -160,7 +160,7 @@ class ProductsController extends Controller
}
/** @var \Illuminate\Database\Eloquent\Collection<\App\Models\Product> */
$products = $productsQuery->get();
$products = $productsQuery->paginate(3);
return \response()->json(ProductResource::collection($products));
}

View file

@ -1,5 +1,5 @@
@unless ($breadcrumbs->isEmpty())
<div class="container d-none d-lg-block pt-2">
<section class="d-none d-lg-block pt-2">
<div class="row ps-5">
<div class="col ps-5">
<div class="breadrumbs content ps-5">
@ -21,5 +21,5 @@
</div>
</div>
</div>
</div>
</section>
@endunless

View file

@ -1,5 +1,5 @@
<template>
<div id="basket" class="basket-hidden w-100 h-100 position-fixed top-0">
<section id="basket" class="basket-hidden w-100 h-100 position-fixed top-0">
<div class="basket-bg bkg-white position-absolute top-0 left-0 w-100 h-100"></div>
<div class="container d-flex flex-row-reverse h-100 p-0 m-0 m-sm-auto">
<div class="basket-preview p-3 rounded-4">
@ -68,7 +68,7 @@
</div>
</div>
</div>
</div>
</section>
</template>

View file

@ -5,7 +5,7 @@
<span class="number d-block d-flex flex-row justify-content-center align-items-center bkg-dark text-white rounded-4 mx-2 me-4">{{ countFilter }}</span>
<img class="basket-icon" src="/sable/img/icons/filter.png" alt="Basket icon">
</button>
<div class="filters filters-hidden position-fixed position-lg-relative w-100 w-lg-25 h-100 h-lg-auto px-0 pt-3 pb-6">
<div class="filters filters-hidden position-fixed position-lg-relative col-12 col-lg-3 h-100 h-lg-auto px-0 pt-3 pb-6 p-lg-0">
<div class="filters-list h-100">
<p class="filters-title d-flex justify-content-between justify-content-lg-start align-items-center position-relative title-small font-Barlow-bold m-0 m-lg-3">
<span class="btn d-lg-none bkg-orange text-white rounded-4 ms-3">Effacer</span>
@ -31,7 +31,7 @@
</button>
<div class="collapse show" id="filterCollapsePromotions">
<div class="form-check d-flex flex-row flex-lg-row-reverse justify-content-between align-items-center rounded-2 px-3 py-2 p-lg-0 mt-2 mb-0">
<input v-on:click="fetchProducts" v-model="hasPromotion" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button" id="filterPromotions">
<input v-on:click="updatedFilter()" v-model="hasPromotion" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button" id="filterPromotions">
<label class="form-check-label w-100 ps-3 pe-0 ps-lg-0 pe-lg-3" role="button" for="filterPromotions">
Promotions
</label>
@ -45,15 +45,13 @@
type="button" data-bs-toggle="collapse" data-bs-target="#filterCollapseRanges"
aria-expanded="true" aria-controls="filterCollapseRanges">
<span class="title-small font-Barlow-bold">Rayons</span>
<svg id="arrow-down" class="svg" viewBox='0 0 16 16'>
<path d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z" />
</svg>
<i class="fas fa-chevron-down"></i>
</button>
<div class="collapse show" id="filterCollapseRanges">
<!-- Ranges -->
<div v-for="(range, rangeIndex) in ranges" :key="rangeIndex"
class="form-check d-flex flex-row flex-lg-row-reverse justify-content-between align-items-center rounded-2 px-3 py-2 p-lg-0 mt-2 mb-0">
<input v-on:click="fetchProducts" v-model="range.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
<input v-on:click="updatedFilter()" v-model="range.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
:id="`filterRanges${rangeIndex}`">
<label class="form-check-label w-100 ps-3 pe-0 ps-lg-0 pe-lg-3" role="button"
:for="`filterRanges${rangeIndex}`">
@ -69,15 +67,13 @@
type="button" data-bs-toggle="collapse" data-bs-target="#filterCollapseBrand"
aria-expanded="true" aria-controls="filterCollapseBrand">
<span class="title-small font-Barlow-bold">Marques</span>
<svg id="arrow-down" class="svg" viewBox='0 0 16 16'>
<path d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z" />
</svg>
<i class="fas fa-chevron-down"></i>
</button>
<div class="collapse show" id="filterCollapseBrand">
<!-- Brands -->
<div v-for="(brand, brandIndex) in brands" :key="brandIndex"
class="form-check d-flex flex-row flex-lg-row-reverse justify-content-between align-items-center rounded-2 px-3 py-2 p-lg-0 mt-2 mb-0">
<input v-on:click="fetchProducts" v-model="brand.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
<input v-on:click="updatedFilter()" v-model="brand.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
:id="`filterBrand${brandIndex}`">
<label class="form-check-label w-100 ps-3 pe-0 ps-lg-0 pe-lg-3" role="button"
:for="`filterBrand${brandIndex}`">
@ -93,15 +89,13 @@
type="button" data-bs-toggle="collapse" data-bs-target="#filterCollapseTag"
aria-expanded="true" aria-controls="filterCollapseTag">
<span class="title-small font-Barlow-bold">Spécial</span>
<svg id="arrow-down" class="svg" viewBox='0 0 16 16'>
<path d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z" />
</svg>
<i class="fas fa-chevron-down" aria-hidden="true"></i>
</button>
<div class="collapse show" id="filterCollapseTag">
<!-- Tags -->
<div v-for="(tag, tagIndex) in tags" :key="tagIndex"
class="form-check d-flex flex-row flex-lg-row-reverse justify-content-between align-items-center rounded-2 px-3 py-2 p-lg-0 mt-2 mb-0">
<input v-on:click="fetchProducts" v-model="tag.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
<input v-on:click="updatedFilter()" v-model="tag.selected" :disabled="loading" class="form-check-input m-0" type="checkbox" role="button"
:id="`filterTag${tagIndex}`">
<label class="form-check-label w-100 ps-3 pe-0 ps-lg-0 pe-lg-3" role="button"
:for="`filterTag${tagIndex}`">
@ -116,40 +110,24 @@
</div>
</div>
<!-- container de produit -->
<section class="w-100 w-lg-75 px-0 px-xl-2">
<div class="products col-12 col-lg-9 px-0 px-xl-2">
<p class="title-small font-Barlow-bold m-3 text-center text-lg-start">Tous les rayons</p>
<div v-if="loading" class="placeholder row m-0 g-4">
<article class="col-lg-4 card position-relative rounded-4 p-3 m-0 my-2">
<div class="d-flex flex-row flex-lg-column justify-content-between align-items-center col-md-4 p-0 w-100 m-0">
<div class="image">
<content-placeholders>
<content-placeholders-img />
</content-placeholders>
</div>
<div class="text">
<content-placeholders>
<content-placeholders-text :lines="3" />
</content-placeholders>
</div>
</div>
</article>
</div>
<div v-else class="row m-0 g-4">
<div class="row m-0 g-4">
<!-- card article -->
<article v-for="(product, productIndex) in showedProducts"
<article v-for="(product, productIndex) in products"
:key="productIndex"
class="col-lg-4 card rounded-4 px-0 py-2 p-lg-1 p-xl-3 m-0"
v-bind:class="[!product.availability ? 'card-available-soon' : '']">
v-bind:class="[product.availability ? 'card-available-soon' : '']">
<div class="position-relative w-100 h-100 px-4">
<div class="d-flex flex-row flex-lg-column justify-content-between align-items-center w-100 h-100 col-md-4 m-0x py-2 pb-lg-4">
<div class="position-relative text-center w-25 w-lg-100">
<p class="position-absolute top-50 start-50 translate-middle font-Barlow-bold title-small text-uppercase d-none text-center m-0">
bientôt disponible
</p>
<img v-if="product.picture" class="h-lg-100 mw-100" :src="'/images/produits/' + product.picture" :alt="'Image du produit '+product.name">
<img v-else class="h-lg-100 mw-100" src="/sable/img/pl_nophoto.png" alt="Image d'un produit par défaut">
<img v-if="product.picture" class="mw-100" :src="'/images/produits/' + product.picture" :alt="'Image du produit '+product.name">
<img v-else class="mw-100" src="/sable/img/pl_nophoto.png" alt="Image d'un produit par défaut">
</div>
<div class="card-body d-flex flex-column justify-content-center justify-content-lg-between align-items-start align-items-lg-center w-75 w-lg-100 h-100 ps-2 px-lg-0">
<div class="card-body d-flex flex-column justify-content-center justify-content-lg-between align-items-start align-items-lg-center w-75 w-lg-100 ps-2 px-lg-0">
<h5 class="font-Barlow-bold text-center m-0 m-lg-2 mb-lg-0">
<a class="text-decoration-none text-dark stretched-link" :href="'/produit/'+product.slug">{{ product.name }}</a>
</h5>
@ -168,26 +146,33 @@
</div>
</div>
</div>
<div v-if="!product.loading" class="filtre position-absolute bottom-0 start-0 w-100 h-100 h-lg-75 rounded-4"></div>
<div class="filtre position-absolute bottom-0 start-0 w-100 h-100 h-lg-75 rounded-4"></div>
</div>
</article>
<article v-if="loading" class="placeholder col-lg-4 card rounded-4 px-0 py-2 p-lg-1 p-xl-3 m-0">
<div class="position-relative w-100 h-100 px-4">
<div class="d-flex flex-row flex-lg-column justify-content-between align-items-center col-md-4 p-0 w-100 h-100 m-0">
<div class="image w-25 w-lg-100 h-lg-50">
<content-placeholders>
<content-placeholders-img />
</content-placeholders>
</div>
<div class="text w-75 w-lg-100 h-lg-50 ps-2 px-lg-0">
<content-placeholders>
<content-placeholders-text :lines="5" />
</content-placeholders>
</div>
</div>
</div>
</article>
<div class="d-flex flex-column m-0">
<button class="btn btn-more-products my-3" v-bind:class="[this.productsResults > this.products.length ? 'd-none' : (currentPage > 1) ? 'd-none' : '']" v-on:click="checkScroll()">
<i class="fas fa-circle-plus" aria-hidden="true"></i>
</button>
<p class="fst-italic text-center text-muted" v-bind:class="[(this.productsResults * this.currentPage) <= this.products.length ? 'd-none' : '']" v-on:click="checkScroll()">Tous les produits correspondants à votre recherche sont affichés</p>
</div>
</div>
<div class="paginator my-4" v-if="lastPage > 1 && !loading">
<nav>
<ul class="pagination justify-content-end font-Barlow-bold m-0">
<li class="page-item" v-if="currentPage > firstPage" v-on:click="showPrevPage(true, $event);">
<a href="#" rel="prev" aria-label="« Précédent" class="page-link">‹</a>
</li>
<li v-for="pageNumber in pagesNumber" :key="pageNumber" class="page-item" v-on:click="loadPage(pageNumber)">
<a href="#" :class="'page-link ' + (pageNumber==currentPage ? 'active' : '')" >{{ pageNumber }}</a>
</li>
<li class="page-item" v-if="currentPage < lastPage" v-on:click="showNextPage(true, $event);">
<a href="#" rel="next" aria-label="Suivant »" class="page-link">›</a>
</li>
</ul>
</nav>
</div>
</section>
</div>
</div>
</template>
@ -205,20 +190,16 @@
},
data() {
return {
loading: true,
loading: false,
countFilter: 0,
hasPromotion: false,
ranges: [],
brands: [],
tags: [],
products: [],
showedProducts: {},
maxPagesToShow: 2,
paginate: 9,
firstPage: 1,
lastPage: 2,
productsResults: 0,
url: '',
currentPage: 1,
pagesNumber: [],
btnFilter: undefined,
btnFilterCross: undefined
}
@ -230,93 +211,97 @@
this.brands = jsonData.brands ?? [];
this.tags = jsonData.tags ?? [];
this.products = jsonData.products ?? [];
this.fetchProducts();
this.productsResults = jsonData.productsResults ?? 0;
this.responsiveFilters();
this.countResults();
},
methods: {
genPagesNumbers() {
// Reset pagesNumber.
let pagesNumber = []
let wantedFirst = this.currentPage - this.maxPagesToShow
let wantedLast = this.currentPage + this.maxPagesToShow
let start = this.firstPage >= wantedFirst ? this.firstPage : wantedFirst
let stop = this.lastPage <= wantedLast ? this.lastPage : wantedLast
do {
// Set pages array.
pagesNumber.push(start++)
} while(start <= stop)
this.$set(this, 'pagesNumber', pagesNumber)
},
showPrevPage() {
let wantedPage = this.currentPage - 1
this.currentPage = wantedPage >=this.firstPage ? wantedPage : this.firstPage
this.loadPage(this.currentPage)
},
showNextPage() {
let wantedPage = this.currentPage + 1
this.currentPage = wantedPage <= this.lastPage ? wantedPage : this.lastPage
this.loadPage(this.currentPage)
},
loadPage(page) {
// Reset showed products.
this.$set(this, 'showedProducts', {})
this.$set(this, 'currentPage', page)
let perPage = this.paginate
let start = perPage * page - perPage + 1
let stop = perPage * page
let k = 1
for (let id in this.products) {
if(k < start) {
k++
continue
/**
* Add an event on scroll to set the nextest page.
*/
checkScroll() {
this.nextPage();
window.addEventListener("scroll", () => {
if (
window.innerHeight + window.scrollY >=
document.body.offsetHeight - 50 &&
!this.loading &&
(this.productsResults * this.currentPage) <= this.products.length
) {
this.nextPage();
}
if(k > stop) {
break
}
let product = Object.assign({}, this.products[id])
product.availability = this.getAvailability(product)
this.$set(this.showedProducts, k++, product)
}
this.lastPage = Math.ceil(Object.keys(this.products).length / this.paginate)
this.genPagesNumbers()
this.responsiveFilters()
this.countResults()
});
},
fetchProducts() {
/**
* Set the current page to the next.
* Update the url.
* Update the products.
*/
nextPage() {
this.currentPage++;
this.updateUrl();
this.updateProducts();
},
/**
* Reset the pagination.
* Update the url.
* Load the new products.
*/
updatedFilter() {
setTimeout(() => {
const filters = this.getFilters();
this.countFilter = 0;
let url = '/produits/filter?' ;
url += `promotion=${filters.promotion ? '1' : '0'}`;
filters.promotion ? this.countFilter++ : '';
filters.brands.map((value) => {
url += `&brands[]=${value}`;
this.countFilter++;
})
filters.tags.map((id) => {
url += `&tags[]=${id}`;
this.countFilter++;
})
filters.ranges.map((id) => {
url += `&ranges[]=${id}`;
this.countFilter++;
})
window.axios.get(url)
.then(response => {
this.products = response.data;
this.currentPage = 1;
this.loadPage(this.currentPage);
this.loading = false
})
.catch((error) => {
console.log(error);
alert('Erreur 101');
this.processing = false;
});
}, 10)
this.resetPagination();
this.updateUrl();
this.updateProducts();
}, 50)
},
// Get all selected value on inputs.
/**
* Set the current page to the first.
* Set the products array to empty.
*/
resetPagination() {
this.currentPage = 1;
this.products = [];
},
/**
* Load the current page.
*/
updateProducts() {
this.loading = true,
window.axios.get(this.url)
.then(response => {
this.products = this.products.concat(response.data);
this.loading = false;
})
.catch((error) => {
console.log(error);
});
},
/**
* Construct the url with the selected filters.
*/
updateUrl() {
const filters = this.getFilters();
this.countFilter = 0;
this.url = '/produits/filter?' ;
this.url += `promotion=${filters.promotion ? '1' : '0'}`;
filters.promotion ? this.countFilter++ : '';
filters.brands.map((value) => {
this.url += `&brands[]=${value}`;
this.countFilter++;
});
filters.tags.map((id) => {
this.url += `&tags[]=${id}`;
this.countFilter++;
});
filters.ranges.map((id) => {
this.url += `&ranges[]=${id}`;
this.countFilter++;
});
this.url += ('&page=' + this.currentPage);
},
/**
* Return an array with the selected filters.
*/
getFilters() {
return {
promotion: this.hasPromotion,
@ -329,27 +314,34 @@
.map((tag) => tag.id),
};
},
// Return the availability of a product.
/**
* Return the availability of a product.
*/
getAvailability(product) {
if(product.orderable_start != null && product.orderable_end != null){
let dateNow = new Date()
let dateStart = new Date(product.orderable_start)
let dateEnd = new Date(product.orderable_end)
let dateNow = new Date();
let dateStart = new Date(product.orderable_start);
let dateEnd = new Date(product.orderable_end);
if(dateNow > dateStart && dateNow < dateEnd) {
return true
return true;
}
else {
return false
return false;
}
}
else {
return true
return true;
}
},
/**
* Update filters total number.
*/
countResults() {
this.btnResults.textContent = "Voir les " + Object.keys(this.products).length + " produits";
this.btnResults.textContent = "Voir les " + this.products.length + " produits";
},
// Event that show filters on mobile.
/**
* Event on mobile version.
*/
responsiveFilters(){
this.btnFilter = document.querySelector(".filter-btn")
this.btnFilterCross = document.querySelector(".filters-title button")
@ -361,11 +353,16 @@
if (this.btnResults)
this.btnResults.addEventListener("click", this.responsiveFiltersDisplay)
},
/**
* Show/hide filters on mobile version.
*/
responsiveFiltersDisplay() {
document.querySelector(".filters").classList.toggle("filters-hidden");
document.querySelector("body").classList.toggle("overflow-hidden");
},
// General purpose.
/**
* General purpose.
*/
currencyFormatter(num) {
if (isNaN(num)) {
return num;
@ -375,30 +372,3 @@
}
}
</script>
<style lang="scss">
.placeholder {
& .image {
width: 100%;
height: 100%;
}
& .text {
width: 75%;
height: 50%;
padding: 0.5rem;
}
}
@media (min-width: 992px) {
.placeholder {
& .image {
width: 100%;
}
& .text {
width: 100%;
}
}
}
</style>

View file

@ -11,7 +11,11 @@
</a>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-link"
<a class="nav-link @if (Route::is('home')) d-none @endif"
href="{{ route('home') }}">
Accueil
</a>
<a class="nav-link @if (!Route::is('home')) d-none @endif"
href="@if (Route::is('home')) {{config('app.url') }}#promotions @else {{ route('products') }} @endif">
Offres
</a>
@ -19,10 +23,6 @@
href="@if (Route::is('home')) {{config('app.url') }}#rayons @else {{ route('products') }} @endif">
Rayons
</a>
<a class="nav-link @if (Route::is('home')) d-none @endif d-lg-none"
href="{{ route('home') }}">
Accueil
</a>
<a class="nav-link @if (!Route::is('home')) d-none @endif"
href="{{ config('app.url') }}#services">
Services

View file

@ -6,16 +6,17 @@
@section('breadcrumb', request()->route()->getName())
@section('content')
<main id="ranges" class="container vue-apps">
<section id="ranges" class="vue-apps">
@php
$dataComponent = [
'hasPromotion' => request()->promotion ? true : false,
'ranges' => $ranges,
'brands' => \App\Enums\ProductBrand::toArray(),
'tags' => $tags,
'products' => \App\Http\Resources\ProductResource::collection($products)
'products' => \App\Http\Resources\ProductResource::collection($products),
'productsResults' => $products->perPage()
];
@endphp
<filter-component json-data='@json($dataComponent)'/>
</main>
</section>
@endsection

View file

@ -38,10 +38,11 @@
alt="{{ $cartItem->name }}" class="img-fluid">
@endif
</div>
<div class="card-body d-flex flex-column justify-content-between justify-content-lg-center align-items-start w-75 w-lg-auto h-100 ps-2">
<div class="card-body d-flex flex-column justify-content-between justify-content-lg-center align-items-start w-75 h-100 ps-2">
<a href="{{ route('product', ['productSlug' => $cartItem->model->slug]) }}"
class="text-decoration-none"
title="{{ $cartItem->name }}" target="_self">
<p class="title-small font-Barlow-bold text-decoration-none stretched-link text-dark m-0">
<p class="title-small font-Barlow-bold stretched-link text-dark m-0">
{{ $cartItem->name }}
</p>
</a>

View file

@ -79,6 +79,10 @@
@include btn($white, none, $orange);
}
.btn-more-products svg {
height: 2rem;
}
.btn-section {
margin-top: 1rem;
@include btn($dark, 1px solid $dark, transparent);

View file

@ -27,7 +27,7 @@
font-size: 2em;
}
button .svg {
button svg {
transform: rotate(-90deg);
}
@ -148,11 +148,11 @@
}
button {
&[aria-expanded=true] .svg {
&[aria-expanded=true] svg {
transform: rotate(180deg);
}
.svg {
svg {
transform: rotate(0deg);
}
}
@ -167,6 +167,16 @@
}
}
}
.products {
.card-body {
min-height: auto;
}
.placeholder {
height: 20rem;
}
}
}
// XXXXXXX XXXXXXXLLLLLLLLLLL
// X:::::X X:::::XL:::::::::L