diff --git a/public/scripts/index.js b/public/scripts/index.js index 3771aac..7332ee7 100644 --- a/public/scripts/index.js +++ b/public/scripts/index.js @@ -77,8 +77,11 @@ const Validate = { input.parentNode.appendChild(div); }, clearErrors(input) { - const errorDiv = input.parentNode.querySelector('.error'); + let errorDiv; + const formErrors = document.querySelectorAll('.error.messages'); + if (input) errorDiv = input.parentNode.querySelector('.error'); if (errorDiv) errorDiv.remove(); + if (formErrors) formErrors.forEach(error => error.remove()); }, isEmail(value) { let error = null; @@ -90,6 +93,23 @@ const Validate = { error, value }; + }, + allFields(event) { + Validate.clearErrors(); + + const items = document.querySelectorAll('.item input, .item select, .item textarea'); + items.forEach(item => { + item.style.borderColor = '#ddd'; + if (item.value == '' && item.name != 'removed_files' && item.type != 'file') { + const message = document.createElement('div'); + message.classList.add('messages'); + message.classList.add('error'); + message.innerHTML = 'Por favor, preencha todos os campos.'; + document.querySelector('body').appendChild(message); + item.style.borderColor = '#ff3131'; + event.preventDefault(); + } + }); } } diff --git a/public/styles/global.css b/public/styles/global.css index af297a1..c5022d2 100644 --- a/public/styles/global.css +++ b/public/styles/global.css @@ -196,7 +196,7 @@ button.btn-delete:hover { } .messages { - position: absolute; + position: fixed; top: 16px; right: 16px; display: flex; diff --git a/src/app/controllers/ChefController.js b/src/app/controllers/ChefController.js index ae98dc3..4a75c2e 100644 --- a/src/app/controllers/ChefController.js +++ b/src/app/controllers/ChefController.js @@ -18,16 +18,6 @@ module.exports = { }, async post(req, res) { try { - const keys = Object.keys(req.body); - - for (let key of keys) { - if (req.body[key] == '') - return res.send('Por favor, preencha todos os campos.'); - }; - - if (req.files.length == 0) - return res.send('Por favor, envie uma imagem.'); - const { filename, path } = req.files[0]; const file_id = await File.create({ name: filename, path }); @@ -59,16 +49,6 @@ module.exports = { }, async put(req, res) { try { - const keys = Object.keys(req.body); - - for (let key of keys) { - if (req.body[key] == '' & key != 'removed_files') - return res.send('Por favor, preencha todos os campos.'); - }; - - if (req.body.removed_files && req.files == 0) - return res.send('Por favor, envie uma imagem.'); - let file_id; if (req.files.length != 0) { diff --git a/src/app/controllers/RecipeController.js b/src/app/controllers/RecipeController.js index e2a6331..41476ca 100644 --- a/src/app/controllers/RecipeController.js +++ b/src/app/controllers/RecipeController.js @@ -32,16 +32,6 @@ module.exports = { }, async post(req, res) { try { - const keys = Object.keys(req.body); - - for (let key of keys) { - if (req.body[key] == '') - return res.send('Por favor, preencha todos os campos.'); - }; - - if (req.files.length == 0) - return res.send('Por favor, envie pelo menos uma imagem.'); - const { chef: chef_id, title, ingredients, preparation, information } = req.body; @@ -93,13 +83,6 @@ module.exports = { }, async put(req, res) { try { - const keys = Object.keys(req.body); - - for (let key of keys) { - if (req.body[key] == '' && key != 'removed_files') - return res.send('Por favor, preencha todos os campos.'); - }; - let { id, removed_files, chef: chef_id, title, ingredients, preparation, information } = req.body; diff --git a/src/app/validators/chef.js b/src/app/validators/chef.js new file mode 100644 index 0000000..aa4d250 --- /dev/null +++ b/src/app/validators/chef.js @@ -0,0 +1,27 @@ +const { checkAllFields } = require('../../lib/utils'); + +function post(req, res, next) { + const fillAllFields = checkAllFields(req.body); + if (fillAllFields) return res.send(fillAllFields.error); + + if (req.files.length == 0) { + return res.send('Por favor, envie uma imagem.'); + } + next(); +} + +function put(req, res, next) { + const fillAllFields = checkAllFields(req.body); + if (fillAllFields) return res.send(fillAllFields.error); + + if (req.body.removed_files && req.files == 0) { + return res.send('Por favor, envie uma imagem.'); + } + + next(); +} + +module.exports = { + post, + put +} \ No newline at end of file diff --git a/src/app/validators/recipe.js b/src/app/validators/recipe.js new file mode 100644 index 0000000..ed6387b --- /dev/null +++ b/src/app/validators/recipe.js @@ -0,0 +1,22 @@ +const { checkAllFields } = require('../../lib/utils'); + +function post(req, res, next) { + const fillAllFields = checkAllFields(req.body); + if (fillAllFields) return res.send(fillAllFields.error); + + if (req.files.length == 0) { + return res.send('Por favor, envie uma imagem.'); + } + next(); +} + +function put(req, res, next) { + const fillAllFields = checkAllFields(req.body); + if (fillAllFields) return res.send(fillAllFields.error); + next(); +} + +module.exports = { + post, + put +} \ No newline at end of file diff --git a/src/app/validators/session.js b/src/app/validators/session.js index 4940bb2..40b4c31 100644 --- a/src/app/validators/session.js +++ b/src/app/validators/session.js @@ -5,6 +5,11 @@ const User = require('../models/User'); async function login(req, res, next) { const { email, password } = req.body; + if (!email || !password) return res.render('session/login', { + user: req.body, + error: 'Por favor, entre com seu email e senha.' + }); + const mailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; if (!email.match(mailFormat)) return res.render('session/login', { user: req.body, diff --git a/src/app/validators/user.js b/src/app/validators/user.js index 4dbfe95..23a1ae7 100644 --- a/src/app/validators/user.js +++ b/src/app/validators/user.js @@ -1,17 +1,5 @@ const User = require('../models/User'); - -function checkAllFields(body) { - const keys = Object.keys(body); - - for (let key of keys) { - if (body[key] == '') { - return { - user: body, - error: 'Por favor, preencha todos os campos!' - }; - } - } -} +const { checkAllFields } = require('../../lib/utils'); async function show(req, res, next) { const { userId: id } = req.session; diff --git a/src/app/views/admin/chefs/create.njk b/src/app/views/admin/chefs/create.njk index 97f624d..5347290 100644 --- a/src/app/views/admin/chefs/create.njk +++ b/src/app/views/admin/chefs/create.njk @@ -12,7 +12,7 @@

Novo Chef

Cancelar - +
diff --git a/src/app/views/admin/chefs/edit.njk b/src/app/views/admin/chefs/edit.njk index 9e7565e..0f56ed6 100644 --- a/src/app/views/admin/chefs/edit.njk +++ b/src/app/views/admin/chefs/edit.njk @@ -13,7 +13,7 @@
- +
diff --git a/src/app/views/admin/recipes/create.njk b/src/app/views/admin/recipes/create.njk index b7d1dea..4fd3c07 100644 --- a/src/app/views/admin/recipes/create.njk +++ b/src/app/views/admin/recipes/create.njk @@ -12,7 +12,7 @@

Nova receita

Cancelar - +
diff --git a/src/app/views/admin/recipes/edit.njk b/src/app/views/admin/recipes/edit.njk index 78dd496..3c70caf 100644 --- a/src/app/views/admin/recipes/edit.njk +++ b/src/app/views/admin/recipes/edit.njk @@ -13,7 +13,7 @@
- +
diff --git a/src/app/views/session/login.njk b/src/app/views/session/login.njk index ebab910..04fd3a3 100644 --- a/src/app/views/session/login.njk +++ b/src/app/views/session/login.njk @@ -21,7 +21,7 @@ Perdeu a senha?
  • - +
  • diff --git a/src/app/views/templates/includes/chefs/chefs-fields.njk b/src/app/views/templates/includes/chefs/chefs-fields.njk index b4db309..307d063 100644 --- a/src/app/views/templates/includes/chefs/chefs-fields.njk +++ b/src/app/views/templates/includes/chefs/chefs-fields.njk @@ -4,7 +4,6 @@ diff --git a/src/app/views/templates/includes/recipes/recipes-fields.njk b/src/app/views/templates/includes/recipes/recipes-fields.njk index ea7a587..8e19862 100644 --- a/src/app/views/templates/includes/recipes/recipes-fields.njk +++ b/src/app/views/templates/includes/recipes/recipes-fields.njk @@ -38,7 +38,7 @@ @@ -47,7 +47,7 @@
    Chef
    - {% for chef in chefsOptions %}
    @@ -77,7 +77,7 @@
    {% endfor %} @@ -92,7 +92,7 @@ @@ -101,7 +101,7 @@ {% endfor %} @@ -112,6 +112,6 @@
    informações adicionais
    - +
    \ No newline at end of file diff --git a/src/app/views/templates/includes/sessions/login-fields.njk b/src/app/views/templates/includes/sessions/login-fields.njk index 9852d54..53be5fd 100644 --- a/src/app/views/templates/includes/sessions/login-fields.njk +++ b/src/app/views/templates/includes/sessions/login-fields.njk @@ -5,7 +5,6 @@ type="email" name="email" placeholder="Digite seu email" - required value="{{user.email}}" onblur="Validate.apply(this, 'isEmail')" > @@ -19,7 +18,6 @@ type="password" name="password" placeholder="Digite sua senha" - required > \ No newline at end of file diff --git a/src/app/views/users/edit.njk b/src/app/views/users/edit.njk index 9e1d08c..87ffc69 100644 --- a/src/app/views/users/edit.njk +++ b/src/app/views/users/edit.njk @@ -13,7 +13,7 @@
    - +
    diff --git a/src/app/views/users/index.njk b/src/app/views/users/index.njk index afe0ab5..9edace3 100644 --- a/src/app/views/users/index.njk +++ b/src/app/views/users/index.njk @@ -12,7 +12,8 @@

    Bem-vindo {{user.name}}

    - +
    diff --git a/src/app/views/users/register.njk b/src/app/views/users/register.njk index 80face1..0890e06 100644 --- a/src/app/views/users/register.njk +++ b/src/app/views/users/register.njk @@ -13,7 +13,7 @@
    Cancelar - +
    diff --git a/src/lib/utils.js b/src/lib/utils.js index 5e1a2eb..330eadd 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -10,7 +10,7 @@ module.exports = { Logo Foodfy @@ -46,5 +46,17 @@ module.exports = { src: `${file.path.replace('public', '')}` })); return files; + }, + checkAllFields(body) { + const keys = Object.keys(body); + + for (let key of keys) { + if (body[key] == '' & key != 'removed_files') { + return { + user: body, + error: 'Por favor, preencha todos os campos!' + }; + } + } } }; \ No newline at end of file diff --git a/src/routes/chefs.js b/src/routes/chefs.js index 12b6d34..ae87b9a 100644 --- a/src/routes/chefs.js +++ b/src/routes/chefs.js @@ -5,14 +5,15 @@ const multer = require('../app/middlewares/multer'); const ChefController = require('../app/controllers/ChefController'); const { onlyUsers, isAdmin } = require('../app/middlewares/sessions'); +const Validator = require('../app/validators/chef'); // Chefs Admin // routes.get('/', onlyUsers, ChefController.index); routes.get('/create', onlyUsers, isAdmin, ChefController.create); routes.get('/:id', onlyUsers, ChefController.show); routes.get('/:id/edit', isAdmin, onlyUsers, ChefController.edit); -routes.post('/', onlyUsers, isAdmin, multer.array('photos', 1), ChefController.post); -routes.put('/', onlyUsers, isAdmin, multer.array('photos', 1), ChefController.put); +routes.post('/', onlyUsers, isAdmin, multer.array('photos', 1), Validator.post, ChefController.post); +routes.put('/', onlyUsers, isAdmin, multer.array('photos', 1), Validator.put, ChefController.put); routes.delete('/', onlyUsers, isAdmin, ChefController.delete); module.exports = routes; \ No newline at end of file diff --git a/src/routes/recipes.js b/src/routes/recipes.js index e6c75e1..84892eb 100644 --- a/src/routes/recipes.js +++ b/src/routes/recipes.js @@ -5,6 +5,7 @@ const multer = require('../app/middlewares/multer'); const RecipeController = require('../app/controllers/RecipeController'); const { onlyUsers, isCreator } = require('../app/middlewares/sessions'); +const Validator = require('../app/validators/recipe'); // Recipes Admin // routes.get('/', onlyUsers, RecipeController.index); @@ -12,8 +13,8 @@ routes.get('/my-recipes', onlyUsers, RecipeController.userRecipes); routes.get('/create', onlyUsers, RecipeController.create); routes.get('/:id', onlyUsers, RecipeController.show); routes.get('/:id/edit', onlyUsers, isCreator, RecipeController.edit); -routes.post('/', onlyUsers, multer.array('photos', 5), RecipeController.post); -routes.put('/', onlyUsers, multer.array('photos', 5), RecipeController.put); +routes.post('/', onlyUsers, multer.array('photos', 5), Validator.post, RecipeController.post); +routes.put('/', onlyUsers, multer.array('photos', 5),Validator.put, RecipeController.put); routes.delete('/', onlyUsers, RecipeController.delete); module.exports = routes; \ No newline at end of file