-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
170 lines (151 loc) · 4.53 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { useState, useEffect } = React;
const PRICE_PER_DAY = 50; // Rs. 50 per presence
const App = () => {
const [presentDates, setPresentDates] = useState(() => {
const saved = localStorage.getItem("milkVendorAttendance");
return saved ? new Set(JSON.parse(saved)) : new Set();
});
const [currentMonth, setCurrentMonth] = useState(new Date());
useEffect(() => {
localStorage.setItem(
"milkVendorAttendance",
JSON.stringify([...presentDates])
);
}, [presentDates]);
const getDaysInMonth = (date) => {
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
};
const formatDate = (year, month, day) => {
return `${year}-${String(month + 1).padStart(2, "0")}-${String(
day
).padStart(2, "0")}`;
};
const togglePresence = (dateStr) => {
const newPresentDates = new Set(presentDates);
if (presentDates.has(dateStr)) {
newPresentDates.delete(dateStr);
} else {
newPresentDates.add(dateStr);
}
setPresentDates(newPresentDates);
};
const changeMonth = (offset) => {
const newMonth = new Date(currentMonth);
newMonth.setMonth(newMonth.getMonth() + offset);
setCurrentMonth(newMonth);
};
const getMonthlyStats = () => {
const year = currentMonth.getFullYear();
const month = currentMonth.getMonth();
const daysInMonth = getDaysInMonth(currentMonth);
let presentCount = 0;
for (let day = 1; day <= daysInMonth; day++) {
const dateStr = formatDate(year, month, day);
if (presentDates.has(dateStr)) {
presentCount++;
}
}
return {
present: presentCount,
totalBill: presentCount * PRICE_PER_DAY,
percentage: ((presentCount / daysInMonth) * 100).toFixed(1),
};
};
const renderCalendar = () => {
const year = currentMonth.getFullYear();
const month = currentMonth.getMonth();
const daysInMonth = getDaysInMonth(currentMonth);
const firstDay = new Date(year, month, 1).getDay();
const today = new Date().toISOString().split("T")[0];
const days = [];
// Empty cells for days before the first of the month
for (let i = 0; i < firstDay; i++) {
days.push(React.createElement("td", { key: `empty-${i}` }));
}
// Days of the month
for (let day = 1; day <= daysInMonth; day++) {
const dateStr = formatDate(year, month, day);
const isPresent = presentDates.has(dateStr);
const isToday = today === dateStr;
days.push(
React.createElement(
"td",
{ key: dateStr },
React.createElement(
"button",
{
onClick: () => togglePresence(dateStr),
className: `date-button ${isPresent ? "present" : ""} ${
isToday ? "today" : ""
}`,
},
day
)
)
);
}
// Split days into weeks
const weeks = [];
for (let i = 0; i < days.length; i += 7) {
weeks.push(React.createElement("tr", { key: i }, days.slice(i, i + 7)));
}
return weeks;
};
const stats = getMonthlyStats();
return React.createElement(
"div",
{ className: "calendar-container" },
React.createElement(
"div",
{ className: "calendar-header" },
React.createElement(
"button",
{
className: "calendar-nav",
onClick: () => changeMonth(-1),
},
"←"
),
React.createElement(
"div",
{ className: "calendar-title" },
currentMonth.toLocaleString("default", {
month: "long",
year: "numeric",
})
),
React.createElement(
"button",
{
className: "calendar-nav",
onClick: () => changeMonth(1),
},
"→"
)
),
React.createElement(
"table",
{ className: "calendar-table" },
React.createElement(
"thead",
null,
React.createElement(
"tr",
null,
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].map((day) =>
React.createElement("th", { key: day }, day)
)
)
),
React.createElement("tbody", null, renderCalendar())
),
React.createElement(
"div",
{ className: "stats" },
React.createElement("span", null, `Present: ${stats.present} days`),
React.createElement("span", null, `Bill: ₹${stats.totalBill}`),
React.createElement("span", null, `${stats.percentage}% attended`)
)
);
};
ReactDOM.render(React.createElement(App), document.getElementById("root"));