|
| 1 | +<style> |
| 2 | + .error-popup-wrapper { |
| 3 | + position: fixed; |
| 4 | + top: 16px; |
| 5 | + right: 50px; |
| 6 | + z-index: 1000; |
| 7 | + } |
| 8 | + .error-popup { |
| 9 | + display: none; |
| 10 | + background: #ffffff; |
| 11 | + border-radius: 8px; |
| 12 | + padding: 14px 18px; |
| 13 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
| 14 | + min-width: 260px; |
| 15 | + } |
| 16 | + .error-popup__title { |
| 17 | + font-size: 14px; |
| 18 | + font-weight: 600; |
| 19 | + margin-bottom: 6px; |
| 20 | + color: #d32f2f; |
| 21 | + } |
| 22 | + .error-popup__message { |
| 23 | + font-size: 13px; |
| 24 | + margin-bottom: 10px; |
| 25 | + } |
| 26 | + .error-popup__actions { |
| 27 | + display: flex; |
| 28 | + justify-content: flex-end; |
| 29 | + } |
| 30 | + .error-popup__ok-btn { |
| 31 | + cursor: pointer; |
| 32 | + padding: 4px 12px; |
| 33 | + font-size: 13px; |
| 34 | + border-radius: 4px; |
| 35 | + border: none; |
| 36 | + } |
| 37 | +</style> |
| 38 | + |
| 39 | +<div class="error-popup-wrapper"> |
| 40 | + <div id="error-popup" class="error-popup"> |
| 41 | + <div class="error-popup__title">알림</div> |
| 42 | + <div id="error-popup-message" class="error-popup__message"></div> |
| 43 | + <div class="error-popup__actions"> |
| 44 | + <button id="error-popup-ok" class="error-popup__ok-btn" type="button"> |
| 45 | + OK |
| 46 | + </button> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | +</div> |
| 50 | + |
| 51 | +<script> |
| 52 | + (function () { |
| 53 | + const popup = document.getElementById("error-popup"); |
| 54 | + const msgEl = document.getElementById("error-popup-message"); |
| 55 | + const okBtn = document.getElementById("error-popup-ok"); |
| 56 | + |
| 57 | + window.showErrorPopup = function (message, title) { |
| 58 | + const titleEl = popup.querySelector(".error-popup__title"); |
| 59 | + msgEl.textContent = message || "오류가 발생했습니다."; |
| 60 | + if (title) titleEl.textContent = title; |
| 61 | + popup.style.display = "block"; |
| 62 | + }; |
| 63 | + |
| 64 | + window.hideErrorPopup = function () { |
| 65 | + popup.style.display = "none"; |
| 66 | + }; |
| 67 | + |
| 68 | + okBtn.addEventListener("click", () => window.hideErrorPopup()); |
| 69 | + |
| 70 | + // ---- 공통 submit 가로채기 (B안: data-ajax 있는 폼만) ---- |
| 71 | + document.addEventListener("submit", async (e) => { |
| 72 | + const form = e.target; |
| 73 | + if (!(form instanceof HTMLFormElement)) return; |
| 74 | + |
| 75 | + // 안전하게: data-ajax="true"인 폼만 가로챔 |
| 76 | + if (form.dataset.ajax !== "true") return; |
| 77 | + |
| 78 | + e.preventDefault(); |
| 79 | + |
| 80 | + // multipart/form-data는 여기서 처리 안 함(파일 업로드 등) |
| 81 | + const enctype = (form.enctype || "").toLowerCase(); |
| 82 | + if (enctype.includes("multipart/form-data")) { |
| 83 | + window.showErrorPopup("파일 업로드 폼은 지원하지 않습니다.", "오류"); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const body = new URLSearchParams(new FormData(form)); |
| 88 | + |
| 89 | + try { |
| 90 | + const res = await fetch(form.action, { |
| 91 | + method: (form.method || "POST").toUpperCase(), |
| 92 | + headers: { |
| 93 | + "Accept": "application/json", |
| 94 | + "Content-Type": "application/x-www-form-urlencoded", |
| 95 | + }, |
| 96 | + body, |
| 97 | + credentials: "same-origin", |
| 98 | + }); |
| 99 | + |
| 100 | + if (res.redirected) { |
| 101 | + window.location.href = res.url; |
| 102 | + return; |
| 103 | + } else if (res.ok) { |
| 104 | + window.location.href = "/"; |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + let err = null; |
| 109 | + try { err = await res.json(); } catch {} |
| 110 | + |
| 111 | + const message = |
| 112 | + (err && (err.message || err.errorMessage)) || |
| 113 | + "요청 처리에 실패했습니다."; |
| 114 | + |
| 115 | + window.showErrorPopup(message, "요청 실패"); |
| 116 | + } catch (networkError) { |
| 117 | + window.showErrorPopup("서버와 통신 중 문제가 발생했습니다.", "오류"); |
| 118 | + } |
| 119 | + }, true); // capture로 잡으면 더 안정적으로 submit을 가로챌 수 있음 |
| 120 | + })(); |
| 121 | +</script> |
0 commit comments