index.ts 3.2 KB
Newer Older
Benjamin Krause's avatar
Benjamin Krause committed
1
2
3
4
5
6
"use strict";

import "./style.scss";
import axios from "axios";

let userData: {
Benjamin Krause's avatar
Benjamin Krause committed
7
8
9
  mail: string;
  password: string;
} = { mail: "", password: "" };
Benjamin Krause's avatar
Benjamin Krause committed
10
11
12
13
14
15
16
17
18

export namespace EmailProxy {
  const protocol = "http://";
  const ip = "localhost";
  const port = "8000";
  const baseUrl = `${protocol}${ip}:${port}/`;

  export async function getMail(obj: {
    user: {
Benjamin Krause's avatar
Benjamin Krause committed
19
20
21
22
      mail: string;
      password: string;
    };
    mailbox: string;
Benjamin Krause's avatar
Benjamin Krause committed
23
24
25
26
27
28
29
30
31
32
33
  }) {
    const url = `${baseUrl}mails/get`;
    let response = await axios
      .post(url, obj)
      .then((response) => {
        return response;
      })
      .catch((reason) => {
        return reason.response;
      });
    return response;
Benjamin Krause's avatar
Benjamin Krause committed
34
  }
Benjamin Krause's avatar
Benjamin Krause committed
35
36
37

  export async function sendMail(obj: {
    from_user: {
Benjamin Krause's avatar
Benjamin Krause committed
38
39
40
      mail: string;
      password: string;
    };
Benjamin Krause's avatar
Benjamin Krause committed
41
    to_user: {
Benjamin Krause's avatar
Benjamin Krause committed
42
43
44
45
46
      mail: string;
      password: string;
    };
    subject: string;
    message: string;
Benjamin Krause's avatar
Benjamin Krause committed
47
48
49
  }) {
    const url = `${baseUrl}mails/send`;
    let response = await axios
Benjamin Krause's avatar
Benjamin Krause committed
50
51
52
53
54
55
56
57
      .post(url, obj)
      .then((response) => {
        return response;
      })
      .catch((reason) => {
        return reason.response;
      });
    return response;
Benjamin Krause's avatar
Benjamin Krause committed
58
59
60
61
  }
}

function removeChilds(parent: HTMLElement) {
Benjamin Krause's avatar
Benjamin Krause committed
62
63
  while (parent.firstChild) {
    parent.firstChild.remove();
Benjamin Krause's avatar
Benjamin Krause committed
64
65
66
67
68
69
70
  }
}

async function fillChat() {
  const resp = await EmailProxy.getMail({
    user: {
      mail: userData.mail,
Benjamin Krause's avatar
Benjamin Krause committed
71
      password: userData.password,
Benjamin Krause's avatar
Benjamin Krause committed
72
    },
Benjamin Krause's avatar
Benjamin Krause committed
73
74
    mailbox: "Spam",
  });
Benjamin Krause's avatar
Benjamin Krause committed
75
  const data = resp.data;
Benjamin Krause's avatar
Benjamin Krause committed
76
77
78
  const parent: HTMLElement = document.querySelector("#chat-content");
  removeChilds(parent);
  data.forEach((element) => {
Benjamin Krause's avatar
Benjamin Krause committed
79
    const text = document.createElement("li");
Benjamin Krause's avatar
Benjamin Krause committed
80
81
82
    const date = new Date(element.date);
    const dateString = `${date.getDate()}.${date.getMonth()}.${date.getFullYear()} ${date.getHours()}:${date.getMinutes()}`
    text.innerText = `${element.mail_from} - ${dateString} - ${element.body}`;
Benjamin Krause's avatar
Benjamin Krause committed
83
84
85
86
87
    parent.appendChild(text);
  });
}

async function work(obj: any) {
Benjamin Krause's avatar
Benjamin Krause committed
88
  const resp = await EmailProxy.sendMail(obj);
Benjamin Krause's avatar
Benjamin Krause committed
89
90
91
92
  const data = resp.data;
  console.log(data);
}

Benjamin Krause's avatar
Benjamin Krause committed
93
94
95
96
97
98
99
100
101
102
function setNotifierStatus(status: boolean) {
  document.querySelectorAll(".notifier").forEach((element) => {
    if (status) {
      element.classList.add("green");
    } else {
      element.classList.remove("green");
    }
  });
}

Benjamin Krause's avatar
Benjamin Krause committed
103
104
105
function init() {
  document.querySelector("#login-btn").addEventListener("click", () => {
    userData.mail = (document.querySelector("#mail") as HTMLInputElement).value;
Benjamin Krause's avatar
Benjamin Krause committed
106
107
108
109
110
    userData.password = (document.querySelector(
      "#password"
    ) as HTMLInputElement).value;
    (document.querySelector("#login-btn") as HTMLButtonElement).disabled = true;
    fillChat();
Benjamin Krause's avatar
Benjamin Krause committed
111
112
    setInterval(() => {
      fillChat();
Benjamin Krause's avatar
Benjamin Krause committed
113
114
115
116
117
118
      setNotifierStatus(true);
      setTimeout(() => {
        setNotifierStatus(false);
      }, 500);
    }, 5 * 1000);
  });
Benjamin Krause's avatar
Benjamin Krause committed
119

Benjamin Krause's avatar
Benjamin Krause committed
120
  document.querySelector("#send").addEventListener("click", () => {
Benjamin Krause's avatar
Benjamin Krause committed
121
122
123
    const obj = {
      from_user: {
        mail: userData.mail,
Benjamin Krause's avatar
Benjamin Krause committed
124
        password: userData.password,
Benjamin Krause's avatar
Benjamin Krause committed
125
126
      },
      to_user: {
Benjamin Krause's avatar
Benjamin Krause committed
127
128
        mail: (document.querySelector("#to") as HTMLInputElement).value,
        password: "",
Benjamin Krause's avatar
Benjamin Krause committed
129
130
      },
      subject: "Test",
Benjamin Krause's avatar
Benjamin Krause committed
131
132
133
      message: (document.querySelector("#text") as HTMLInputElement).value,
    };
    work(obj);
Benjamin Krause's avatar
Benjamin Krause committed
134
135
  });
}
Benjamin Krause's avatar
Benjamin Krause committed
136
137

init();