{% set datasets = h.ckanext_ids_recomm_datasets_sidebar(entity, count) %}
{% set services = h.ckanext_ids_recomm_services_sidebar(entity, count) %}
{% set applications = h.ckanext_ids_recomm_applications_sidebar(entity, count) %}

{% if datasets or services or applications %}

	<div class="module module-search module-narrow module-shallow box">

		<div class="module-content" style="display:table; text-align:center; width:100%;">
			
			<h3 class="heading">Recommendations</h3>
			
			{% if datasets %}
				<div>
					<h4 style="font-size:16px;">Datasets</h4>
					{% for dataset in datasets %}
						<div class="row">
							<a onclick="storeRecommClick( '{{ dataset['pkg_id'] }}', '{{ dataset['recoId'] }}' );" href="{{ dataset['id'] }} ">{{ dataset['title'] }}</a>
						</div>
					{% endfor %}
				</div>
			{% endif %}
			
			{% if services %}
				<div>
					<h4 style="font-size:16px;">Services</h4>
					{% for service in services %}
						<div class="row">
							<a onclick="storeRecommClick( '{{ service['pkg_id'] }}', '{{ service['recoId'] }}' );" href="{{ service['id'] }} ">{{ service['title'] }}</a>
						</div>
					{% endfor %}
				</div>
			{% endif %}

			{% if applications %}
				<div>
					<h4 style="font-size:16px;">Applications</h4>
					{% for application in applications %}
						<div class="row">
							<a onclick="storeRecommClick( '{{ application['pkg_id'] }}', '{{ application['recoId'] }}' );" href="{{ application['id'] }} ">{{ application['title'] }}</a>
						</div>
					{% endfor %}
				</div>
			{% endif %}	
		</div>
	</div>
{% endif %}

<script>
    function storeRecommClick(entityId, recoId) {
         
        const XHR = new XMLHttpRequest(),
        FD  = new FormData();

		var data = {
		  entityId: entityId, 
		  recoId: recoId
		};
		
		for( name in data ) {
            FD.append( name, data[ name ] );
        }

        XHR.addEventListener( 'load', function( event ) {
			return true;
        } );

        XHR.addEventListener(' error', function( event ) {
            console.log("failed to store view recomm interaction");
			return true;
        } );

        XHR.open( 'POST', '/ids/actions/store_view_recomm_interaction' );
		XHR.send( FD );
		
		return true;
    }
</script>