Skip to content

Commit 010bb6d

Browse files
committed
new feature: now the browser back button works.
Fix #79
1 parent dd334f9 commit 010bb6d

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

static/js/home.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ function rerender_contacts_view() {
187187
function onClickContact() {
188188
// on click of a contact, set the selectedContactId and rerender the message view
189189
const contactId = $(this).attr('id')
190+
// change the URL to reflect the selected contact
191+
window.history.pushState(null, '', `/chat/${contactId}/`)
190192
app_states.setSelectedContactId(contactId)
191193
render_msg_view()
192194
}
@@ -354,6 +356,11 @@ $(function () {
354356
console.log(app_states.sessionId)
355357
console.log(app_states.userEmail)
356358

359+
// if the url is like root/chat/4, then set the selectedContactId to 4
360+
const match = window.location.pathname.match(/\/chat\/(\d+)\//)
361+
const contactId = match ? parseInt(match[1]).toString() : -1
362+
app_states.setSelectedContactId(contactId)
363+
357364
const username = app_states.userEmail.split('@')[0]
358365
$('#username').text(username)
359366

zserver/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
path("api/sign-up-otp/", views.VerifyUserOTPView.as_view(), name="sign-up-otp"),
2222

2323
path("google-login/", views.GoogleLoginView.as_view(), name="google_login"),
24+
25+
path("chat/<int:contact_id>/", views.Chat.as_view()),
2426
]

zserver/views/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
from zserver.views.chat import * # noqa: F403
12
from zserver.views.message import * # noqa: F403
23
from zserver.views.user_profile import * # noqa: F403

zserver/views/chat.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from urllib.request import Request
2+
3+
from django.shortcuts import render
4+
from django.views import View
5+
6+
from zserver.utils import get_env_var
7+
8+
9+
class Chat(View):
10+
11+
def get(self, request: Request, contact_id: int):
12+
"""Render the home page."""
13+
context = {
14+
"name" : "John Doe",
15+
"env_var" : get_env_var(),
16+
contact_id: contact_id,
17+
}
18+
return render(request, "home.html", context)

0 commit comments

Comments
 (0)