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

Issue 245 #274

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
9 changes: 9 additions & 0 deletions src/components/ChangePassword/images/eye-slash.svg
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 186 additions & 0 deletions src/components/ChangePassword/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import { Component } from 'pet-dex-utilities';
import TextInput from '../TextInput';
import Button from '../Button';
import './index.scss';

import eyeSlashIcon from './images/eye-slash.svg';

const events = ['change-password'];
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

const html = `
<div>
<div class="change-password-container">
<form
data-select="change-password-form"
class="change-password-container__change-password-form"
action="submit"
>
<h3 class="change-password-container__title">
Senha antiga
</h3>
<div data-select="password-input-container"></div>
<span data-select="password-error-message" class="error-message"></span>

<div class="change-password-container__separator">
<hr class="divisor" />
</div>

<h3 class="change-password-container__title">Nova senha</h3>

<div data-select="new-password-input-container" class="change-password-container__new-password"></div>
<span data-select="new-password-error-message" class="error-message"></span>

<div data-select="confirm-password-input-container" class="change-password-container__confirm-password"></div>
<span data-select="confirm-password-error-message" class="error-message"></span>

<ul class="change-password-container__password-tips">
<li>Insira no mínimo 6 caracteres</li>
<li>A senha deve conter uma letra maiúscula</li>
<li>Deve conter um caractere especial</li>
</ul>
</form>
</div>
</div>
`;

export default function ChangePassword() {
Component.call(this, { html, events });
const $changePasswordForm = this.selected.get('change-password-form');
const $passwordInputContainer = this.selected.get('password-input-container');
const $passwordErrorMessage = this.selected.get('password-error-message');
const $newPasswordInputContainer = this.selected.get(
'new-password-input-container',
);
const $newPasswordErrorMessage = this.selected.get(
'new-password-error-message',
);
const $confirmPasswordInputContainer = this.selected.get(
'confirm-password-input-container',
);
const $confirmPasswordErrorMessage = this.selected.get(
'confirm-password-error-message',
);

const passwordInput = new TextInput({
placeholder: 'Senha',
assetUrl: eyeSlashIcon,
assetPosition: 'suffix',
});
const newPasswordInput = new TextInput({
placeholder: 'Nova senha',
assetUrl: eyeSlashIcon,
assetPosition: 'suffix',
});
const confirmPasswordInput = new TextInput({
placeholder: ' Confirmar senha',
assetUrl: eyeSlashIcon,
assetPosition: 'suffix',
});
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
const submitButton = new Button({
text: 'Salvar',
isFullWidth: true,
isDisabled: true,
});

passwordInput.mount($passwordInputContainer);
newPasswordInput.mount($newPasswordInputContainer);
confirmPasswordInput.mount($confirmPasswordInputContainer);
submitButton.mount($changePasswordForm);

passwordInput.selected.get('input-text').type = 'password';
newPasswordInput.selected.get('input-text').type = 'password';
confirmPasswordInput.selected.get('input-text').type = 'password';

const validateFields = () => {
const password = passwordInput.selected.get('input-text').value;
const newPassword = newPasswordInput.selected.get('input-text').value;
const confirmPassword =
confirmPasswordInput.selected.get('input-text').value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talvez não seja ideal validar os 3 campos juntos, acredito que essa função poderia receber um data-select como parametro, ai pegar apenas aquele campo e entao valida-lo


if (password && newPassword && confirmPassword) {
submitButton.enable();
} else {
submitButton.disable();
}
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
};

passwordInput.selected
.get('input-text')
.addEventListener('input', validateFields);
newPasswordInput.selected
.get('input-text')
.addEventListener('input', validateFields);
confirmPasswordInput.selected
.get('input-text')
.addEventListener('input', validateFields);

submitButton.listen('click', () => {
const password = passwordInput.selected.get('input-text').value;
const newPassword = newPasswordInput.selected.get('input-text').value;
const confirmPassword =
confirmPasswordInput.selected.get('input-text').value;
let validPassword = true;
let validNewPassword = true;
let validConfirmPassword = true;
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

if (!this.validatePassword(password)) {
validPassword = false;
$passwordErrorMessage.classList.add('show-error');
$passwordErrorMessage.innerText = 'Senha inválida';
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
passwordInput.inputError();
}
if (!this.validatePassword(newPassword)) {
validNewPassword = false;
$newPasswordErrorMessage.classList.add('show-error');
$newPasswordErrorMessage.innerText = 'Senha inválida';
newPasswordInput.inputError();
}
if (!this.validatePassword(confirmPassword)) {
validConfirmPassword = false;
$confirmPasswordErrorMessage.classList.add('show-error');
$confirmPasswordErrorMessage.innerText = 'Senha inválida';
confirmPasswordInput.inputError();
}
if (!this.comparePasswords(newPassword, confirmPassword)) {
validNewPassword = false;
validConfirmPassword = false;
$newPasswordErrorMessage.classList.add('show-error');
$confirmPasswordErrorMessage.classList.add('show-error');
$newPasswordErrorMessage.innerText = 'As senhas informadas não coincidem';
$confirmPasswordErrorMessage.innerText =
'As senhas informadas não coincidem';
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
newPasswordInput.inputError();
confirmPasswordInput.inputError();
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
}

if (validPassword) $passwordErrorMessage.classList.remove('show-error');
if (validNewPassword)
$newPasswordErrorMessage.classList.remove('show-error');
if (validConfirmPassword)
$confirmPasswordErrorMessage.classList.remove('show-error');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A remoção do erro idealmente deveria acontecer no ato de digitar, no caso, se a pessoa alterar algum dos campos com erro ja remover os erros, remover apenas quando poder enviar da uma impressão de que a pessoa teria que clicar no botao de enviar mesmo com os campos exibindo erro, talvez isso não seja ideal


if (validPassword && validNewPassword && validConfirmPassword) {
this.changePassword();
}
});
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
}
ChangePassword.prototype = Object.assign(
ChangePassword.prototype,
Component.prototype,
{
changePassword() {
this.emit('change-password');
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
},
comparePasswords(password, newPassword) {
return password === newPassword;
},
validatePassword(password) {
const hasMinLength = password.length >= 10;
const hasUppercase = /[A-Z]/g.test(password);
const hasNumber = /[0-9]/g.test(password);
const hasSpecialCharacter = /[!@#$%^&*(),.?":{}|<>]/g.test(password);

return hasMinLength && hasUppercase && hasNumber && hasSpecialCharacter;
},
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
},
);
81 changes: 81 additions & 0 deletions src/components/ChangePassword/index.scss
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@use '~styles/colors.scss' as colors;
@use '~styles/fonts.scss' as fonts;
@use '~styles/breakpoints.scss' as breakpoints;

.change-password-container {
font-family: fonts.$primaryFont;
line-height: fonts.$headerLineHeight;

background-color: rgb(255, 255, 255);
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

&__title {
color: colors.$gray600;
font-size: 1.8rem;
font-weight: fonts.$headerFontWeight;
}

&__change-password-form {
display: flex;
flex-direction: column;
gap: 2rem;

justify-content: start;
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

.input-text-container {
&__input {
background-size: auto 40%;

&.input-error {
color: rgb(255, 255, 255);
}
}
}
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

.button {
margin-top: 3rem;
}

.error-message {
display: none;

color: colors.$error100;

&.show-error {
display: block;
}
}
}
Clerijr marked this conversation as resolved.
Show resolved Hide resolved

&__password-tips {
color: colors.$gray500;
font-weight: fonts.$footerFontWeight;

list-style-type: disc;
padding-inline-start: 2rem;
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
}

&__error-message {
display: block;

color: colors.$error100;

margin-top: 0.8rem;

user-select: all;
}

&__separator {
display: flex;

.divisor {
width: 100%;

border: unset;
Clerijr marked this conversation as resolved.
Show resolved Hide resolved
border-top: 0.1rem solid colors.$gray200;
}

.separator-text {
margin: 0 2.8rem;
}
}
}
Empty file.
41 changes: 41 additions & 0 deletions src/stories/ChangePassword.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Drawer from '../components/Drawer';
import ChangePassword from '../components/ChangePassword';
import Button from '../components/Button';
import { initializeSwiper } from '../utils/swiper';

export default {
title: 'Components/ChangePassword',
parameters: {
backgrounds: {
default: 'petdex',
values: [
{
name: 'default',
value: '#F8F8F8',
},
{
name: 'petdex',
value: '#003459',
},
],
},
},
render: () => {
initializeSwiper();
const button = new Button({
text: 'open',
});
const changePassword = new ChangePassword();
const $container = document.createElement('div');
const drawer = new Drawer({
title: 'Alterar senha',
content: changePassword,
});
button.listen('click', () => drawer.open());
button.mount($container);

return $container;
},
};

export const Default = {};