mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
133 lines
4.0 KiB
Plaintext
133 lines
4.0 KiB
Plaintext
// Shipments AJAX API
|
|
|
|
$(document).ready(function() {
|
|
|
|
handle_ship_click = function(){
|
|
var link = $(this);
|
|
var shipment_number = link.data('shipment-number');
|
|
var url = Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number + "/ship.json");
|
|
$.ajax({
|
|
type: "PUT",
|
|
url: url
|
|
}).done(function( msg ) {
|
|
window.location.reload();
|
|
}).error(function( msg ) {
|
|
console.log(msg);
|
|
});
|
|
}
|
|
$('[data-hook=admin_order_edit_form] a.ship').click(handle_ship_click);
|
|
|
|
//handle shipping method edit click
|
|
$('a.edit-method').click(toggleMethodEdit);
|
|
$('a.cancel-method').click(toggleMethodEdit);
|
|
|
|
handle_shipping_method_save = function(){
|
|
var link = $(this);
|
|
var shipment_number = link.data('shipment-number');
|
|
var selected_shipping_rate_id = link.parents('tbody').find("select#selected_shipping_rate_id[data-shipment-number='" + shipment_number + "']").val();
|
|
var url = Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipment_number + ".json");
|
|
|
|
$.ajax({
|
|
type: "PUT",
|
|
url: url,
|
|
data: { shipment: { selected_shipping_rate_id: selected_shipping_rate_id } }
|
|
}).done(function( msg ) {
|
|
window.location.reload();
|
|
}).error(function( msg ) {
|
|
console.log(msg);
|
|
});
|
|
}
|
|
$('[data-hook=admin_order_edit_form] a.save-method').click(handle_shipping_method_save);
|
|
|
|
//handle tracking info edit/delete
|
|
|
|
// Show the input field to edit the tracking info
|
|
// And hide the input field when cancel is clicked
|
|
$('a.edit-tracking').click(toggleTrackingEdit);
|
|
$('a.cancel-tracking').click(toggleTrackingEdit);
|
|
|
|
saveTrackingInfo = function(){
|
|
let shipmentNumber = $(this).data('shipment-number');
|
|
let tracking = document.getElementById('tracking').value
|
|
|
|
makeApiCall(trackingUrl(shipmentNumber), { shipment: { tracking: tracking } } )
|
|
}
|
|
|
|
deleteTrackingInfo = function(){
|
|
let shipmentNumber = $(this).data('shipment-number');
|
|
let tracking = ''
|
|
|
|
confirmDelete(trackingUrl(shipmentNumber), { shipment: { tracking: tracking } })
|
|
}
|
|
|
|
trackingUrl = function(shipmentNumber){
|
|
return Spree.url( Spree.routes.orders_api + "/" + order_number + "/shipments/" + shipmentNumber + ".json");
|
|
}
|
|
|
|
$('[data-hook=admin_order_edit_form] a.save-tracking').click(saveTrackingInfo);
|
|
$('[data-hook=admin_order_edit_form] a.delete-tracking').click(deleteTrackingInfo);
|
|
|
|
// handle note edit/delete
|
|
|
|
// Show the input field to edit the note
|
|
// And hide the input field when cancel is clicked
|
|
$('a.edit-note.icon-edit').click(toggleNoteEdit);
|
|
$('a.cancel-note').click(toggleNoteEdit);
|
|
|
|
saveNote = function(){
|
|
let note = document.getElementById('note').value
|
|
makeApiCall(getNoteUrl(), { note: note })
|
|
}
|
|
|
|
deleteNote = function(){
|
|
let note = ''
|
|
confirmDelete(getNoteUrl(), { note: note })
|
|
}
|
|
|
|
getNoteUrl = function(){
|
|
return Spree.url( Spree.routes.orders_api + "/" + order_number);
|
|
}
|
|
|
|
confirmDelete = function(url, params){
|
|
displayDeleteAlert(function(confirmation) {
|
|
if (confirmation) {
|
|
makeApiCall(url, params)
|
|
}
|
|
});
|
|
}
|
|
|
|
$('[data-hook=admin_order_edit_form] a.save-note').click(saveNote);
|
|
$('[data-hook=admin_order_edit_form] a.delete-note').click(deleteNote);
|
|
|
|
// Makes API call for notes/tracking info
|
|
makeApiCall = function(url, params) {
|
|
$.ajax({
|
|
type: "PUT",
|
|
url: url,
|
|
data: params
|
|
}).done(function( msg ) {
|
|
window.location.reload();
|
|
}).error(function( msg ) {
|
|
console.log(msg);
|
|
});
|
|
}
|
|
|
|
displayDeleteAlert = function(callback) {
|
|
i18nKey = "are_you_sure";
|
|
$('#custom-confirm .message').html(
|
|
` ${t(i18nKey)}
|
|
<div class="form">
|
|
</div>`);
|
|
$('#custom-confirm button.confirm').unbind( "click" ).click(() => {
|
|
$('#custom-confirm').hide();
|
|
callback(true);
|
|
});
|
|
$('#custom-confirm button.cancel').click(() => {
|
|
$('#custom-confirm').hide();
|
|
callback(false)
|
|
});
|
|
$('#custom-confirm').show();
|
|
}
|
|
|
|
});
|