summaryrefslogtreecommitdiffstats
path: root/src/main.ts
blob: c4b616b4055edc80412fc7b5e562142f2807233f (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
import './style.css'

function edit(e: PointerEvent) {
  e.preventDefault();
  const tar = e.target as HTMLInputElement;
  const form = tar.parentNode as HTMLFormElement
  form.onseeked
  const id = tar.dataset.id;
  const tds = document.querySelectorAll(`[data-id="${id}"]`) as NodeListOf<HTMLElement>
  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 as HTMLInputElement)
      if (iel.type !== "hidden") {
        iel.value = "Save"
        el.onclick = e => save(e, tds);
      }
    }
  })
}
//@ts-expect-error
window.edit = edit;

async function save(e: MouseEvent, tds: NodeListOf<HTMLElement>) {
  console.log(tds)
  const tar = e.target as HTMLInputElement
  const body = new FormData();
  e.preventDefault();
  let i = 0;
  tds.forEach((el) => {
    if (el.tagName === "TD") {
      const v = (el.firstChild as HTMLInputElement).value;
      el.innerHTML = 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 as HTMLInputElement)
      iel.value = "Remove"
      iel.onclick = null;
    } else {
      const iel = (el as HTMLInputElement)
      if (iel.type === "hidden") {
        body.append(iel.name, iel.value);
      } else {
        iel.style.display = ""
      }
    }
  })
  await fetch("api/add.php", {
    method: "POST",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    body
  });
}