diff --git a/amiibofindr/apps/amiibo/views.py b/amiibofindr/apps/amiibo/views.py index 4b2f365..80e9541 100644 --- a/amiibofindr/apps/amiibo/views.py +++ b/amiibofindr/apps/amiibo/views.py @@ -84,7 +84,7 @@ class UserAmiiboView(View): '-wishlist': 'remove_wishlist', '+trade': 'add_trade', '-trade': 'remove_trade', - '=trade': 'toggle_trade', + '+-trade': 'toggle_trade', } def get(self, request, amiibo, action): @@ -97,6 +97,16 @@ class UserAmiiboView(View): if result: return result + # Handle templating + request_from = request.GET.get('from', None) + if request_from == 'detail': + return render(request, + 'amiibo/widgets/relation_header_buttons.html', + { + 'amiibo': amiibo, + 'item': amiibo + }) + return HttpResponseRedirect(amiibo.as_type().get_absolute_url()) def add_wishlist(self, request, amiibo): diff --git a/amiibofindr/static/app/relation.js b/amiibofindr/static/app/relation.js new file mode 100644 index 0000000..da6ab2a --- /dev/null +++ b/amiibofindr/static/app/relation.js @@ -0,0 +1,50 @@ +(function() { + var RelationComponent = function() { + this.relations = document.querySelectorAll('[data-handle="relation"]'); + this.initialize(); + }; + + RelationComponent.prototype.initialize = function() { + var self = this; + + // Prepare handlers + var linkHandler = function(event) { + event.preventDefault(); + self.click(event.target); + }; + + [].forEach.call(this.relations, function(buttons) { + $(buttons).on('click', 'a', linkHandler); + }); + }; + + RelationComponent.prototype.click = function(element) { + var $link = element; + var href = $link.href; + var $buttons = $(element).closest('[data-amiibo]'); + var from = $buttons.attr('data-from'); + var amiiboId = $buttons.attr('data-amiibo'); + var loader = $buttons.closest('[data-loader="' + amiiboId + '"]'); + this.doGet(href, from, loader, $buttons); + }; + + RelationComponent.prototype.doGet = function(href, from, loader, container) { + var self = this; + // loader.addClass('loading'); + + $.ajax({ + url: href, + data: { from: from }, + success: function(result) { + self.handleResult(loader, container, result); + } + }); + }; + + RelationComponent.prototype.handleResult = function(loader, container, result) { + container.html(result) + // loader.removeClass('loading'); + }; + + SimpleViews.register('relation', RelationComponent); +})(); diff --git a/amiibofindr/templates/amiibo/amiibo-figure.html b/amiibofindr/templates/amiibo/amiibo-figure.html index 542d501..fcc1caa 100644 --- a/amiibofindr/templates/amiibo/amiibo-figure.html +++ b/amiibofindr/templates/amiibo/amiibo-figure.html @@ -17,7 +17,7 @@ AMIIBO FIGURE {% block meta_twitter_url %}{{ amiibo.get_absolute_url }}{% endblock %} {% block meta_og_url %}{{ amiibo.get_absolute_url }}{% endblock %} -{% block js_views %}money,time{% endblock %} +{% block js_views %}money,time,relation{% endblock %} {% block breadcrumb %} {{ block.super }} @@ -30,7 +30,7 @@ AMIIBO FIGURE {% block main_content %}