Skip to content

Commit

Permalink
max 5 notifications, more tts details on shoppinglist
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoryne committed Dec 10, 2024
1 parent c116017 commit 45ff902
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
41 changes: 31 additions & 10 deletions apps/shoppinglist/templates/shopping_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,39 @@ <h3>Total Items</h3>

<script>
document.getElementById('btn-tts').addEventListener('click', () => {
const items = [...document.querySelectorAll('#shopping-list tbody tr td:nth-child(2)')]
.map(td => td.textContent.trim())
.join(', ');

if (items) {
const utterance = new SpeechSynthesisUtterance(`Your shopping list includes: ${items}`);
utterance.lang = 'en-US';
utterance.rate = 1;
const rows = [...document.querySelectorAll('#shopping-list tbody tr')];

const inCartItems = [];
const notInCartItems = [];

rows.forEach(row => {
const quantity = row.querySelector('td:nth-child(3)').textContent.trim();
const itemName = row.querySelector('td:nth-child(2)').textContent.trim();
if (row.classList.contains('in-cart')) {
inCartItems.push(`${quantity} ${itemName}`);
} else {
notInCartItems.push(`${quantity} ${itemName}`);
}
});

if (notInCartItems.length == 0) {
const utterance = new SpeechSynthesisUtterance("Your shopping list is empty.");
window.speechSynthesis.speak(utterance);
}

if (inCartItems.length == 0) {
const utterance = new SpeechSynthesisUtterance("Your cart is empty.");
window.speechSynthesis.speak(utterance);
}

if (notInCartItems.length > 0) {
const utterance = new SpeechSynthesisUtterance("Items in shopping list are: " + notInCartItems.join(', ') + ".");
window.speechSynthesis.speak(utterance);
}

if (inCartItems.length > 0) {
const utterance = new SpeechSynthesisUtterance("Items in cart are: " + inCartItems.join(', ') + ".");
window.speechSynthesis.speak(utterance);
} else {
alert('Your shopping list is empty!');
}
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion foodguard/context_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

def notifications(request):
if request.user.is_authenticated:
user_notifications = Notification.objects.filter(user=request.user).order_by('-created_at')
user_notifications = Notification.objects.filter(user=request.user).order_by('-created_at')[:5]
else:
user_notifications = []
return {'notifications': user_notifications}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 45ff902

Please sign in to comment.