Antoni09 commited on
Commit
e965bd5
verified
1 Parent(s): f12b2d3

Upload main.js

Browse files
Files changed (1) hide show
  1. main.js +41 -49
main.js CHANGED
@@ -1,9 +1,9 @@
1
- const APP_PATHNAME =
2
- typeof window !== "undefined" && window.location && typeof window.location.pathname === "string"
3
- ? window.location.pathname.replace(/\/$/, "")
4
- : "";
5
-
6
- const VAT_OPTIONS = [
7
  { value: "23", label: "23%" },
8
  { value: "8", label: "8%" },
9
  { value: "5", label: "5%" },
@@ -792,24 +792,24 @@ function exitInvoiceEdit() {
792
  cancelEditInvoiceButton.classList.add("hidden");
793
  }
794
 
795
- function buildApiUrl(path = "") {
796
- if (!path) {
797
- return APP_PATHNAME || "/";
798
- }
799
- if (/^https?:\/\//i.test(path)) {
800
- return path;
801
- }
802
- return path.startsWith("/")
803
- ? `${APP_PATHNAME}${path}` || "/"
804
- : `${APP_PATHNAME}/${path}`.replace(/\/{2,}/g, "/");
805
- }
806
-
807
- async function apiRequest(path, { method = "GET", body, headers = {} } = {}, requireAuth = false) {
808
- const options = {
809
- method,
810
- headers: {
811
- "Content-Type": "application/json",
812
- ...headers,
813
  },
814
  };
815
 
@@ -824,8 +824,8 @@ async function apiRequest(path, { method = "GET", body, headers = {} } = {}, req
824
  options.headers.Authorization = `Bearer ${authToken}`;
825
  }
826
 
827
- const url = buildApiUrl(path);
828
- const response = await fetch(url, options);
829
  const isJson = response.headers.get("content-type")?.includes("application/json");
830
  const data = isJson ? await response.json() : {};
831
 
@@ -1679,28 +1679,19 @@ async function initialize() {
1679
  closeRegisterPanel({ resetForm: true, focusTrigger: false });
1680
  clearFeedback(registerFeedback);
1681
  clearFeedback(loginFeedback);
1682
- try {
1683
- const status = await apiRequest("/api/status");
1684
- if (typeof status.max_logo_size === "number") {
1685
- maxLogoSize = status.max_logo_size;
1686
- }
1687
- if (status.legacy_login_hint) {
1688
- legacyLoginHint.textContent = `Zaloguj si臋 korzystajac ze starego loginu "${status.legacy_login_hint}", a nastepnie dodaj adres email.`;
1689
- legacyLoginHint.classList.remove("hidden");
1690
- } else {
1691
- legacyLoginHint.classList.add("hidden");
1692
- legacyLoginHint.textContent = "";
1693
- }
1694
-
1695
- if (authToken) {
1696
- await bootstrapApp();
1697
- }
1698
- } catch (error) {
1699
- console.error(error);
1700
- showFeedback(registerFeedback, "Nie uda艂o si臋 nawi膮za膰 po艂膮czenia z serwerem.");
1701
  }
1702
  }
1703
 
 
1704
  if (registerForm && registerFeedback && loginFeedback) {
1705
  registerForm.addEventListener("submit", async (event) => {
1706
  event.preventDefault();
@@ -1728,6 +1719,8 @@ if (registerForm && registerFeedback && loginFeedback) {
1728
 
1729
  const payload = {
1730
  email: emailValue,
 
 
1731
  company_name: formData.get("company_name")?.toString().trim(),
1732
  owner_name: formData.get("owner_name")?.toString().trim(),
1733
  address_line: formData.get("address_line")?.toString().trim(),
@@ -1735,11 +1728,10 @@ if (registerForm && registerFeedback && loginFeedback) {
1735
  city: formData.get("city")?.toString().trim(),
1736
  tax_id: formData.get("tax_id")?.toString().trim(),
1737
  bank_account: formData.get("bank_account")?.toString().trim(),
1738
- password,
1739
  };
1740
 
1741
  try {
1742
- await apiRequest("/api/setup", { method: "POST", body: payload });
1743
  showFeedback(registerFeedback, "Konto utworzone. Mo偶esz sie zalogowac.", "success");
1744
  if (loginForm && loginForm.elements.email) {
1745
  loginForm.elements.email.value = emailValue;
@@ -1811,7 +1803,7 @@ if (loginForm && loginFeedback) {
1811
  const errorMessage = error instanceof Error ? error.message : String(error || "");
1812
  let feedbackMessage = errorMessage || "Logowanie nie powiod艂o si臋.";
1813
  if (/nieprawidlowy (login|email) lub has艂o/i.test(errorMessage)) {
1814
- feedbackMessage = "Podany email lub has艂o s膮 nieprawid艂owe. Utw贸rz konto, je艣li jeszcze go nie masz.";
1815
  } else if (/brak autoryzacji/i.test(errorMessage) || /brak tokenu autoryzacyjnego/i.test(errorMessage)) {
1816
  feedbackMessage = "Sesja wygas艂a. Zaloguj si臋 ponownie.";
1817
  } else if (/failed to fetch|networkerror/i.test(errorMessage) || error instanceof TypeError) {
@@ -1859,7 +1851,7 @@ if (businessForm && businessFeedback) {
1859
  };
1860
 
1861
  try {
1862
- const data = await apiRequest("/api/business", { method: "PUT", body: payload }, true);
1863
  currentBusiness = data.business;
1864
  renderBusinessDisplay(currentBusiness);
1865
  fillBusinessForm(currentBusiness);
 
1
+ const APP_PATHNAME =
2
+ typeof window !== "undefined" && window.location && typeof window.location.pathname === "string"
3
+ ? window.location.pathname.replace(/\/$/, "")
4
+ : "";
5
+
6
+ const VAT_OPTIONS = [
7
  { value: "23", label: "23%" },
8
  { value: "8", label: "8%" },
9
  { value: "5", label: "5%" },
 
792
  cancelEditInvoiceButton.classList.add("hidden");
793
  }
794
 
795
+ function buildApiUrl(path = "") {
796
+ if (!path) {
797
+ return APP_PATHNAME || "/";
798
+ }
799
+ if (/^https?:\/\//i.test(path)) {
800
+ return path;
801
+ }
802
+ return path.startsWith("/")
803
+ ? `${APP_PATHNAME}${path}` || "/"
804
+ : `${APP_PATHNAME}/${path}`.replace(/\/{2,}/g, "/");
805
+ }
806
+
807
+ async function apiRequest(path, { method = "GET", body, headers = {} } = {}, requireAuth = false) {
808
+ const options = {
809
+ method,
810
+ headers: {
811
+ "Content-Type": "application/json",
812
+ ...headers,
813
  },
814
  };
815
 
 
824
  options.headers.Authorization = `Bearer ${authToken}`;
825
  }
826
 
827
+ const url = buildApiUrl(path);
828
+ const response = await fetch(url, options);
829
  const isJson = response.headers.get("content-type")?.includes("application/json");
830
  const data = isJson ? await response.json() : {};
831
 
 
1679
  closeRegisterPanel({ resetForm: true, focusTrigger: false });
1680
  clearFeedback(registerFeedback);
1681
  clearFeedback(loginFeedback);
1682
+ if (legacyLoginHint) {
1683
+ legacyLoginHint.classList.add("hidden");
1684
+ legacyLoginHint.textContent = "";
1685
+ }
1686
+ if (authToken) {
1687
+ await bootstrapApp().catch((error) => {
1688
+ console.error(error);
1689
+ showFeedback(registerFeedback, "Nie uda?o si? nawi?za? po??czenia z serwerem.");
1690
+ });
 
 
 
 
 
 
 
 
 
 
1691
  }
1692
  }
1693
 
1694
+
1695
  if (registerForm && registerFeedback && loginFeedback) {
1696
  registerForm.addEventListener("submit", async (event) => {
1697
  event.preventDefault();
 
1719
 
1720
  const payload = {
1721
  email: emailValue,
1722
+ password,
1723
+ confirm_password: confirmPassword,
1724
  company_name: formData.get("company_name")?.toString().trim(),
1725
  owner_name: formData.get("owner_name")?.toString().trim(),
1726
  address_line: formData.get("address_line")?.toString().trim(),
 
1728
  city: formData.get("city")?.toString().trim(),
1729
  tax_id: formData.get("tax_id")?.toString().trim(),
1730
  bank_account: formData.get("bank_account")?.toString().trim(),
 
1731
  };
1732
 
1733
  try {
1734
+ await apiRequest("/api/register", { method: "POST", body: payload });
1735
  showFeedback(registerFeedback, "Konto utworzone. Mo偶esz sie zalogowac.", "success");
1736
  if (loginForm && loginForm.elements.email) {
1737
  loginForm.elements.email.value = emailValue;
 
1803
  const errorMessage = error instanceof Error ? error.message : String(error || "");
1804
  let feedbackMessage = errorMessage || "Logowanie nie powiod艂o si臋.";
1805
  if (/nieprawidlowy (login|email) lub has艂o/i.test(errorMessage)) {
1806
+ feedbackMessage = "Podany email lub has艂o s膮 nieprawid艂owe. Utw贸rz konto, je艣li jeszcze go nie masz.";
1807
  } else if (/brak autoryzacji/i.test(errorMessage) || /brak tokenu autoryzacyjnego/i.test(errorMessage)) {
1808
  feedbackMessage = "Sesja wygas艂a. Zaloguj si臋 ponownie.";
1809
  } else if (/failed to fetch|networkerror/i.test(errorMessage) || error instanceof TypeError) {
 
1851
  };
1852
 
1853
  try {
1854
+ const data = await apiRequest("/api/business", { method: "POST", body: payload }, true);
1855
  currentBusiness = data.business;
1856
  renderBusinessDisplay(currentBusiness);
1857
  fillBusinessForm(currentBusiness);