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

changing alert to toast message #195

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
18 changes: 17 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>To-Do List App</title>
<title>Todo-List</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="toast.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css"
integrity="sha512-Kc323vGBEqzTmouAECnVceyQqyqdsSiqLQISBL29aUW4U/M7pSPA/gEUZQqv1cwx4OnYxTxve5UMg5GT6L4JJg=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css"
integrity="sha512-DTOQO9RWCH3ppGqcWaEA1BIZOC6xxalwEsw9c2QQeAIftl+Vegovlnee1c9QX4TctnWMn13TZye+giMm8e2LwA=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
</head>
<body class="bg-gray-50 text-gray-800">
<!-- Navbar -->
Expand Down
99 changes: 89 additions & 10 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ checkIcon.classList.add('fa-solid', 'fa-check');
// Display the remaining characters count out of 120
document.querySelector('.js-name-input').addEventListener('input', (e) => {
let input = e.target.value;
if (input.length === 120) {
alert('max character limits exceeded');
if (input.length > 120) {
showToast('max character limits reached', 'Danger');
e.target.value = input.slice(0, 120);
}
});

Expand Down Expand Up @@ -73,6 +74,34 @@ function clearInputs() {
setDefaultDateTime();
}

function validInfo(name, date, time, category, priority) {
const specifyMessage = [];
if (!name) {
specifyMessage.push('task');
}
if (!date) {
specifyMessage.push('date');
}
if (!time) {
specifyMessage.push('time');
}
if (!category) {
specifyMessage.push('category');
}
if (!priority) {
specifyMessage.push('priority');
}

if (!name || !date || !time || !category || !priority) {
showToast(
`Please fill this fields: ${specifyMessage.join(', ')}.`,
'Danger'
);
return false;
}
return true;
}

function addTodo() {
const inputNameElement = document.querySelector('.js-name-input');
const inputDateElement = document.querySelector('.js-date-input');
Expand All @@ -87,22 +116,17 @@ function addTodo() {
let priority = inputPriorityElement.value;

// Validation checks
if (!name || !date || !time || !category || !priority) {
alert(
'Please fill in all fields: task, date, time, category, and priority.'
);
return;
}
if (!validInfo(name, date, time, category, priority)) return;

// Check that date is not in past
if (date < inputDateElement.min) {
alert('Please select the current date or a future date.');
showToast('Please select the current date or a future date.', 'Danger');
return;
}

// Check that time is not in past
if (time < inputTimeElement.min && date === inputDateElement.min) {
alert('Please select a future time.');
showToast('Please select the current time or a future time.', 'Danger');
return;
}

Expand Down Expand Up @@ -379,6 +403,61 @@ document.addEventListener('DOMContentLoaded', () => {
.addEventListener('change', filterTodos);
});

/** New toast Info */
class Toast {
constructor(message, color, time) {
this.message = message;
this.color = color;
this.time = time;
this.element = null;
var element = document.createElement('div');
element.className = 'toast-notification hide';
this.element = element;
var countElements = document.getElementsByClassName('toast-notification');
element.style.opacity = 0.9;

element.style.marginBottom = countElements.length * 55 + 'px';

element.style.backgroundColor = this.color;

element.textContent = this.message;

var close = document.createElement('div');
close.className = 'close-notification';

var icon = document.createElement('i');
icon.className = 'fa fa-times';

close.appendChild(icon);
element.appendChild(close);

document.body.appendChild(element);

setTimeout(() => {
element.classList.replace('hide', 'show');
}, 100);

setTimeout(() => {
element.classList.replace('show', 'hide');
setTimeout(() => element.remove(), 300);
}, this.time);

close.addEventListener('click', () => {
element.classList.replace('show', 'hide');
setTimeout(() => element.remove(), 300);
});
}
}

const ToastType = {
Danger: '#eb3b5a',
Warning: '#f6b93b',
Succes: '#00b894',
};
function showToast(message, type) {
new Toast(message, ToastType[type], 3000);
}

// Add year in the footer(CopyRight Notice)
let year = document.querySelector('.year');
year.innerText = new Date().getFullYear();
43 changes: 43 additions & 0 deletions toast.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.toast-notification {
position: fixed;
bottom: 20px; /* Espacio desde la parte inferior */
right: 20px; /* Espacio desde el lado derecho */
min-width: 250px; /* Ancho mínimo */
max-width: 350px; /* Ancho máximo */
padding: 15px 20px; /* Espacio interno */
margin-bottom: 10px; /* Espacio entre toasts si hay más de uno */
color: #fff;
border-radius: 8px;
font-size: 14px;
display: flex;
align-items: center;
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.2);
opacity: 0.9;
transition:
opacity 0.3s ease,
transform 0.3s ease;
transform: translateY(20px);
}

.toast-notification.show {
opacity: 1;
transform: translateY(0);
}

.toast-notification.hide {
opacity: 0;
transform: translateY(20px);
}

.close-notification {
margin-left: 15px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}

.close-notification i {
font-size: 16px;
color: #fff;
}
Loading