Skip to content

Commit

Permalink
Infinite scrolling to reviews was added
Browse files Browse the repository at this point in the history
  • Loading branch information
vilgefortzz committed May 1, 2017
1 parent dd9f48f commit be85757
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 76 deletions.
9 changes: 5 additions & 4 deletions app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@

class ProductController extends Controller
{
public function show(Product $product){
public function showReviews(Product $product){

// Get all reviews for this product
$reviews = $product->reviews;
// Get all reviews for this product and sort reviews by date from the newest one
$reviews = Review::where('product_id', $product->id)->orderBy('created_at', 'desc')->paginate(2);

// Check if has user already given a review to this product
if (Auth::check()){

$isGiven = $reviews->contains('user_id', Auth::user()->id);

return view('product_details', compact('product', 'isGiven', 'reviews'));
}

return view('product_details', compact('product', 'reviews'));

}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/SubcategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function showAllProducts(Subcategory $subcategory, Request $request){

$categories = Category::all();
$category = $subcategory->category;
$products = Product::where('subcategory_id', $subcategory->id)->paginate(30);
$products = Product::where('subcategory_id', $subcategory->id)->paginate(3);

if ($request->ajax()) {
return view('products.products_list', compact('products'))->render();
Expand Down
9 changes: 9 additions & 0 deletions database/factories/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@
'price' => $faker->randomFloat(2, 150, 900)
];
});

$factory->define(App\Review::class, function (Faker\Generator $faker) {

return [
'product_id' => 1,
'user_id' => rand(1, 10),
'review' => $faker->text(500),
];
});
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public function run()
$this->call(CategoriesTableSeeder::class);
$this->call(SubcategoriesTableSeeder::class);
$this->call(ProductsTableSeeder::class);
//$this->call(ReviewsTableSeeder::class);
}
}
17 changes: 17 additions & 0 deletions database/seeds/ReviewsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use App\Review;
use Illuminate\Database\Seeder;

class ReviewsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(Review::class, 40)->create();
}
}
14 changes: 12 additions & 2 deletions public/css/app.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11279,6 +11279,7 @@ $(document).ready(function () {
});

$('#give_review_this_page').on('click', function () {

if ($('#write_review').length != 0) {
$('html, body').animate({
scrollTop: $('#write_review').offset().top - 170
Expand All @@ -11298,6 +11299,7 @@ $(document).ready(function () {
});

if (sessionStorage.length > 0) {

if ($('#write_review').length != 0) {
$('html, body').animate({
scrollTop: $('#write_review').offset().top - 170
Expand Down
12 changes: 12 additions & 0 deletions public/js/jscroll/jquery.jscroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ function getProducts(url) {

// Add listeners

// Again to animate when redirect to page with details

$('.give_review').on('click', function () {

sessionStorage.setItem('hash', 'write_review');
});

// Again to paginate

$('.pagination a').on('click', function (e) {
Expand All @@ -64,7 +71,7 @@ function getProducts(url) {
$('.add_to_cart').on('click', function (e) {
e.preventDefault();

var url = $(this).attr("href");
var url = $(this).attr('href');

$.ajax({
type: 'POST',
Expand Down Expand Up @@ -93,7 +100,7 @@ function getProducts(url) {
$('.remove_from_cart').on('click', function (e) {
e.preventDefault();

var url = $(this).attr("href");
var url = $(this).attr('href');

$.ajax({
type: 'DELETE',
Expand Down
2 changes: 2 additions & 0 deletions resources/assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ $(document).ready(function() {
});

$('#give_review_this_page').on('click', function() {

if($('#write_review').length != 0) {
$('html, body').animate({
scrollTop: $('#write_review').offset().top - 170
Expand All @@ -85,6 +86,7 @@ $(document).ready(function() {
});

if (sessionStorage.length > 0){

if($('#write_review').length != 0) {
$('html, body').animate({
scrollTop: $('#write_review').offset().top - 170
Expand Down
14 changes: 12 additions & 2 deletions resources/assets/sass/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,18 @@ body{
text-align: center;
}

.pagination > li >a, .pagination>li>span {
.pagination > li > a, .pagination > li > span {
border: none;
border-radius: 50%;
margin: 0 5px;
color: black;
}

.pagination > .active > a,
.pagination > .active > a:focus,
.pagination > .active > a:hover,
.pagination > .active > span,
.pagination > .active > span:focus,
.pagination > .active > span:hover {
background-color: #b4b37a;
border-color: black;
}
47 changes: 25 additions & 22 deletions resources/views/product_details.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

@section('content')

<input type="hidden" name="start_from" id="start_from" value="1">

<div class="container container-fix">
<div class="row">
<div class="col-md-10 col-lg-offset-1">
Expand Down Expand Up @@ -71,7 +73,7 @@
{{--Header!!--}}
<h1 class="text-center" style="margin-bottom: 50px"><span class="glyphicon glyphicon-comment"></span><b>Reviews</b></h1>

<div id="reviews">
<div id="reviews_section">
{{--Autheniticated users can give reviews--}}
@if(Auth::check())
@if(!$isGiven)
Expand Down Expand Up @@ -121,31 +123,15 @@

<hr>

{{-- All reviews connected with this product--}}
<div id="reviews">

@foreach($reviews as $review)
<div class="row">
<div class="col-sm-2">
<div class="thumbnail">
<i>{{$review->user->name}}</i>
</div>
</div>
{{--All reviews connected with this product--}}
@include('reviews.reviews_list')

</div>

<div class="col-sm-8">
<div class="panel panel-review panel-default">
<div class="panel-heading panel-heading-review">
<span class="glyphicon glyphicon-time"></span><span class="text-muted"><u><b>{{$review->created_at->toDateString()}}</b></u></span>
</div>
<div class="panel-body">
{{$review->review}}
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>

</div>
</div>

Expand All @@ -154,4 +140,21 @@
<script src="{{ asset('js/add_to_cart_ajax.js') }}"></script>
<script src="{{ asset('js/delete_from_cart_view_products_ajax.js') }}"></script>

{{-- jscroll --}}
<script src="{{ asset('js/jscroll/jquery.jscroll.min.js') }}"></script>

<script type="text/javascript">
$('ul.pagination').hide();
$('.infinite-scroll').jscroll({
autoTrigger: true,
padding: 0,
nextSelector: '.pagination li.active + li a',
contentSelector: 'div.infinite-scroll',
callback: function () {
$('ul.pagination').remove();
}
});
</script>

@endsection
79 changes: 38 additions & 41 deletions resources/views/products/products_list.blade.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
{{-- Pagination links --}}
{{ $products->links() }}

<div id="load" style="position: relative;">
@foreach($products as $product)
@foreach($products as $product)
<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="group list-group-image" src="{{$product->image}}" width="200" height="200"/>
<div class="caption">
<h2 class="group inner list-group-item-heading">
<b>
{{$product->name}}
<a href="{{ url('/products/'.$product->id) }}" style="font-size: 12px">
<span class="glyphicon glyphicon-triangle-right"></span>See details
</a>
</b>
</h2>

<div class="item col-xs-4 col-lg-4">
<div class="thumbnail">
<img class="group list-group-image" src="/images/{{$product->image}}" width="200" height="200"/>
<div class="caption">
<h2 class="group inner list-group-item-heading">
<b>
{{$product->name}}
<a href="{{ url('/products/'.$product->id) }}" style="font-size: 12px">
<span class="glyphicon glyphicon-triangle-right"></span>See details
</a>
</b>
</h2>
<div class="row" style="margin-top: 50px">
<div class="col-xs-12 col-md-4 group_div">
<p><b>Buy now:</b></p>
<p class="lead_sub"><b>{{$product->price}}$</b></p>
</div>
<div class="col-xs-12 col-md-6 group_div">
<a id="add_{{$product->id}}" class="add_to_cart" href="{{ url('/cart/add/'.$product->id) }}">
<div id="add_to_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-shopping-cart"></span><b>Add to cart</b>
</div>
</a>

<div class="row" style="margin-top: 50px">
<div class="col-xs-12 col-md-4 group_div">
<p><b>Buy now:</b></p>
<p class="lead_sub"><b>{{$product->price}}$</b></p>
</div>
<div class="col-xs-12 col-md-6 group_div">
<a id="add_{{$product->id}}" class="add_to_cart" href="{{ url('/cart/add/'.$product->id) }}">
<div id="add_to_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-shopping-cart"></span><b>Add to cart</b>
</div>
</a>
{{-- Hidden link - dynamically change--}}
<a id="remove_{{$product->id}}" class="remove_from_cart" href="{{ url('/cart/delete/'.$product->id) }}" hidden>
<div id="remove_from_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-remove"></span><b>Remove</b>
</div>
</a>

{{-- Hidden link - dynamically change--}}
<a id="remove_{{$product->id}}" class="remove_from_cart" href="{{ url('/cart/delete/'.$product->id) }}" hidden>
<div id="remove_from_cart_btn{{$product->id}}">
<span class="glyphicon glyphicon-remove"></span><b>Remove</b>
{{--For autheniticated users--}}
@if(Auth::check())
<a class="give_review" href="{{ url('/products/'.$product->id) }}">
<div id="give_review_btn">
<span class="glyphicon glyphicon-comment"></span><b>Give a review</b>
</div>
</a>

{{--For autheniticated users--}}
@if(Auth::check())
<a class="give_review" href="{{ url('/products/'.$product->id) }}">
<div id="give_review_btn">
<span class="glyphicon glyphicon-comment"></span><b>Give a review</b>
</div>
</a>
@endif
</div>
@endif
</div>
</div>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
20 changes: 20 additions & 0 deletions resources/views/reviews/reviews_list.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="infinite-scroll">
@foreach($reviews as $review)
<div class="row">
<div class="col-sm-8">
<div class="panel panel-review panel-default">
<div class="panel-heading panel-heading-review">
<span class="glyphicon glyphicon-user"></span><span class="text-muted" style="margin-right: 10px"><u><b>{{$review->user->name}}</b></u></span>
<span class="glyphicon glyphicon-time"></span><span class="text-muted"><u><b>{{$review->created_at->toDateString()}}</b></u></span>
</div>
<div class="panel-body">
{{$review->review}}
</div>
</div>
</div>
</div>
@endforeach

{{ $reviews->links() }}

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@

<script src="{{ asset('js/add_to_cart_ajax.js') }}"></script>
<script src="{{ asset('js/delete_from_cart_view_products_ajax.js') }}"></script>
<script src="{{ asset('js/pagination_ajax.js') }}"></script>
<script src="{{ asset('js/pagination_products_ajax.js') }}"></script>

{{-- Change view (grid, list)--}}
<script src="{{ asset('js/change_view.js') }}"></script>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
Route::group(['prefix' => 'products'], function () {

Route::get('/{product}', 'ProductController@show');
Route::get('/{product}', 'ProductController@showReviews');
});

Route::get('/subcategories/{subcategory}/products', 'SubcategoryController@showAllProducts');
Expand Down

0 comments on commit be85757

Please sign in to comment.