Skip to content

Commit

Permalink
feat(common): add online status information
Browse files Browse the repository at this point in the history
This adds an "Online" status when a user is online, and updates to "Watching" when watching media.
  • Loading branch information
trueChazza committed Jan 31, 2022
1 parent b4fdf0b commit 552f8ab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
19 changes: 17 additions & 2 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@ let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("
topbar.config({barColors: {0: "#6366f1"}, shadowColor: "rgba(0, 0, 0, .3)"})

window.addEventListener("phx:page-loading-start", info => topbar.show())
window.addEventListener("phx:page-loading-stop", info => topbar.hide())
window.addEventListener("phx:page-loading-stop", info => {
if (info.detail.kind === 'initial' && window.location.toString().includes("/watch")) {
channel.push('shout', { user_id: window.userId, page_title: document.title })
}

topbar.hide()
})

channel.on("shout", message => {
const element = document.querySelector(`#current-location-${ message.user_id }`)

if(element) {
element.innerHTML = `Watching ${ message.page_title }`
}
})

presence.onSync(() => {
let presences = []
Expand Down Expand Up @@ -43,7 +57,8 @@ presence.onSync(() => {
<span class="bg-green-400 absolute top-0 right-0 block h-2.5 w-2.5 rounded-full ring-2 ring-white" aria-hidden="true"></span>
</span>
<div class="ml-4 truncate">
<p class="text-sm text-gray-900 truncate">${ item.user_name }</p>
<p class="text-sm text-gray-900 truncate">${ item.user_name }</p>
<p id="current-location-${ item.user_id }" class="text-sm text-gray-500 truncate">Online</p>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/media_server_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
window.userName = "<%= assigns[:current_user].name %>";
</script>
<script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=9")}></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=10")}></script>
<% end %>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion lib/media_server_web/templates/watch/watch.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
window.userEmail = "<%= assigns[:current_user].email %>";
window.userName = "<%= assigns[:current_user].name %>";
</script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=9")}></script>
<script defer phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js?id=10")}></script>
<% end %>
</head>
<body>
Expand Down

0 comments on commit 552f8ab

Please sign in to comment.