-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
139 lines (128 loc) · 5.01 KB
/
script.js
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
129
130
131
132
133
134
135
136
137
138
document.addEventListener('DOMContentLoaded', () => {
let timerInterval;
let endTime;
const apiKey = '312d2a72-a74d-4c05-911b-10a68db11703';
const timerDisplay = document.getElementById('timer');
const startButton = document.getElementById('start-button');
const endButton = document.getElementById('end-button');
const slackIdInput = document.getElementById('slack-id');
const connectSlackButton = document.getElementById('connect-slack');
const darkModeButton = document.getElementById('dark-mode');
const lightModeButton = document.getElementById('light-mode');
const sessionHistory = document.getElementById('session-history');
const tabLinks = document.querySelectorAll('.tab-link');
const tabContents = document.querySelectorAll('.tab-content');
const updateTimerDisplay = () => {
const now = new Date().getTime();
const distance = endTime - now;
const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((distance % (1000 * 60)) / 1000);
timerDisplay.textContent = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
if (distance < 0) {
clearInterval(timerInterval);
timerDisplay.textContent = "00:00";
}
};
startButton.addEventListener('click', async () => {
const slackId = slackIdInput.value;
const work = document.getElementById('coding-field').value;
if (!slackId || !work) {
alert('Please enter your Slack ID and what you are coding.');
return;
}
try {
const response = await fetch('/api/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({ work })
});
const data = await response.json();
if (data.ok) {
const sessionResponse = await fetch(`/api/session/${slackId}`);
const sessionData = await sessionResponse.json();
if (sessionData.ok) {
endTime = new Date(sessionData.data.endTime).getTime();
timerInterval = setInterval(updateTimerDisplay, 1000);
} else {
alert('Failed to retrieve session data.');
}
} else {
alert('Failed to start session.');
}
} catch (error) {
alert('Error starting session: ' + error.message);
}
});
endButton.addEventListener('click', async () => {
const slackId = slackIdInput.value;
if (!slackId) {
alert('Please enter your Slack ID.');
return;
}
try {
const response = await fetch('/api/cancel', {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
const data = await response.json();
if (data.ok) {
clearInterval(timerInterval);
timerDisplay.textContent = "60:00";
} else {
alert('Failed to end session.');
}
} catch (error) {
alert('Error ending session: ' + error.message);
}
});
connectSlackButton.addEventListener('click', () => {
const slackId = slackIdInput.value;
if (slackId) {
alert('Successfully connected to Slack account.');
} else {
alert('Please enter your Slack ID.');
}
});
darkModeButton.addEventListener('click', () => {
document.body.classList.add('bg-gray-900', 'text-white');
document.body.classList.remove('bg-white', 'text-gray-800');
});
lightModeButton.addEventListener('click', () => {
document.body.classList.add('bg-white', 'text-gray-800');
document.body.classList.remove('bg-gray-900', 'text-white');
});
const loadSessionHistory = async () => {
const slackId = slackIdInput.value;
if (!slackId) {
return;
}
try {
const response = await fetch(`/api/history/${slackId}`);
const data = await response.json();
if (data.ok) {
sessionHistory.innerHTML = data.data.map(session => `
<li>${session.createdAt}: ${session.work} - ${session.time} minutes</li>
`).join('');
}
} catch (error) {
alert('Error loading session history: ' + error.message);
}
};
loadSessionHistory();
tabLinks.forEach(link => {
link.addEventListener('click', () => {
const target = link.getAttribute('data-target');
tabContents.forEach(content => {
content.classList.add('hidden');
if (content.id === target) {
content.classList.remove('hidden');
}
});
});
});
});