troubleshooting Question

Could you point a way to enable/ disable an like icon depending on the user had clicked it before or not?

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
JavaScriptPHPjQueryLaravel
36 Comments2 Solutions161 ViewsLast Modified:
Hi Experts


Could you point a way to enable/ disable an like icon depending on the user had clicked the icon before or not?

Accordingly with:

img004
The user has the right of click the "Like" of  a photo once and after that he coudn't do nothing else, the like icon must to be disabled.
If the user reenter the page the like icon for the photo that had received a "like" must to stay disabled, also, by using the user's id.

The actual view:

@extends('layouts.appinterno')

@section('content')

	<div class='row'>

	{{ csrf_field() }}
    <input type="hidden" id="hdnCurtiu" value="{{ $curtiu }}">

        <div class="col-md-12 col-sm-12">
               
                <section class="row">
               
                        @foreach($vitrinesX->chunk(3) as $row)
                        
                            @foreach($row as $vitrine)
                          
                               <div class="col-sm-12 col-md-4">
                               
                                    <div class="box-noticias">
                                       	<a href="javascript:void(0);" onclick='xxx.vitrine.abrirVitrine({{$vitrine->id}});' >
                	    				{{$vitrine->titulo}}
                	    			    </a>                    
                                    </div> 
                                    
                                   	<p id="like" class="likes" onclick="xxx.vitrine.salvarEscolha({{ $vitrine->id }}, 1)">
    				                    <i class="fa fa-thumbs-up"></i> <span id="qtdeCurtiu_{{ $vitrine->id }}">{{ $vitrine->qtdeCurtiu }}</span>
    				               	</p> 
                     
                               </div>     
                              
                            @endforeach
                            
                        @endforeach
                          
                                                  
                </section>
			</div>

	</div>
@endsection


@section('scripts')

	<script src="{{ asset('js/xxx/xxx.Formatadores.js').'?v='xxx(uniqid()) }}" ></script>
	<script src="{{ asset('js/xxx/xxx.Vitrine.js').'?v='xxx(uniqid()) }}" ></script>

	<script>
		$(document).ready(function() {
			if($('#hdnCurtiu').val() == "1")
				$('#like').addClass('like');
			else if($('#hdnCurtiu').val() == "0")
				$('#dislike').addClass('like');
		})
	</script>

@endsection

I guess when the page starts it must to be checked on the existent jQuery code

JS code for the view (where I guess it must to be checked):

Vitrine.prototype.abrirVitrine = function(id) {
  $.ajax({
    url: "/vitrine/" + id + "/obter",
    method: "GET",
    error: function() {
      xxx.openModalCustom(
        "Erro",
        "Erro ao obter a notícia.",
        "Entendi",
        "error"
      );
    },
    success: function(data) {
      datacriacao = xxx.formatadores.DataBDParaDataBrasil(
        data.retorno.created_at
      );
      titulo = data.retorno.titulo;
      subtitulo = data.retorno.subtitulo;
      conteudo = data.retorno.conteudo;
      url = data.retorno.url;
      $("#datacriacao").html(datacriacao);
      $("#titulo").html(titulo);
      $("#subtitulo").html(subtitulo);
      $("#conteudo").html(conteudo);
      $("#image").attr("src", url);
      $("html, body").animate(
        { scrollTop: $("#datacriacao").offset().top },
        1000
      );
    }
  });
};


Controller code (where the above JS calls the obter method):
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

use Debugbar;
use DB;

class UserVitrine extends Model
{
    public function obter($user_id, $vitrine_id){
    
        return UserVitrine
            ::where('user_id', \DB::raw($user_id))
            ->where('vitrine_id', \DB::raw($vitrine_id))
            ->first();
    }
    
    public function obterCurtidasPorVitrine($vitrine_id, $curtiu){

        return UserVitrine
            ::where('vitrine_id', \DB::raw($vitrine_id))
            ->where('curtiu', \DB::raw($curtiu))
            ->count();
            
    }		
    
 		
}

Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 36 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 36 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros