Skip to content

Commit

Permalink
fix: empty names with emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovan Ilić committed Mar 7, 2024
1 parent e69aef0 commit 805b1de
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 109 deletions.
14 changes: 10 additions & 4 deletions public/admin/santaAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $(async () => {
const userElement = $.parseHTML(userTemplate);
$(userElement).find('[data-name="userIndex"]').text(++index);
$(userElement).find('[data-name="userName"]').text(userData.name);
$(userElement).find('a').attr('href', `/friends/${userData._id}`);
$(userElement).find('a').attr('href', `/profile?id=${userData._id}`);
$(userElement).find('[data-name="userId"]').val(userData._id);
$(userElement).find('[data-name="userEmail"]').text(userData.email);
$(userElement).find('[data-name="userRole"]').val(userData.groups.role);
Expand Down Expand Up @@ -97,8 +97,12 @@ $(async () => {
result.forEach((pair, index) => {
const pairElement = $.parseHTML(pairTemplate);
$(pairElement).find('[data-name="pairIndex"]').text(++index);
$(pairElement).find('[data-name="pairUser"]').text(pair.user);
$(pairElement).find('[data-name="pairForbiddenPair"]').text(pair.forbiddenPair);
let santaName = pair.user;
if (pair.user === undefined || pair.user === '') { santaName = pair.userEmail; }
$(pairElement).find('[data-name="pairUser"]').text(santaName);
let childName = pair.forbiddenPair;
if (pair.forbiddenPair === undefined || pair.forbiddenPair === '') { childName = pair.forbiddenPairEmail; }
$(pairElement).find('[data-name="pairForbiddenPair"]').text(childName);

$(pairElement).find('[data-name="pairDelete"]').on('click', () => {
$('#removeUserDialog').prop('hidden', true);
Expand All @@ -114,7 +118,9 @@ $(async () => {
// fill up the forbiddenPair modal select elements with usernames
$.getJSON('/friends/api/list', function(result) {
result.forEach(function(friend) {
$('#forbiddenUser1, #forbiddenUser2').append(`<option value="${friend._id}" data-email="${friend.email}">${friend.name}</option>`);
let name = friend.email;
if (friend.name !== undefined && friend.name !== '') { name = friend.name; }
$('#forbiddenUser1, #forbiddenUser2').append(`<option value="${friend._id}" data-email="${friend.email}">${name}</option>`);
});
});
$('#forbiddenPairsForm').on('submit', () => {
Expand Down
130 changes: 68 additions & 62 deletions public/chat/santaChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,84 +8,90 @@ $(async function() {
let chatEl;

await $.getScript('/santa.js');
pageLoaded.then(async () => {
const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];

const months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];

$.getJSON(`${friendsApiUrl}/list`, function(result) {
result.forEach(function(friend) {
$('#user').append(`<option value="${friend._id}" data-email="${friend.email}">${friend.name}</option>`);
$.getJSON(`${friendsApiUrl}/list`, function(result) {
result.forEach(function(friend) {
let name = friend.email;
if (friend.name !== undefined && friend.name !== '') { name = friend.name; }
$('#user').append(`<option value="${friend._id}" data-email="${friend.email}">${name}</option>`);
});
});
});

$.getJSON(`${apiUrl}/list`, chat => {
$.get('chat/message.html', chatTemplate => {
for (const item of chat) {
const date = new Date(item.timestamp);

let hours = date.getHours();
hours = hours < 10 ? '0' + hours : hours;
let minutes = date.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
const dateStr = `${hours}:${minutes} - ${date.getDate()}. ${months[date.getMonth()]} ${date.getFullYear()}`;

const chatElement = $.parseHTML(chatTemplate);
$(chatElement).find('[data-name="chatTo"]').text(`To: ${item.name}`);
$(chatElement).find('[data-name="chatMessage"]').text(item.message);
$(chatElement).find('[data-name="chatDate"]').text(dateStr);
$(chatElement).find('[data-name="chatFrom"]').text(`From: ${item.from || 'Anonymous'}`);

$(chatElement).find('[data-name="msgDelete"]').on('click', function() {
chatId = item._id;
chatEl = chatElement;
});
$('#chat').append(chatElement);
$.getJSON(`${apiUrl}/list`, chat => {
if (chat.length === 0) {
showAlert({ warning: 'No chat activity' });
}
$.get('chat/message.html', chatTemplate => {
for (const item of chat) {
const date = new Date(item.timestamp);

$('#chat').scrollTop($('#chat').prop('scrollHeight'));
});
});
let hours = date.getHours();
hours = hours < 10 ? '0' + hours : hours;
let minutes = date.getMinutes();
minutes = minutes < 10 ? '0' + minutes : minutes;
const dateStr = `${hours}:${minutes} - ${date.getDate()}. ${months[date.getMonth()]} ${date.getFullYear()}`;

$('#chat-form').on('submit', function() {
const requestData = {
userId: $('#user option:selected').val(),
email: $('#user option:selected').attr('data-email'),
message: $('#message').val()
};
$.post(`${apiUrl}/send`, requestData, function(response) {
if (!response.error) {
$.get('chat/message.html', chatTemplate => {
const chatElement = $.parseHTML(chatTemplate);
$(chatElement).find('[data-name="chatTo"]').text(`To: ${$('#user option:selected').text()}`);
$(chatElement).find('[data-name="chatMessage"]').text(requestData.message);
$(chatElement).find('[data-name="chatDate"]').text('Just now...');
$(chatElement).find('[data-name="chatFrom"]').text('From: you');
$(chatElement).find('[data-name="chatTo"]').text(`To: ${item.name}`);
$(chatElement).find('[data-name="chatMessage"]').text(item.message);
$(chatElement).find('[data-name="chatDate"]').text(dateStr);
$(chatElement).find('[data-name="chatFrom"]').text(`From: ${item.from || 'Anonymous'}`);

$(chatElement).find('[data-name="msgDelete"]').on('click', function() {
chatId = response.insertedId;
chatId = item._id;
chatEl = chatElement;
});
$('#chat').append(chatElement);
$('#chat').scrollTop($('#chat').prop('scrollHeight'));
}

// Reset the form
$('#chat-form')[0].reset();
});
}
showAlert(response);
$('#chat').scrollTop($('#chat').prop('scrollHeight'));
});
});
return false;
});

$('.alert .btn-close').on('click', function() {
$('.alert').hide();
});
$('#chat-form').on('submit', function() {
const requestData = {
userId: $('#user option:selected').val(),
email: $('#user option:selected').attr('data-email'),
message: $('#message').val()
};
$.post(`${apiUrl}/send`, requestData, function(response) {
if (!response.error) {
$.get('chat/message.html', chatTemplate => {
const chatElement = $.parseHTML(chatTemplate);
$(chatElement).find('[data-name="chatTo"]').text(`To: ${$('#user option:selected').text()}`);
$(chatElement).find('[data-name="chatMessage"]').text(requestData.message);
$(chatElement).find('[data-name="chatDate"]').text('Just now...');
$(chatElement).find('[data-name="chatFrom"]').text('From: you');

$('#deleteMessageButton').on('click', () => {
$.post(`${apiUrl}/delete`, { _id: chatId }, result => {
showAlert(result);
if (result.success) {
$(chatEl).remove();
}
$(chatElement).find('[data-name="msgDelete"]').on('click', function() {
chatId = response.insertedId;
chatEl = chatElement;
});
$('#chat').append(chatElement);
$('#chat').scrollTop($('#chat').prop('scrollHeight'));

// Reset the form
$('#chat-form')[0].reset();
});
}
showAlert(response);
});
return false;
});

$('.alert .btn-close').on('click', function() {
$('.alert').hide();
});

$('#deleteMessageButton').on('click', () => {
$.post(`${apiUrl}/delete`, { _id: chatId }, result => {
showAlert(result);
if (result.success) {
$(chatEl).remove();
}
});
});
});
});
8 changes: 6 additions & 2 deletions public/history/year/santaYear.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ $(async () => {

function listGifts(gift) {
const giftElement = $.parseHTML(giftTemplate);
$(giftElement).find('#santa').text(gift.santa);
$(giftElement).find('#child').text(gift.child);
let santaName = gift.santa;
let childName = gift.child;
if (gift.santa === '') { santaName = gift.santaEmail; }
if (gift.child === '') { childName = gift.childEmail; }
$(giftElement).find('#santa').text(santaName);
$(giftElement).find('#child').text(childName);
if (gift.gift === null) {
gift.gift = 'N/A';
}
Expand Down
66 changes: 32 additions & 34 deletions public/profile/santaProfile.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,40 +45,38 @@

<!-- user data -->
<div class="col-md-8 col-xs-12">
<form>
<fieldset {{isDisabled}}>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
<label for="name" class="form-label">Name</label>
</div>
<div class="form-floating mb-3">
<textarea class="form-control" id="description" placeholder="Description"></textarea>
<label for="description" class="form-label">Description</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="street" name="street" placeholder="Street" required>
<label for="street" class="form-label">Street</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" id="postalCode" name="postalCode"
placeholder="Postal code" required>
<label for="postalCode" class="form-label">Postal code</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="city" name="city" placeholder="City" required>
<label for="city" class="form-label">City</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="state" name="state" placeholder="State" required>
<label for="state" class="form-label">State</label>
</div>
<div class="form-floating mb-3">
<input disabled type="email" class="form-control" id="email" placeholder="Name">
<label for="email" class="form-label">Email</label>
</div>
<button type="submit" class="btn btn-primary" {{isHidden}}>Save changes</button>
</fieldset>
</form>
<fieldset {{isDisabled}}>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
<label for="name" class="form-label">Name</label>
</div>
<div class="form-floating mb-3">
<textarea class="form-control" id="description" placeholder="Description"></textarea>
<label for="description" class="form-label">Description</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="street" name="street" placeholder="Street">
<label for="street" class="form-label">Street</label>
</div>
<div class="form-floating mb-3">
<input type="number" class="form-control" id="postalCode" name="postalCode"
placeholder="Postal code">
<label for="postalCode" class="form-label">Postal code</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="city" name="city" placeholder="City">
<label for="city" class="form-label">City</label>
</div>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="state" name="state" placeholder="State">
<label for="state" class="form-label">State</label>
</div>
<div class="form-floating mb-3">
<input disabled type="email" class="form-control" id="email" placeholder="Name">
<label for="email" class="form-label">Email</label>
</div>
<button id="profileSaveButton" class="btn btn-primary" {{isHidden}}>Save changes</button>
</fieldset>
</div>
<!-- end user data -->

Expand Down
12 changes: 9 additions & 3 deletions public/profile/santaProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ $(async () => {
}
});

$('form').on('submit', function() {
$('#profileSaveButton').on('click', function() {
const friend = {
name: $('#name').val(),
description: $('#description').val(),
Expand All @@ -51,8 +51,14 @@ $(async () => {
if (searchParams.has('id')) {
updateUrl += `?id=${searchParams.get('id')}`;
}
$.post(updateUrl, friend, result => {
showAlert(result);
$.ajax({
url: updateUrl,
type: 'POST',
data: JSON.stringify(friend),
contentType: 'application/json',
success: result => {
showAlert(result);
}
});
return false;
});
Expand Down
2 changes: 1 addition & 1 deletion routers/chat-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ chatRouter.get('/api/list', async (req, res) => {
if (req.session.activeGroup !== undefined) {
const result = await getChat(req.session.activeGroup._id);
res.send(result);
} else { return res.send([]); } // TODO show that user is not part of any group
} else { return res.send([]); }
});

// DELETE CHAT MESSAGE
Expand Down
2 changes: 1 addition & 1 deletion routers/friends-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ friendsRouter.get('/api/list', async (req, res) => {
if (req.session.activeGroup !== undefined) {
const result = await getFriends(req.session.activeGroup._id);
res.send(result);
} else { return res.send([]); } // TODO show that user is not part of any group
} else { return res.send([]); }
});

export { friendsRouter };
4 changes: 2 additions & 2 deletions routers/history-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ historyRouter.get('/api/list', async (req, res) => {
if (req.session.activeGroup !== undefined) {
const history = await getYearsByGroup(req.session.activeGroup._id);
res.send(history.filter(item => item.revealed));
} else { return res.send([]); } // TODO show that user is not part of any group
} else { return res.send([]); }
});

/* YEAR ROUTER */
Expand All @@ -36,7 +36,7 @@ historyRouter.get('/year/api/gifts', async (req, res) => {
if (req.session.activeGroup !== undefined) {
const yearGifts = await getGiftsByYear(req.session.activeGroup._id, req.query.id);
res.send(yearGifts[0]);
} else { return res.send({ year: req.query.year, gifts: [] }); } // TODO show that user is not part of any group
} else { return res.send({ year: req.query.year, gifts: [] }); }
});

historyRouter.get('/year/api/location-image', async (req, res) => {
Expand Down
2 changes: 2 additions & 0 deletions utils/adminPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ export async function getForbiddenPairs(groupId) {
}, {
$project: {
user: { $first: '$user.name' },
userEmail: { $first: '$user.email' },
userId: { $first: '$user._id' },
forbiddenPair: { $first: '$forbiddenPair.name' },
forbiddenPairEmail: { $first: '$forbiddenPair.email' },
forbiddenPairId: { $first: '$forbiddenPair._id' }
}
}
Expand Down
2 changes: 2 additions & 0 deletions utils/historyPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export async function getGiftsByYear(groupId, yearId) {
gifts: {
$push: {
santa: '$$ROOT.santaUser.name',
santaEmail: '$$ROOT.santaUser.email',
child: '$$ROOT.childUser.name',
childEmail: '$$ROOT.childUser.email',
gift: '$$ROOT.gifts.gift',
imageUploaded: '$$ROOT.gifts.imageUploaded',
giftId: '$$ROOT.gifts.giftId'
Expand Down

0 comments on commit 805b1de

Please sign in to comment.