summaryrefslogtreecommitdiffstats
path: root/src/main.js
blob: db9a00f37113c37916e96ca64d15f97710fd3137 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//import './style.css';
function edit(e) {
  e.preventDefault();
  const tar = e.target;
  const form = tar.parentNode;
  form.onseeked;
  const id = tar.dataset.id;
  const tds = document.querySelectorAll(`[data-id="${id}"]`);
  const names = ["name", "thumbnail", "author", "magazine",];
  tds.forEach((el, i) => {
    if (el.tagName === "TD") {
      const v = el.innerHTML;
      el.innerHTML = `<input name="${names[i]}" value="${v}">`;
    }
    else if (el === tar) {
      el.style.display = "none";
    }
    else {
      const iel = el;
      if (iel.type !== "hidden") {
        iel.value = "Save";
        el.onclick = e => save(e, tds);
      }
    }
  });
}
//@ts-expect-error
window.edit = edit;
async function save(e, tds) {
  console.log(tds);
  const tar = e.target;
  const body = new FormData();
  e.preventDefault();
  let i = 0;
  tds.forEach((el) => {
    if (el.tagName === "TD") {
      const v = el.firstChild.value;

      if (i === 3)
        el.innerHTML = v;
      else
        el.innerText = v;

      switch (i) {
        case 0:
          body.append("name", v);
          break;
        case 1:
          body.append("thumbnail", v);
          break;
        case 2:
          body.append("author", v);
          break;
        case 3:
          body.append("magazine", v);
          break;
      }
      i++;
    }
    else if (el === tar) {
      const iel = el;
      iel.value = "Remove";
      iel.onclick = null;
    }
    else {
      const iel = el;
      if (iel.type === "hidden") {
        body.append(iel.name, iel.value);
      }
      else {
        iel.style.display = "";
      }
    }
  });
  console.log(await (await fetch("api/add.php", {
    method: "POST",
    headers: {
      // "Content-Type": "multipart/form-data",
    },
    body
  })).text());
}
//# sourceMappingURL=main.js.map