Skip to content

Commit

Permalink
Merge pull request #24 from euseanwoon2016/ming/task/profile-stuff
Browse files Browse the repository at this point in the history
✨: Added Profile Functionality
  • Loading branch information
wooneusean authored Dec 3, 2021
2 parents 4bb7e2b + cf9513e commit 12ac9fe
Show file tree
Hide file tree
Showing 10 changed files with 609 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class DashboardController implements Initializable {
@FXML
private MFXButton btnMenu;

@FXML
private MFXPillButton btnAccount;

@FXML
private MFXButton btnHome;

Expand Down Expand Up @@ -96,6 +99,11 @@ void onNavBtnPress(ActionEvent event, String route) {
Navigator.navigateInContainer(route, vbxContent, this);
}

@FXML
void onAccountPressed(ActionEvent event) {
Navigator.showInDialog(btnAccount.getScene().getWindow(),"view_profile", null);
}

@FXML
void onLogoutPressed(ActionEvent event) {
Logging.write(String.format("Logout {user_id: %d, committee: %b}", Global.getUserId(), Global.isCommittee()));
Expand All @@ -107,6 +115,10 @@ void onLogoutPressed(ActionEvent event) {
public void initialize(URL location, ResourceBundle resources) {
Navigator.navigateInContainer(Global.isCommittee() ? "appointments" : "home", vbxContent, null);

if(Global.isCommittee()) {
btnAccount.setManaged(false);
}

initializeIcons();
Global.setDashboardReference(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.oodj.vaccspace.controllers.profile;

import com.oodj.vaccspace.Global;
import com.oodj.vaccspace.controllers.BaseController;
import com.oodj.vaccspace.models.Person;
import com.oodj.vaccspace.utils.Navigator;
import com.oodj.vaccspace.utils.Page;
import io.github.euseanwoon.MFXPillButton;
import io.github.palexdev.materialfx.controls.MFXPasswordField;
import io.github.palexdev.materialfx.controls.MFXTextField;
import io.github.palexdev.materialfx.controls.enums.DialogType;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import org.kordamp.ikonli.javafx.FontIcon;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

public class ChangePasswordController extends BaseController implements Initializable {

Person person = new Person();

@FXML
private MFXPillButton btnSaveChanges;

@FXML
private Label lblClose;

@FXML
private MFXPasswordField txtNewPassword;

@FXML
private MFXPasswordField txtOldPassword;

@FXML
private MFXPasswordField txtRepeatPassword;

@FXML
void onClosePressed(MouseEvent event) {
getStageDialog().close();
Navigator.showInDialog(lblClose.getScene().getWindow(),"view_profile", null);
}

@FXML
void onSaveChangesPressed(ActionEvent event) {
if (!validateInput()) {
return;
}

Optional<ButtonType> result = Page.showDialogAndWait(
txtNewPassword.getScene().getWindow(),
"Changing password",
"You are about to change your password",
"Do you want to proceed?"
);
if (result.isPresent() && result.get() == ButtonType.OK) {
person.setPassword(txtNewPassword.getPassword());
person.save();

getStageDialog().close();
Navigator.showInDialog(lblClose.getScene().getWindow(),"view_profile", null);
}
}

private boolean validateInput() {
person = Global.getLoggedInUser();

//Empty Fields Validation
if (txtNewPassword.getPassword().equals("") ||
txtOldPassword.getPassword().equals("") ||
txtRepeatPassword.getPassword().equals("")
) {
Page.showDialog(
txtNewPassword.getScene().getWindow(),
DialogType.ERROR,
"Error: Empty fields",
"All fields must be filled in!"
);
return false;
}

if (!txtRepeatPassword.getPassword().equals(txtNewPassword.getPassword())) {
Page.showDialog(
txtRepeatPassword.getScene().getWindow(),
DialogType.ERROR,
"Error: Repeat password is wrong",
"Please ensure the repeated password is correct!"
);
return false;
}

if (!txtOldPassword.getPassword().equals(person.getPassword())) {
Page.showDialog(
txtOldPassword.getScene().getWindow(),
DialogType.ERROR,
"Error: Wrong Password",
"Old Password is wrong!"
);
return false;
}

return true;
}

@Override
public void initialize(URL location, ResourceBundle resources) {
FontIcon icon = new FontIcon("fas-arrow-left");
icon.setIconColor(Color.WHITE);
icon.setIconSize(18);

lblClose.setGraphic(icon);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.oodj.vaccspace.controllers.profile;

import com.oodj.vaccspace.Global;
import com.oodj.vaccspace.controllers.BaseController;
import com.oodj.vaccspace.models.Person;
import com.oodj.vaccspace.utils.Navigator;
import com.oodj.vaccspace.utils.Page;
import com.oodj.vaccspace.utils.ValidationHelper;
import io.github.euseanwoon.MFXPillButton;
import io.github.palexdev.materialfx.controls.MFXCheckbox;
import io.github.palexdev.materialfx.controls.MFXTextField;
import io.github.palexdev.materialfx.controls.enums.DialogType;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import org.kordamp.ikonli.javafx.FontIcon;
import textorm.TextORM;

import java.net.URL;
import java.util.Objects;
import java.util.Optional;
import java.util.ResourceBundle;

public class ViewProfileController extends BaseController implements Initializable {

Person person = new Person();

@FXML
private MFXPillButton btnChangePassword;

@FXML
private MFXPillButton btnDelete;

@FXML
private MFXPillButton btnEditPerson;

@FXML
private MFXPillButton btnSaveChanges;

@FXML
private MFXCheckbox chkIsNonCitizen;

@FXML
private Label lblClose;

@FXML
private MFXTextField txtEmail;

@FXML
private MFXTextField txtName;

@FXML
private MFXTextField txtPhoneNumber;

@FXML
void onClosePressed(MouseEvent event) {
getStageDialog().close();
}

@FXML
void onChangePasswordPressed(ActionEvent event) {
Navigator.showInDialog(btnChangePassword.getScene().getWindow(),"change_password", null);
}

@FXML
void onEditPersonPressed(ActionEvent event) {
btnEditPerson.setManaged(false);
btnSaveChanges.setManaged(true);

txtPhoneNumber.setDisable(false);
txtEmail.setDisable(false);
chkIsNonCitizen.setDisable(false);

txtPhoneNumber.setStyle("-fx-opacity: 0.99");
txtEmail.setStyle("-fx-opacity: 0.99");
}

@FXML
void onSaveChangesPressed(ActionEvent event) {
if (!validateInput()) {
return;
}


Optional<ButtonType> result = Page.showDialogAndWait(
txtEmail.getScene().getWindow(),
"Edit Profile",
"You are about to edit your profile details",
"Do you want to proceed?"
);
if (result.isPresent() && result.get() == ButtonType.OK) {
person.setPhone(txtPhoneNumber.getText());
person.setEmail(txtEmail.getText());
person.setNonCitizen(chkIsNonCitizen.isSelected());
person.save();

getStageDialog().close();
}
}

private boolean validateInput() {
//Empty Fields Validation
if (txtEmail.getText().equals("") ||
txtPhoneNumber.getText().equals("")
) {
Page.showDialog(
txtName.getScene().getWindow(),
DialogType.ERROR,
"Error: Empty fields",
"All fields must be filled in!"
);
return false;
}

//Phone Validation
if (!txtPhoneNumber.getText().matches("^[0-9]{4,}$")) {
Page.showDialog(
txtName.getScene().getWindow(),
DialogType.ERROR,
"Error: Invalid Phone",
"Phone number must only have numbers!"
);
return false;
}

//Email Validation
if (!ValidationHelper.isValidEmail(txtEmail.getText())) {
Page.showDialog(
txtName.getScene().getWindow(),
DialogType.ERROR,
"Error: Invalid Email",
"Invalid Email Address!"
);
return false;
}

//Email Duplication Validation
Person duplicate = TextORM.getOne(
Person.class,
data -> Objects.equals(data.get("email"), txtEmail.getText()) &&
Integer.parseInt(data.get("id")) != person.getId()
);

if (duplicate != null) {
Page.showDialog(
txtName.getScene().getWindow(),
DialogType.ERROR,
"Error: Duplicate Email",
"The given email is already registered!"
);
return false;
}

return true;
}

@Override
public void initialize(URL location, ResourceBundle resources) {
FontIcon icon = new FontIcon("fas-times");
icon.setIconColor(Color.WHITE);
icon.setIconSize(18);

lblClose.setGraphic(icon);
}

@Override
public void onLoaded() {
btnSaveChanges.setManaged(false);

person = Global.getLoggedInUser();

txtName.setText(person.getName());
txtPhoneNumber.setText(person.getPhone());
txtEmail.setText(person.getEmail());
chkIsNonCitizen.setSelected(person.isNonCitizen());
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/oodj/vaccspace/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class User extends Model {
@Column
private String phone;

@Column
@Column
private String email;

@Column
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/oodj/vaccspace/utils/Navigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class Navigator {
put("view_batch", new Page("views/view-vaccine-batches-view.fxml", "View Vaccine Batch"));
put("new_people", new Page("views/new-people-view.fxml", "Add Person"));
put("view_people", new Page("views/view-people-view.fxml", "View Person"));
put("view_profile", new Page("views/view-profile-view.fxml", "View Profile"));
put("change_password", new Page("views/change-password-view.fxml", "Change Password"));
put("reports", new Page("views/reports-view.fxml", "Reports"));
}};

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
exports com.oodj.vaccspace.controllers.reports;
opens com.oodj.vaccspace.controllers.reports to javafx.fxml;

exports com.oodj.vaccspace.controllers.profile;
opens com.oodj.vaccspace.controllers.profile to javafx.fxml;

exports com.oodj.vaccspace.models;
}
Loading

0 comments on commit 12ac9fe

Please sign in to comment.