Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated app.js instructions in presence.md #263 #263

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 8 additions & 25 deletions presence.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,28 +321,28 @@ const peopleListDesktop = document.getElementById('people_online-list-desktop');
// This function will be probably caught when the person first enters the page
channel.on('presence_state', function (payload) {
// Array of objects with id and name
const currentlyOnlinepeople = Object.entries(payload).map(elem => ({name: elem[0], id: elem[1].metas[0].phx_ref}))
const currentlyOnlinePeople = Object.entries(payload).map(elem => ({name: elem[0], id: elem[1].metas[0].phx_ref}))

updateOnlinePeopleList(currentlyOnlinepeople)
updateOnlinePeopleList(currentlyOnlinePeople)
})

// Listening to presence events whenever a person leaves or joins
channel.on('presence_diff', function (payload) {
if(payload.joins && payload.leaves) {
// Array of objects with id and name
const currentlyOnlinepeople = Object.entries(payload.joins).map(elem => ({name: elem[0], id: elem[1].metas[0].phx_ref}))
const currentlyOnlinePeople = Object.entries(payload.joins).map(elem => ({name: elem[0], id: elem[1].metas[0].phx_ref}))
const peopleThatLeft = Object.entries(payload.leaves).map(elem => ({name: elem[0], id: elem[1].metas[0].phx_ref}))

updateOnlinePeopleList(currentlyOnlinepeople)
updateOnlinePeopleList(currentlyOnlinePeople)
removePeopleThatLeft(peopleThatLeft)
}
});

function updateOnlinePeopleList(currentlyOnlinepeople) {
function updateOnlinePeopleList(currentlyOnlinePeople) {
// Add joined people
for (var i = currentlyOnlinepeople.length - 1; i >= 0; i--) {
const name = currentlyOnlinepeople[i].name
const id = name + "-" + currentlyOnlinepeople[i].id
for (var i = currentlyOnlinePeople.length - 1; i >= 0; i--) {
const name = currentlyOnlinePeople[i].name
const id = name + "-" + currentlyOnlinePeople[i].id

if (document.getElementById(name) == null) {
var liMobile = document.createElement("li"); // create new person list item DOM element for mobile
Expand All @@ -368,30 +368,13 @@ function removePeopleThatLeft(peopleThatLeft) {
const personThatLeftMobile = document.getElementById(id + '_mobile')
const personThatLeftDesktop = document.getElementById(id + '_desktop')



if (personThatLeftMobile != null && personThatLeftDesktop != null) {
peopleListMobile.removeChild(personThatLeftMobile); // remove the person from list mobile
peopleListDesktop.removeChild(personThatLeftDesktop); // remove the person from list desktop
}
}
}

function removePeopleThatLeft(peopleThatLeft) {
// Remove people that left
for (var i = peopleThatLeft.length - 1; i >= 0; i--) {
const name = peopleThatLeft[i]

const personThatLeftMobile = document.getElementById(name + '_mobile')
const personThatLeftDesktop = document.getElementById(name + '_desktop')
if (personThatLeftMobile != null && personThatLeftDesktop != null) {
peopleListMobile.removeChild(personThatLeftMobile); // remove the person from list mobile
peopleListDesktop.removeChild(personThatLeftDesktop); // remove the person from list desktop
}
}
}


function sanitizeString(str){
str = str.replace(/[^a-z0-9áéíóúñü \.,_-]/gim,"");
return str.trim();
Expand Down
Loading