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 @@