From 47ffd357ed689028751bed38c9d0ab030c5465be Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 7 Apr 2016 15:05:09 +0200 Subject: [PATCH] Bug 14974: Try to make benefit from using the API Previous patch immediately redirect after each action (add/edit/delete) so there is no real benefit from using the API. This patch move the add/edit form into a modal window, and redraw the table after each action, so the page is never reloaded. It also adds a messages area to put messages in (after success/error) --- .../intranet-tmpl/prog/en/modules/admin/cities.tt | 176 ++++++++++++++------- 1 file changed, 118 insertions(+), 58 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt index ae7428d..67c0343 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/cities.tt @@ -1,3 +1,32 @@ +[% BLOCK city_form %] + + + +
+
    + [% IF city %] +
  1. City ID: [% city.cityid %]
  2. + [% END %] +
  3. + + Required +
  4. +
  5. + + +
  6. +
  7. + + Required +
  8. +
  9. + + +
  10. +
+
+[% END %] + [% INCLUDE 'doc-head-open.inc' %] Koha › Administration › [% IF op =='add_form' %]Cities › [% IF city.cityid %] Modify city[% ELSE %] New city[% END %][% ELSE %][% IF op == 'delete_confirm' %]Cities › Confirm deletion of city[% ELSE %] Cities[% END %][% END %] [% INCLUDE 'doc-head-close.inc' %] @@ -28,8 +57,8 @@ this.city_state, this.city_zipcode, this.city_country, - "" + _("Edit") + "", - "" + _("Delete") + "" + "" + _("Edit") + "", + "" + _("Delete") + "" ]); }); json.iTotalRecords = data.length; @@ -40,11 +69,6 @@ } }); }, - 'fnRowCallback': function(nRow, aData, iDisplayIndex, iDisplayIndexFull) { - $("td:eq(6)", nRow).find('a').on('click', function() { - return delete_city($(this).attr('data-cityid')); - }); - }, // Should add filter/search/pagination // Not supported yet 'bAutoWidth': false, @@ -64,53 +88,95 @@ }; // Double check on required fields should be avoided if ( !city.city_name || !city.city_zipcode ) return; - cityid ? update_city(city) : add_city(city); + var jqxhr = cityid ? update_city(city) : add_city(city); + jqxhr.done(function() { + $('#city-form').modal('hide'); + $('#table_cities').dataTable().fnDraw(); + + var message; + if (cityid) { + message = _("The city was successfully updated"); + } else { + message = _("The city was successfully added"); + } + display_message(message); + }); + jqxhr.fail(function(data) { + display_error(data); + }); + }); + + $('#newcity').on('click', function(e) { + e.preventDefault(); + $('#city-form').modal({show: true}); + }) + + $('#table_cities').on('click', '.edit', function(e) { + e.preventDefault(); + var row = $(this).parents('tr'); + // FIXME There must be a better way to retrieve city data + var city = { + id: row.find('td:eq(0)').text(), + name: row.find('td:eq(1)').text(), + state: row.find('td:eq(2)').text(), + zipcode: row.find('td:eq(3)').text(), + country: row.find('td:eq(4)').text(), + }; + var form = $('#city-form').find('form'); + form.find('[name="cityid"]').val(city.id); + form.find('[name="city_name"]').val(city.name); + form.find('[name="city_state"]').val(city.state); + form.find('[name="city_zipcode"]').val(city.zipcode); + form.find('[name="city_country"]').val(city.country); + $('#city-form').modal({show: true}); + }); + $('#table_cities').on('click', '.delete', function(e) { + e.preventDefault(); + delete_city($(this).attr('data-cityid')) + .done(function(data){ + $('#table_cities').dataTable().fnDraw(); + + display_message(_("The city was successfully deleted")); + }) + .fail(function(data){ + display_error(data); + }); }); }); // The 3 functions should be extracted somewhere else function update_city(city) { - $.ajax({ + return $.ajax({ type: 'PUT', url: '/api/v1/cities/' + city.cityid, data: JSON.stringify(city), - success: function(data){ - // Need a better way to redirect, we would like to inform the user that the action succeeded - window.location.href='/cgi-bin/koha/admin/cities.pl'; - }, - error: function(data){ - // Need a better way to inform the user that the action has failed - alert(data); - } }); } function add_city(city) { - $.ajax({ + return $.ajax({ type: 'PUT', url: '/api/v1/cities', data: JSON.stringify(city), - success: function(data){ - window.location.href='/cgi-bin/koha/admin/cities.pl'; - }, - error: function(data){ - alert(data); - } }); } function delete_city(cityid) { if ( confirm(_("Are you sure you want to delete this city?")) ) { - $.ajax({ + return $.ajax({ type: 'DELETE', url: '/api/v1/cities/' + cityid, - success: function(data){ - window.location.href='/cgi-bin/koha/admin/cities.pl'; - }, - error: function(data){ - alert(data); - } - }) + }); } } + function display_message(msg) { + var message = $('
').addClass('dialog message').html(msg); + setTimeout(function() {message.fadeOut();}, 5000); + $('#messages').append(message); + } + function display_error(msg) { + var message = $('
').addClass('dialog alert').html(msg); + setTimeout(function() {message.fadeOut();}, 5000); + $('#messages').append(message); + } //]]> @@ -164,32 +230,7 @@ [% END %]
- - - -
-
    - [% IF city %] -
  1. City ID: [% city.cityid %]
  2. - [% END %] -
  3. - - Required -
  4. -
  5. - - -
  6. -
  7. - - Required -
  8. -
  9. - - -
  10. -
-
+ [% INCLUDE city_form city = city %]
@@ -205,6 +246,9 @@

Cities

+ +
+ [% IF searchfield %] Searching: [% searchfield %] [% END %] @@ -226,6 +270,22 @@ [% ELSE %] There is no city defined. Create a new city. [% END %] + + [% END %] -- 2.1.4