Skip to content

Commit eb6ea8e

Browse files
committed
Group message by Date or Weekdays (if last 7 days message)
it will show on top of each message per date or weekday
1 parent e462c15 commit eb6ea8e

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

chat/static/chat/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,14 @@ svg {
581581
font-size: .93rem;
582582
}
583583

584+
.date_weekday {
585+
display: grid;
586+
justify-self: center;
587+
padding: 5px 12px 6px;
588+
font-size: 0.78rem;
589+
background-color: #e1f3faff;
590+
border-radius: 7.5px;
591+
width: fit-content;
592+
box-shadow: 0 1px .5px rgba(0,0,0,.13);
593+
line-height: 21px;
594+
}

chat/templates/chat/room.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<script>
8585

8686
const roomId = '{{chatgroup.id}}'
87+
var tempDaysWeekdays = [];
8788

8889
const chatSocket = new WebSocket(
8990
`ws://${window.location.host}/ws/chat/${roomId}/`
@@ -181,6 +182,12 @@
181182
</div>
182183
</div>`
183184
}
185+
186+
if (msg_time) {
187+
showDatesWeekDays(msg_time)
188+
} else {
189+
showDatesWeekDays(new Date())
190+
}
184191

185192
newDiv.innerHTML = msg1;
186193

@@ -240,6 +247,45 @@
240247
}));
241248
}
242249

250+
251+
function showDatesWeekDays(date_created) {
252+
// add the newly created element and its content into the DOM
253+
254+
dt = new Date(date_created)
255+
256+
if (!tempDaysWeekdays.includes(dt.toLocaleDateString())) {
257+
let newDiv = document.createElement("div");
258+
let currentDiv = document.getElementById("new-message-chat");
259+
let parentDiv = currentDiv.parentNode;
260+
let days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
261+
262+
if (dt.toDateString() == new Date().toDateString()) {
263+
// display TODAY in message
264+
date_weekday = 'TODAY';
265+
} else if(dt > getDateBefore()) {
266+
// display week day in message
267+
date_weekday = days[dt.getDay()].toUpperCase()
268+
} else {
269+
// display date in message
270+
date_weekday = dt.toLocaleDateString();
271+
}
272+
273+
newDiv.style.display = "grid";
274+
newDiv.innerHTML = `<div class="date_weekday">${date_weekday}</div>`
275+
parentDiv.insertBefore(newDiv, currentDiv);
276+
277+
tempDaysWeekdays.push(dt.toLocaleDateString())
278+
}
279+
280+
}
281+
282+
function getDateBefore(days=7) {
283+
// calculate the last 7 days date
284+
// 7 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds ) = 604800000 or 7 days in milliseconds.
285+
daysInMs= days * 24 * 60 * 60 * 1000
286+
return new Date(Date.now() - daysInMs)
287+
}
288+
243289
function loadMessage() {
244290
fetch("{% url 'chat:history' chatgroup.id %}")
245291
.then( response => response.json() )

chat/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def unauthorized(request):
5656
async def history(request, room_id):
5757

5858
await Tortoise.init(**settings.TORTOISE_INIT)
59-
chat_message = await ChatMessage.filter(room_id=room_id).values()
59+
chat_message = await ChatMessage.filter(room_id=room_id).order_by('date_created').values()
6060
await Tortoise.close_connections()
6161

6262
return await sync_to_async(JsonResponse)(chat_message, safe=False)

0 commit comments

Comments
 (0)