Skip to content

Commit

Permalink
Confirm Account page is ready. Removing get() (#998)
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Muravja <[email protected]>
  • Loading branch information
kyberorg committed Feb 20, 2023
1 parent 0f1333c commit 06770f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
15 changes: 5 additions & 10 deletions src/main/java/pm/axe/ui/elements/ConfirmedEmailField.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package pm.axe.ui.elements;

import com.vaadin.flow.component.Composite;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.shared.Tooltip;
import com.vaadin.flow.component.textfield.EmailField;
import lombok.Getter;
import pm.axe.mail.EmailConfirmationStatus;

public class ConfirmedEmailField extends Composite<EmailField> {
public class ConfirmedEmailField extends EmailField {
@Getter private final EmailConfirmationStatus status;

@Getter private final Button statusButton;
Expand All @@ -20,21 +19,17 @@ public ConfirmedEmailField() {
statusButton.addThemeVariants(ButtonVariant.LUMO_ICON, ButtonVariant.LUMO_TERTIARY_INLINE);
statusButton.setVisible(false); //no status by default

getContent().setSuffixComponent(statusButton);
getContent().setTooltipText(status.getStatusString());
this.setSuffixComponent(statusButton);
this.setTooltipText(status.getStatusString());

Tooltip tooltip = getContent().getTooltip().withManual(true);
Tooltip tooltip = this.getTooltip().withManual(true);
statusButton.addClickListener(e -> tooltip.setOpened(!tooltip.isOpened()));
}

public void setStatus(final EmailConfirmationStatus status) {
getContent().setTooltipText(status.getStatusString());
this.setTooltipText(status.getStatusString());
statusButton.setIcon(status.getIcon().create());
statusButton.setVisible(status != EmailConfirmationStatus.NONE);
}

public EmailField get() {
return getContent();
}

}
40 changes: 20 additions & 20 deletions src/main/java/pm/axe/ui/pages/user/ConfirmAccountPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ private void initPage() {

private Component emailSectionContent() {
emailInput = new ConfirmedEmailField();
emailInput.get().setLabel("Email address here");
emailInput.get().setHelperText("Axe will send confirmation letter there");
emailInput.get().setClearButtonVisible(true);
emailInput.get().addValueChangeListener(this::onEmailChanged);
emailInput.setLabel("Email address here");
emailInput.setHelperText("Axe will send confirmation letter there");
emailInput.setClearButtonVisible(true);
emailInput.addValueChangeListener(this::onEmailChanged);

Optional<String> currentEmail = accountService.getCurrentEmail(user);
currentEmail.ifPresent(email -> emailInput.get().setValue(email));
currentEmail.ifPresent(email -> emailInput.setValue(email));

submitEmailButton = new Button("Submit");
submitEmailButton.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
Expand All @@ -131,15 +131,15 @@ private Component emailSectionContent() {

if (hasEmail) {
//user has hasEmail - RO input + span
emailInput.get().setReadOnly(true);
emailInput.setReadOnly(true);
EmailConfirmationStatus status = hasConfirmedEmail ?
EmailConfirmationStatus.CONFIRMED : EmailConfirmationStatus.PENDING;
emailInput.setStatus(status);
emailSpan.setText(status.getStatusString());
if (status == EmailConfirmationStatus.CONFIRMED) {
emailSpan.setClassName("green");
} else {
emailInput.get().setHelperText("Axe sent confirmation letter to given email.");
emailInput.setHelperText("Axe sent confirmation letter to given email.");
nothingCame = getNothingCameDetails();
}
emailLayout = new HorizontalLayout(emailInput, emailSpan);
Expand Down Expand Up @@ -199,8 +199,8 @@ private void onEmailChanged(final AbstractField.ComponentValueChangeEvent<EmailF
final String email = e.getValue().trim();
boolean isValidEmail = EmailValidator.getInstance().isValid(email);
if (StringUtils.isBlank(email)) {
emailInput.get().setInvalid(false);
emailInput.get().setErrorMessage("");
emailInput.setInvalid(false);
emailInput.setErrorMessage("");
if (!submitEmailButton.isEnabled()) {
submitEmailButton.setEnabled(true);
}
Expand All @@ -209,40 +209,40 @@ private void onEmailChanged(final AbstractField.ComponentValueChangeEvent<EmailF
if (isValidEmail) {
boolean emailExists = accountService.isAccountAlreadyExists(email, AccountType.EMAIL);
if (emailExists) {
onInvalidInput(emailInput.get(), "Email already taken");
onInvalidInput(emailInput, "Email already taken");
submitEmailButton.setEnabled(false);
} else {
emailInput.get().setInvalid(false);
emailInput.get().setErrorMessage("");
emailInput.setInvalid(false);
emailInput.setErrorMessage("");
if (!submitEmailButton.isEnabled()) {
submitEmailButton.setEnabled(true);
}
}
} else {
//not valid email
onInvalidInput(emailInput.get(), "Should be valid email");
onInvalidInput(emailInput, "Should be valid email");
submitEmailButton.setEnabled(false);
}
}

private void onSubmitEmail(final ClickEvent<Button> event) {
final String email = emailInput.get().getValue().trim();
final String email = emailInput.getValue().trim();
if (StringUtils.isBlank(email)) {
emailInput.get().setInvalid(true);
emailInput.get().setErrorMessage("Please enter valid email");
emailInput.setInvalid(true);
emailInput.setErrorMessage("Please enter valid email");
return;
}
boolean isValidEmail = EmailValidator.getInstance().isValid(email);
if (isValidEmail) {
//email
boolean emailExists = accountService.isAccountAlreadyExists(email, AccountType.EMAIL);
if (emailExists) {
onInvalidInput(emailInput.get(), "Email already taken");
onInvalidInput(emailInput, "Email already taken");
return;
}
} else {
//not valid email
onInvalidInput(emailInput.get(), "Should be valid email address");
onInvalidInput(emailInput, "Should be valid email address");
return;
}

Expand Down Expand Up @@ -279,8 +279,8 @@ private void onSubmitEmail(final ClickEvent<Button> event) {
ErrorUtils.showErrorNotification("Failed to send confirmation letter. Please try again later.");
currentEmailRecord.ifPresent(accountService::rollbackAccount);
}
emailInput.get().setValue(email);
emailInput.get().setReadOnly(true);
emailInput.setValue(email);
emailInput.setReadOnly(true);
emailLayout.replace(submitEmailButton, emailSpan);
}

Expand Down

0 comments on commit 06770f4

Please sign in to comment.