diff --git a/package.json b/package.json index d795376..f1654a4 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,12 @@ "body-parser": "^1.15.2", "express": "^4.14.0", "technologger": "1.0.0" + }, + "devDependencies": { + "eslint": "^3.6.1", + "eslint-config-airbnb": "^12.0.0", + "eslint-plugin-import": "^1.16.0", + "eslint-plugin-jsx-a11y": "^2.2.2", + "eslint-plugin-react": "^6.3.0" } } diff --git a/public/css/main.css b/public/css/main.css index 1395f16..421b41b 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -2,3 +2,8 @@ body { padding: 20%; box-sizing: border-box; } + +.form__warning { + color: red; + font-style: italic; +} diff --git a/public/index.html b/public/index.html index eea457f..d5f9185 100644 --- a/public/index.html +++ b/public/index.html @@ -6,9 +6,22 @@ + -

Hello, world

+

Hello, world

+
+ + +

Please select your language: + +

+ +
diff --git a/public/main.js b/public/main.js index e69de29..b8ec3a4 100644 --- a/public/main.js +++ b/public/main.js @@ -0,0 +1,71 @@ +'use strict'; + +function langChange() { + switch ($('.js-lang')[0].value) { + case 'ru': + $('.js-email_input')[0].setAttribute('placeholder', 'Пожалуйста, введите e-mail'); + $('.js-name_input')[0].setAttribute('placeholder', 'Пожалуйста, введите имя'); + $('.js-select_lang').html('Выберите язык:'); + break; + case 'de': + $('.js-email_input')[0].setAttribute('placeholder', 'Bitte geben Sie eine E-Mail-Adresse ein!'); + $('.js-name_input')[0].setAttribute('placeholder', 'Bitte geben Sie Ihren Namen ein'); + $('.js-select_lang').html('Bitte wähle eine Sprache:'); + break; + default: + $('.js-email_input')[0].setAttribute('placeholder', 'Please enter your e-mail'); + $('.js-name_input')[0].setAttribute('placeholder', 'Please enter your name'); + $('.js-select_lang').html('Please select your language'); + break; + } +} + +function plural(n, lang) { + const pluralRules = { + en: 1, + ru: 7, + de: 1, + }; + const pluralWords = { + en: ['time', 'times'], + ru: ['раз', 'раза', 'раз'], + de: ['mal', 'mal'], + }; + const getPluralForm = function (rule) { + switch (rule) { + case 1: + return (n === 1) ? 0 : 1; + case 7: + if ([1, 11].indexOf(n) > -1) { + return 0; + } + if ([2, 3, 4].indexOf(n % 10) > -1 && (n < 12 || n > 14)) { + return 1; + } + return 2; + default: + return 0; + } + }; + return pluralWords[lang][getPluralForm(pluralRules[lang])]; +} + +function helloText(user, email, lang) { + const n = request('/users', { email: email }); + const message = { + en: `Hi ${user}! You have been here ${n} ${plural(n, lang)} already!`, + ru: `Привет, ${user}! Ты был здесь уже ${n} ${plural(n, lang)}!`, + de: `Hallo ${user}! Du warst hier schon ${n} ${plural(n,lang)}!`, + }; + return message[lang]; +} + +function onSubmit(form) { + const user = form.elements.user.value; + const email = form.elements.email.value; + const lang = form.elements.lang.value ? form.elements.lang.value : 'en'; + form.style.visibility = 'hidden'; + $('.js-hello_message').html(helloText(user, email, lang)); +} + +exports.plural = plural; diff --git a/server.js b/server.js index 2546558..c8fc249 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,25 @@ -let express = require('express'); -let technologger = require('technologger'); -let parser = require('body-parser'); -let app = express(); +const express = require('express'); +const technologger = require('technologger'); +const parser = require('body-parser'); + +const app = express(); + +const userCount = {}; + +function userCountIncrement(email) { + userCount[email] = userCount[email] ? userCount[email] + 1 : 1; +} app.use('/', express.static('public')); app.use(parser.json()); app.use(technologger); -app.post('/users', (req, res, body) => { - console.log(body); - res.send('0'); - // TODO: вернуть количество обращений +app.post('/users', (req, res) => { + userCountIncrement(req.body.email); + res.send(String(userCount[req.body.email])); }); app.listen(process.env.PORT || 3000, () => { - console.log(`App started on port ${process.env.PORT || 3000}`); + console.log(`App started on port ${process.env.PORT || 3000}`); }); diff --git a/test.js b/test.js index 35e9f3b..a928c9c 100644 --- a/test.js +++ b/test.js @@ -1,12 +1,22 @@ let assert = require('assert'); -let hello = require('./public/main').hello; let plural = require('./public/main').plural; -assert.equal(hello('Test'), 'Привет, Test'); +assert.equal(plural(0, 'en'), 'times'); +assert.equal(plural(1, 'en'), 'time'); +assert.equal(plural(2, 'en'), 'times'); +assert.equal(plural(10, 'en'), 'times'); +assert.equal(plural(990, 'en'), 'times'); +assert.equal(plural(0, 'de'), 'mal'); +assert.equal(plural(1, 'de'), 'mal'); +assert.equal(plural(2, 'de'), 'mal'); +assert.equal(plural(110, 'de'), 'mal'); +assert.equal(plural(0, 'ru'), 'раз'); +assert.equal(plural(1, 'ru'), 'раз'); +assert.equal(plural(2, 'ru'), 'раза'); +assert.equal(plural(11, 'ru'), 'раз'); +assert.equal(plural(12, 'ru'), 'раз'); +assert.equal(plural(13, 'ru'), 'раз'); +assert.equal(plural(22, 'ru'), 'раза'); +assert.equal(plural(14, 'ru'), 'раз'); +assert.equal(plural(27, 'ru'), 'раз'); -assert.equal(plural(0), 'Здравствуй, дух'); -assert.equal(plural(1), 'Рады приветствовать на нашем курсе!'); -assert.equal(plural(2), 'Кликай дальше!! Еще осталось 13 раз(а)'); -assert.equal(plural(13), 'Кликай дальше!! Еще осталось 2 раз(а)'); -assert.equal(plural(15), '01001000 01101001 00101100 00100000 01100010 01110010 01101111'); -assert.equal(plural(100), '01001000 01101001 00101100 00100000 01100010 01110010 01101111');