Bugzilla – Attachment 50022 Details for
Bug 14974
Use the REST API for cities
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 14974: Try to make benefit from using the API
Bug-14974-Try-to-make-benefit-from-using-the-API.patch (text/plain), 11.33 KB, created by
Julian Maurice
on 2016-04-07 13:14:45 UTC
(
hide
)
Description:
Bug 14974: Try to make benefit from using the API
Filename:
MIME Type:
Creator:
Julian Maurice
Created:
2016-04-07 13:14:45 UTC
Size:
11.33 KB
patch
obsolete
>From 47ffd357ed689028751bed38c9d0ab030c5465be Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >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 %] >+ <input type="hidden" name="op" value="add_validate" /> >+ <input type="hidden" name="cityid" id="cityid" value="[% city.cityid %]" /> >+ >+ <fieldset class="rows"> >+ <ol> >+ [% IF city %] >+ <li><span class="label">City ID: </span>[% city.cityid %]</li> >+ [% END %] >+ <li> >+ <label for="city_name" class="required">City: </label> >+ <input type="text" name="city_name" id="city_name" size="40" maxlength="100" value="[% city.city_name |html %]" required="required" class="required" /> <span class="required">Required</span> >+ </li> >+ <li> >+ <label for="city_state">State: </label> >+ <input type="text" name="city_state" id="city_state" size="40" maxlength="100" value="[% city.city_state |html %]" /> >+ </li> >+ <li> >+ <label for="city_zipcode" class="required">ZIP/Postal code: </label> >+ <input type="text" name="city_zipcode" id="city_zipcode" size="20" maxlength="20" value="[% city.city_zipcode %]" required="required" class="required" /> <span class="required">Required</span> >+ </li> >+ <li> >+ <label for="city_country">Country: </label> >+ <input type="text" name="city_country" id="city_country" size="40" maxlength="100" value="[% city.city_country |html %]" /> >+ </li> >+ </ol> >+ </fieldset> >+[% END %] >+ > [% INCLUDE 'doc-head-open.inc' %] > <title>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 %]</title> > [% INCLUDE 'doc-head-close.inc' %] >@@ -28,8 +57,8 @@ > this.city_state, > this.city_zipcode, > this.city_country, >- "<a href='/cgi-bin/koha/admin/cities.pl?op=add_form&cityid=" + this.cityid + "'>" + _("Edit") + "</a>", >- "<a data-cityid='" + this.cityid + "' href='#'>" + _("Delete") + "</a>" >+ "<a href='/cgi-bin/koha/admin/cities.pl?op=add_form&cityid=" + this.cityid + "' class='edit'>" + _("Edit") + "</a>", >+ "<a data-cityid='" + this.cityid + "' href='#' class='delete'>" + _("Delete") + "</a>" > ]); > }); > 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 = $('<div></div>').addClass('dialog message').html(msg); >+ setTimeout(function() {message.fadeOut();}, 5000); >+ $('#messages').append(message); >+ } >+ function display_error(msg) { >+ var message = $('<div></div>').addClass('dialog alert').html(msg); >+ setTimeout(function() {message.fadeOut();}, 5000); >+ $('#messages').append(message); >+ } > //]]> > </script> > </head> >@@ -164,32 +230,7 @@ > [% END %] > > <form id="add_city" action="/cgi-bin/koha/admin/cities.pl" name="Aform" method="post" class="validated"> >- <input type="hidden" name="op" value="add_validate" /> >- <input type="hidden" name="cityid" id="cityid" value="[% city.cityid %]" /> >- >- <fieldset class="rows"> >- <ol> >- [% IF city %] >- <li><span class="label">City ID: </span>[% city.cityid %]</li> >- [% END %] >- <li> >- <label for="city_name" class="required">City: </label> >- <input type="text" name="city_name" id="city_name" size="80" maxlength="100" value="[% city.city_name |html %]" required="required" class="required" /> <span class="required">Required</span> >- </li> >- <li> >- <label for="city_state">State: </label> >- <input type="text" name="city_state" id="city_state" size="80" maxlength="100" value="[% city.city_state |html %]" /> >- </li> >- <li> >- <label for="city_zipcode" class="required">ZIP/Postal code: </label> >- <input type="text" name="city_zipcode" id="city_zipcode" size="20" maxlength="20" value="[% city.city_zipcode %]" required="required" class="required" /> <span class="required">Required</span> >- </li> >- <li> >- <label for="city_country">Country: </label> >- <input type="text" name="city_country" id="city_country" size="80" maxlength="100" value="[% city.city_country |html %]" /> >- </li> >- </ol> >- </fieldset> >+ [% INCLUDE city_form city = city %] > > <fieldset class="action"> > <input type="submit" value="Submit" /> >@@ -205,6 +246,9 @@ > </div> > > <h2>Cities</h2> >+ >+ <div id="messages"></div> >+ > [% IF searchfield %] > Searching: [% searchfield %] > [% END %] >@@ -226,6 +270,22 @@ > [% ELSE %] > There is no city defined. <a href="/cgi-bin/koha/admin/cities.pl?op=add_form">Create a new city</a>. > [% END %] >+ >+ <div id="city-form" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="dataPreviewLabel" aria-hidden="true"> >+ <form id="add_city"> >+ <div class="modal-header"> >+ <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">Ã</button> >+ <h3 id="city-form-label">Add city</h3> >+ </div> >+ <div class="modal-body"> >+ [% INCLUDE city_form %] >+ </div> >+ <div class="modal-footer"> >+ <button class="btn" type="submit">Submit</button> >+ <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button> >+ </div> >+ </form> >+ </div> > [% END %] > > </div> >-- >2.1.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 14974
:
43194
| 50022