diff --git a/client/addfriends.script.js b/client/addfriends.script.js index a8c8386..607096b 100644 --- a/client/addfriends.script.js +++ b/client/addfriends.script.js @@ -10,14 +10,14 @@ function addFriendRow() { idInput.value = table.rows.length; // Sequential ID based on the row count idCell.appendChild(idInput); - // Create name cell + // Name cell with translated default value var nameCell = newRow.insertCell(1); - var nameInput = createInputField('text', 'name', 'Nombre'); + var nameInput = createInputField('text', 'name', languageData[currentLanguage].defaultName); nameCell.appendChild(nameInput); - // Create email cell + // Email cell with translated default value var emailCell = newRow.insertCell(2); - var emailInput = createInputField('email', 'email', 'correo@example.com'); + var emailInput = createInputField('email', 'email', languageData[currentLanguage].defaultEmail); emailCell.appendChild(emailInput); // Create exclude cell @@ -26,11 +26,11 @@ function addFriendRow() { excludeCell.appendChild(excludeInput); } -function createInputField(type, name, value) { +function createInputField(type, name, defaultValue) { var input = document.createElement('input'); input.type = type; input.name = name; - input.value = value; - input.onblur = saveToSessionStorage; // Attach the onblur event + input.value = defaultValue; + input.onblur = saveToSessionStorage; return input; } diff --git a/client/languageswitcher.script.js b/client/languageswitcher.script.js new file mode 100644 index 0000000..626082f --- /dev/null +++ b/client/languageswitcher.script.js @@ -0,0 +1,32 @@ +function switchLanguage(lang) { + document.getElementById('headerTitle').textContent = languageData[lang]['headerTitle']; + document.getElementById('addFriendsButton').textContent = languageData[lang]['addFriends']; + document.getElementById('clearAllButton').textContent = languageData[lang]['clearAll']; + document.getElementById('sendEmailsButton').textContent = languageData[lang]['sendEmails']; + document.getElementById('nameTh').textContent = languageData[lang]['nameTh']; + document.getElementById('emailTh').textContent = languageData[lang]['emailTh']; + document.getElementById('exclusionTh').textContent = languageData[lang]['exclusionTh']; + + document.querySelectorAll("#friendsTable input[name='name']").forEach(input => { + if (input.value === languageData['en'].defaultName || input.value === languageData['es'].defaultName) { + input.value = languageData[lang].defaultName; + } + }); + document.querySelectorAll("#friendsTable input[name='email']").forEach(input => { + if (input.value === languageData['en'].defaultEmail || input.value === languageData['es'].defaultEmail) { + input.value = languageData[lang].defaultEmail; + } + }); +} + +function toggleLanguage() { + currentLanguage = currentLanguage === 'es' ? 'en' : 'es'; + sessionStorage.setItem('preferredLanguage', currentLanguage); // Store in session storage + switchLanguage(currentLanguage); + updateLanguageSwitcherText(); +} + +function updateLanguageSwitcherText() { + const switcherText = currentLanguage === 'es' ? 'Switch to English' : 'Cambiar a Español'; + document.getElementById('languageSwitcher').textContent = switcherText; +} diff --git a/client/sendassignments.script.js b/client/sendassignments.script.js index 2317ff0..ddcb1a5 100644 --- a/client/sendassignments.script.js +++ b/client/sendassignments.script.js @@ -1,24 +1,29 @@ function sendAssignmentsToServer() { - const assignments = JSON.parse(sessionStorage.getItem('secretSantaAssignments')); - if (!assignments) { - console.error('No assignments found in session storage'); - return; - } + const assignments = JSON.parse(sessionStorage.getItem('secretSantaAssignments')); + const language = sessionStorage.getItem('preferredLanguage') || 'es'; // Default to Spanish if not set - fetch('http://localhost:3500/send-emails', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ assignments: assignments }) - }) + if (!assignments) { + console.error('No assignments found in session storage'); + return; + } + + fetch('http://localhost:3500/send-emails', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + assignments: assignments, + language: language, // Include the language preference in the request + }), + }) .then(response => response.text()) .then(data => { - console.log('Response from server:', data); - // You can add more UI feedback here, e.g., a success message + console.log('Response from server:', data); + // Additional UI feedback for success can be added here }) .catch(error => { - console.error('Error sending assignments:', error); - // Handle the error in the UI as well + console.error('Error sending assignments:', error); + // Handle the error in the UI as well }); } diff --git a/client/translation.l18n.js b/client/translation.l18n.js new file mode 100644 index 0000000..366df88 --- /dev/null +++ b/client/translation.l18n.js @@ -0,0 +1,24 @@ +const languageData = { + en: { + headerTitle: `🎅 Secret Santa's Draw 🎅`, + addFriends: 'Add More Friends', + clearAll: 'Clear All', + sendEmails: 'Send Emails', + nameTh: 'Name', + emailTh: 'Email', + exclusionTh: 'Exclusions (Comma separated names)', + defaultName: 'Name', + defaultEmail: 'name@example.com', + }, + es: { + headerTitle: '🎅 Sorteo del Amigo Invisible 🎅', + addFriends: 'Añadir más amigos', + clearAll: 'Borrar todos', + sendEmails: 'Enviar correos', + nameTh: 'Nombre', + emailTh: 'Correo', + exclusionTh: 'Exclusiones (Nombres separados por coma)', + defaultName: 'Nombre', + defaultEmail: 'correo@ejemplo.com', + }, +}; diff --git a/index.html b/index.html index bb64e8c..c0a5b6e 100644 --- a/index.html +++ b/index.html @@ -17,16 +17,23 @@ + +
-

Sorteo del Amigo Invisible

+ Switch to English +

🎅 Sorteo del Amigo Invisible 🎅

@@ -36,16 +43,17 @@

Sorteo del Amigo Invisible

- - + + - - - + + + @@ -65,8 +73,8 @@

Sorteo del Amigo Invisible

IDNombreEmailExclusiones (Nombres separados por comma)NombreEmailExclusiones (Nombres separados por comma)
- +
diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e69de29 diff --git a/server/emailsender.js b/server/emailsender.js index a005e7d..25ed461 100644 --- a/server/emailsender.js +++ b/server/emailsender.js @@ -1,7 +1,9 @@ const nodemailer = require('nodemailer'); -const createSecretSantaEmail = require('./emailtemplate.js'); +const createSecretSantaEmailEN = require('./englishemailtemplate'); +const createSecretSantaEmailES = require('./spanishemailtemplate'); -async function sendEmails(assignments) { +async function sendEmails(assignments, language) { + language = language || 'es'; const transporter = nodemailer.createTransport({ service: 'hotmail', auth: { @@ -10,13 +12,17 @@ async function sendEmails(assignments) { }, }); + const createSecretSantaEmail = language === 'es' ? createSecretSantaEmailES : createSecretSantaEmailEN; + for (const person of assignments) { const emailContent = createSecretSantaEmail(person.name, person.secretSantaFor); + const subject = language === 'es' ? '🎄Tu amigo invisible esta Navidad🌟' : '🎄Your Secret Santa this Christmas🌟'; + const mailOptions = { from: process.env.EMAIL_USER, to: person.email, - subject: '🎄Tu amigo invisible esta Navidad🌟', + subject: subject, html: emailContent, }; diff --git a/server/englishemailtemplate.js b/server/englishemailtemplate.js new file mode 100644 index 0000000..db21a4f --- /dev/null +++ b/server/englishemailtemplate.js @@ -0,0 +1,43 @@ +function createSecretSantaEmailEN(receiverName, secretSantaFor) { + return ` + + + + + +
+
🎅 Secret Santa Assignment 🎅
+
+

Hello ${receiverName},

+

🎄 You have been chosen to be the Secret Santa for: ${secretSantaFor}!

+

Remember, it's a secret and have fun picking a 🎁gift🎁!!

+

🌟Happy Holidays!🌟

+
+
+ + `; +} + +module.exports = createSecretSantaEmailEN; diff --git a/server/server.js b/server/server.js index 4ecf1d8..daf92e2 100644 --- a/server/server.js +++ b/server/server.js @@ -11,14 +11,13 @@ const port = 3500; app.use(bodyParser.json()); app.use(cors({ origin: '*' })); // Allows requests from all origins -app.post('/send-emails', (req, res) => { +app.post('/send-emails', async (req, res) => { try { - const assignments = req.body.assignments; // Expecting assignments to be in the request body - sendEmails(assignments); - res.status(200).send('Emails are being sent.'); + await sendEmails(req.body.assignments, req.body.language); + res.send('Emails sent successfully.'); } catch (error) { console.error(error); - res.status(500).send('An error occurred while sending emails.'); + res.status(500).send('Error sending emails'); } }); diff --git a/server/emailtemplate.js b/server/spanishemailtemplate.js similarity index 88% rename from server/emailtemplate.js rename to server/spanishemailtemplate.js index a924b51..9286247 100644 --- a/server/emailtemplate.js +++ b/server/spanishemailtemplate.js @@ -1,4 +1,4 @@ -function createSecretSantaEmail(receiverName, secretSantaFor) { +function createSecretSantaEmailES(receiverName, secretSantaFor) { return ` @@ -33,11 +33,11 @@ function createSecretSantaEmail(receiverName, secretSantaFor) {

Hola ${receiverName},

🎄Te toco ser el amigo invisible de: ${secretSantaFor}!

Recorda que es un secreto y divertite buscando un 🎁regalo🎁!!

-

🌟Happy Holidays!🌟

+

🌟¡Felices Fiestas!🌟

`; } -module.exports = createSecretSantaEmail; +module.exports = createSecretSantaEmailES;