File size: 4,335 Bytes
26fca56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
document.getElementById('event-form').onsubmit = async (e) => {
  e.preventDefault();
  const data = {
    title: document.getElementById('title').value,
    description: document.getElementById('description').value,
    start_time: document.getElementById('start_time').value,
    end_time: document.getElementById('end_time').value,
  };
  await fetch('/events', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
  });
  location.reload();
};

document.querySelectorAll('.delete-btn').forEach(btn => {
  btn.onclick = async () => {
    const id = btn.parentElement.dataset.id;
    await fetch(`/events/${id}`, { method: 'DELETE' });
    location.reload();
  };
});

let editingId = null;
document.querySelectorAll('.edit-btn').forEach(btn => {
  btn.onclick = () => {
    const card = btn.parentElement;
    editingId = card.dataset.id;
    document.getElementById('edit-title').value = card.querySelector('h3').innerText;
    document.getElementById('edit-description').value = card.querySelectorAll('p')[0].innerText;

    const times = card.querySelectorAll('p')[1].innerText.replace("πŸ•’ ", "").split(" - ");
    const parseToInputFormat = (timeStr) => {
      const date = new Date(timeStr);
      const pad = n => n.toString().padStart(2, '0');
      return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}`;
    };

    document.getElementById('edit-start').value = parseToInputFormat(times[0]);
    document.getElementById('edit-end').value = parseToInputFormat(times[1]);
    document.getElementById('modal').style.display = 'flex';
  };
});

document.getElementById('save-edit').onclick = async () => {
  const data = {
    title: document.getElementById('edit-title').value,
    description: document.getElementById('edit-description').value,
    start_time: document.getElementById('edit-start').value,
    end_time: document.getElementById('edit-end').value,
  };
  await fetch(`/events/${editingId}`, {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(data)
  });
  location.reload();
};

function closeModal() {
  document.getElementById('modal').style.display = 'none';
}

window.onclick = (event) => {
  const modal = document.getElementById('modal');
  if (event.target === modal) {
    closeModal();
  }
};

document.getElementById('clear-search').onclick = () => {
  window.location.href = '/';
};

document.getElementById('search-btn').onclick = () => {
  const query = document.getElementById('search').value.trim();
  if (query.length > 0) {
    window.location.href = `/?search=${encodeURIComponent(query)}`;
  }
};

window.onload = () => {
  const now = new Date();
  const pad = n => n.toString().padStart(2, '0');

  const toDatetimeLocal = dt => {
    return `${dt.getFullYear()}-${pad(dt.getMonth() + 1)}-${pad(dt.getDate())}T${pad(dt.getHours())}:${pad(dt.getMinutes())}`;
  };

  document.getElementById('start_time').value = toDatetimeLocal(now);

  now.setHours(now.getHours() + 4);
  document.getElementById('end_time').value = toDatetimeLocal(now);

  // Convert event display times to 12-hour format
  document.querySelectorAll(".event-card").forEach(card => {
    const timePara = card.querySelectorAll("p")[1];
    if (timePara) {
      const [startRaw, endRaw] = timePara.innerText.replace("πŸ•’ ", "").split(" - ");
      const start = new Date(startRaw);
      const end = new Date(endRaw);

      const format12 = dt => dt.toLocaleString(undefined, {
        year: "numeric", month: "short", day: "numeric",
        hour: "numeric", minute: "2-digit", hour12: true
      });

      timePara.innerText = `πŸ•’ ${format12(start)} - ${format12(end)}`;
    }
  });
};


// script.js
const themeToggle = document.getElementById("theme-toggle");

themeToggle.addEventListener("change", function() {
  const currentTheme = document.documentElement.getAttribute("data-theme");

  // Toggle between light and dark themes
  if (this.checked) {
    document.documentElement.setAttribute("data-theme", "dark");
  } else {
    document.documentElement.setAttribute("data-theme", "light");
  }
});