Просмотр исходного кода

Add confirmation prior to sending email

Alan Lam 3 лет назад
Родитель
Сommit
89ab83cca4
3 измененных файлов с 75 добавлено и 29 удалено
  1. 55 19
      public/js/document-page.js
  2. 18 10
      public/js/sale-page.js
  3. 2 0
      views/header.php

+ 55 - 19
public/js/document-page.js

@@ -24,16 +24,24 @@ class DocumentPage {
 
 		$table.find(".btn-delete-document-item").on("click", event => {
 			const documentItemId = $(event.currentTarget).data("document-item-id");
-			const confirmed = confirm("Are you sure you want to delete this item?");
-			if(confirmed) {
-				$.post(window.location + "/delete-item", {
-					document_item_id: documentItemId
-				}).then(response => {
-					toastr.success("Item deleted successfully.");
-					$("#document-items-section").html(response.html);
-					DocumentPage.InitItemsSection();
-				});
-			}
+
+			swal.fire({
+				icon: "question",
+				title: "Deleting Item",
+				text: "Are you sure you want to delete this item?",
+				confirmButtonText: "Delete",
+				showCancelButton: true
+			}).then(result => {
+				if(result.isConfirmed) {
+					$.post(window.location + "/delete-item", {
+						document_item_id: documentItemId
+					}).then(response => {
+						toastr.success("Item deleted successfully.");
+						$("#document-items-section").html(response.html);
+						DocumentPage.InitItemsSection();
+					});
+				}
+			});
 		});
 	}
 
@@ -54,15 +62,43 @@ class DocumentPage {
 			const { variant } = $element.data();
 
 			DocumentPage.SaveForm().then(() => {
-				toastr.info("Emailing PDF...");
-				$.post(window.location + "/email-pdf", {
-					variant: variant
-				}).then(() => {
-					toastr.success("Email sent successfully.");
-				}).catch(error => {
-					const { responseJSON } = error;
-					if(responseJSON && responseJSON.message) {
-						toastr.error(responseJSON.message);
+
+				const showErrorAlert = (error) => {
+					swal.fire("Error", error, "error");
+				};
+
+				const contactName = $("#contact-name").val();
+				if(!contactName) {
+					showErrorAlert("Contact Name must be set.")
+					return;
+				}
+
+				const contactEmail = $("#contact-email").val();
+				if(!contactEmail) {
+					showErrorAlert("Contact Email must be set.")
+					return;
+				}
+
+				const $p1 = $("<p>").text("Are you sure you want to email the PDF to:");
+				const $p2 = $("<div>").text(`${contactName} <${contactEmail}>`);
+
+				swal.fire({
+					icon: "question",
+					title: "Emailing PDF",
+					html: $p1.get(0).outerHTML + $p2.get(0).outerHTML,
+					confirmButtonText: "Send Email",
+					showCancelButton: true,
+					preConfirm: () => {
+						return $.post(window.location + "/email-pdf", {
+							variant: variant
+						}).then(() => {
+							toastr.success("Email sent successfully.");
+						}).catch(error => {
+							const { responseJSON } = error;
+							if(responseJSON && responseJSON.message) {
+								toastr.error(responseJSON.message);
+							}
+						});
 					}
 				});
 			});

+ 18 - 10
public/js/sale-page.js

@@ -24,16 +24,24 @@ class SalePage {
 
 		$table.find(".btn-delete-sale-item").on("click", event => {
 			const saleItemId = $(event.currentTarget).data("sale-item-id");
-			const confirmed = confirm("Are you sure you want to delete this lead item?");
-			if(confirmed) {
-				$.post(window.location + "/delete-item", {
-					sale_item_id: saleItemId
-				}).then(response => {
-					toastr.success("Lead item deleted successfully.");
-					$("#sale-items-section").html(response.html);
-					SalePage.InitSaleItemSection();
-				});
-			}
+
+			swal.fire({
+				icon: "question",
+				title: "Deleting Item",
+				text: "Are you sure you want to delete this lead item?",
+				confirmButtonText: "Delete",
+				showCancelButton: true
+			}).then(result => {
+				if(result.isConfirmed) {
+					$.post(window.location + "/delete-item", {
+						sale_item_id: saleItemId
+					}).then(response => {
+						toastr.success("Lead item deleted successfully.");
+						$("#sale-items-section").html(response.html);
+						SalePage.InitSaleItemSection();
+					});
+				}
+			});
 		});
 	}
 	static SaveForm() {

+ 2 - 0
views/header.php

@@ -30,6 +30,8 @@ $google_api_key = $_ENV["GOOGLE_API_KEY"] ?? null;
 
 	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
 
+	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.6.16/dist/sweetalert2.all.min.js" integrity="sha256-b+fVEXPbYTCRvyFMiFsP3TAaUr7X3fbvPaPg5JqwsDw=" crossorigin="anonymous"></script>
+
 	<?php
 	if($google_api_key) {
 		?>