Reooiq commited on
Commit
a04ea7a
·
verified ·
1 Parent(s): 654b019

Update public/app.js

Browse files
Files changed (1) hide show
  1. public/app.js +58 -12
public/app.js CHANGED
@@ -1,20 +1,66 @@
 
 
 
 
 
 
 
 
1
 
2
- function add(){
3
- fetch("/add", {
4
- method:"POST",
5
- headers:{"Content-Type":"application/json"},
6
- body: JSON.stringify({text: document.getElementById("msg").value})
7
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  }
9
 
10
- function start(){
11
- fetch("/start",{method:"POST"});
 
 
 
 
 
 
 
 
12
  }
13
 
14
- function stop(){
15
- fetch("/stop",{method:"POST"});
 
 
 
 
 
 
 
 
16
  }
17
 
18
- function emergency(){
19
- fetch("/emergency",{method:"POST"});
 
 
 
 
 
 
 
 
20
  }
 
1
+ async function add() {
2
+ const msgInput = document.getElementById("msg");
3
+ const text = msgInput.value.trim();
4
+
5
+ if (!text) {
6
+ alert("الرجاء إدخال نص قبل الإرسال");
7
+ return;
8
+ }
9
 
10
+ try {
11
+ const response = await fetch("/add", {
12
+ method: "POST",
13
+ headers: {"Content-Type": "application/json"},
14
+ body: JSON.stringify({text: text})
15
+ });
16
+
17
+ if (!response.ok) {
18
+ throw new Error(`خطأ في الخادم: ${response.status}`);
19
+ }
20
+
21
+ const data = await response.json();
22
+ console.log("تم الإضافة بنجاح:", data);
23
+ msgInput.value = ""; // إفراغ الحقل بعد النجاح
24
+ alert("تم الإرسال بنجاح!");
25
+
26
+ } catch (error) {
27
+ console.error("فشل الإرسال:", error);
28
+ alert("حدث خطأ أثناء الإرسال: " + error.message);
29
+ }
30
  }
31
 
32
+ async function start() {
33
+ try {
34
+ const response = await fetch("/start", {method: "POST"});
35
+ if (!response.ok) throw new Error(`خطأ: ${response.status}`);
36
+ console.log("تم التشغيل");
37
+ alert("تم التشغيل بنجاح!");
38
+ } catch (error) {
39
+ console.error("فشل التشغيل:", error);
40
+ alert("فشل التشغيل: " + error.message);
41
+ }
42
  }
43
 
44
+ async function stop() {
45
+ try {
46
+ const response = await fetch("/stop", {method: "POST"});
47
+ if (!response.ok) throw new Error(`خطأ: ${response.status}`);
48
+ console.log("تم الإيقاف");
49
+ alert("تم الإيقاف بنجاح!");
50
+ } catch (error) {
51
+ console.error("فشل الإيقاف:", error);
52
+ alert("فشل الإيقاف: " + error.message);
53
+ }
54
  }
55
 
56
+ async function emergency() {
57
+ try {
58
+ const response = await fetch("/emergency", {method: "POST"});
59
+ if (!response.ok) throw new Error(`خطأ: ${response.status}`);
60
+ console.log("تم تفعيل الطوارئ");
61
+ alert("تم تفعيل وضع الطوارئ!");
62
+ } catch (error) {
63
+ console.error("فشل الطوارئ:", error);
64
+ alert("فشل تفعيل الطوارئ: " + error.message);
65
+ }
66
  }