|
|
@ -1,22 +1,25 @@ |
|
|
|
import { sprintf } from "sprintf-js"; |
|
|
|
import { NotificationService } from "../service/NotificationService"; |
|
|
|
import { AlertifyNotificationService } from "../service/AlertifyNotificationService"; |
|
|
|
|
|
|
|
export function fetchHandler(response: any) { |
|
|
|
const ns: NotificationService = new AlertifyNotificationService(); |
|
|
|
if (response.ok) { |
|
|
|
return response.json().then((json: any) => { |
|
|
|
// the status was ok and there is a json body
|
|
|
|
// return Promise.resolve({ json: json, response: response });
|
|
|
|
// alertify.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
|
|
|
|
ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status)); |
|
|
|
}).catch((err: any) => { |
|
|
|
// the status was ok but there is no json body
|
|
|
|
// return Promise.resolve({ response: response });
|
|
|
|
// alertify.success('Message sent succesfully' + sprintf(" (http code %d)", response.status));
|
|
|
|
ns.success('Message sent succesfully' + sprintf(" (http code %d)", response.status)); |
|
|
|
}); |
|
|
|
|
|
|
|
} else { |
|
|
|
return response.json().catch((err: any) => { |
|
|
|
// the status was not ok and there is no json body
|
|
|
|
// throw new Error(response.statusText);
|
|
|
|
// alertify.error('Some error occured. Please try again.');
|
|
|
|
ns.error('Some error occured. Please try again.'); |
|
|
|
}).then((json: any) => { |
|
|
|
// the status was not ok but there is a json body
|
|
|
|
// throw new Error(json.error.message); // example error message returned by a REST
|
|
|
@ -26,7 +29,7 @@ export function fetchHandler(response: any) { |
|
|
|
json.errors.forEach(function(data: any) { |
|
|
|
errorMessage += sprintf("Field Name: %s \n Rejected value: %s \n Reason: %s \n", data.field_name, data.rejected_value, data.error_message); |
|
|
|
}); |
|
|
|
// alertify.error(sprintf('There were errors in your message - %s', errorMessage));
|
|
|
|
ns.errorWithDelay(sprintf('There were errors in your message - %s', errorMessage), 30); |
|
|
|
// alertify.set('notifier', 'delay', delay);
|
|
|
|
}); |
|
|
|
} |
|
|
|