aboutsummaryrefslogtreecommitdiff
path: root/src/UI/MainWindow.java
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-21 23:42:22 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2024-11-21 23:42:22 +0530
commit9a423e939273fcfe805cde22bb8c8c8a74655702 (patch)
tree12fca7dd2413af2d57b25c1f23b90d7db222c1fe /src/UI/MainWindow.java
parentc82c062ed61879a1f8e32d5ec53eb02b1e0fb628 (diff)
Added Sign Up functionality (crappy but better than nothing)HEADmaster
Diffstat (limited to 'src/UI/MainWindow.java')
-rw-r--r--src/UI/MainWindow.java87
1 files changed, 59 insertions, 28 deletions
diff --git a/src/UI/MainWindow.java b/src/UI/MainWindow.java
index 8c3964e..ad015c5 100644
--- a/src/UI/MainWindow.java
+++ b/src/UI/MainWindow.java
@@ -4,18 +4,17 @@ import Client.Book;
import Client.SocketClient;
import UI.Components.BooksList;
import UI.Components.LoginPanel;
-import org.json.JSONArray;
import org.json.JSONException;
-import org.json.JSONObject;
import javax.swing.*;
import java.awt.*;
-import java.net.Socket;
public class MainWindow extends JFrame {
public static String url = "http://localhost:8080";
public static int userId = 0;
+ private LoginPanel loginPanel;
+
public MainWindow() {
this.setTitle("User Panel - Library Management System");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
@@ -26,38 +25,35 @@ public class MainWindow extends JFrame {
CardLayout cardLayout = new CardLayout();
JPanel cardPanel = new JPanel(cardLayout);
- LoginPanel loginPanel = new LoginPanel();
+ loginPanel = new LoginPanel();
loginPanel.loginButton.addActionListener(e -> {
- new SocketClient(url);
- SocketClient.socket.connect();
+ if (SocketClient.socket == null) {
+ new SocketClient(url);
+ SocketClient.socket.connect();
+ }
+
+ login(cardLayout, cardPanel);
+ });
+ loginPanel.signUpButton.addActionListener(e -> {
+ if (SocketClient.socket == null) {
+ new SocketClient(url);
+ SocketClient.socket.connect();
+ }
- SocketClient.socket.emit("login",
+ loginPanel.statusLabel.setText("Attempting to Sign Up...");
+
+ SocketClient.socket.emit("signUp",
"{" +
"\"userName\": \"" + loginPanel.usernameInput.getValue() + "\"," +
"\"password\": \"" + loginPanel.passwordInput.getValue() + "\"" +
"}");
- SocketClient.socket.on("loggedIn", data -> {
- MainWindow.userId = (int) data[0];
-
- SocketClient.socket.on("allBooksList", d -> {
- try {
- BooksList.refreshBooks(Book.fromObjects(d));
- } catch (JSONException ex) {
- throw new RuntimeException(ex);
- }
- });
-
- SocketClient.socket.on("booksUpdated", d -> {
- try {
- BooksList.refreshBooks(Book.fromObjects(d));
- } catch (JSONException ex) {
- throw new RuntimeException(ex);
- }
- });
-
- SocketClient.socket.emit("getAllBooks");
- cardLayout.next(cardPanel);
+ SocketClient.socket.on("signedUp", data -> {
+ this.login(cardLayout, cardPanel);
+ });
+
+ SocketClient.socket.on("signUpFailed", data -> {
+ loginPanel.statusLabel.setText("Sign Up Failed");
});
});
cardPanel.add(loginPanel);
@@ -66,4 +62,39 @@ public class MainWindow extends JFrame {
this.add(cardPanel);
}
+
+ private void login(CardLayout cardLayout, JPanel cardPanel) {
+ SocketClient.socket.emit("login",
+ "{" +
+ "\"userName\": \"" + loginPanel.usernameInput.getValue() + "\"," +
+ "\"password\": \"" + loginPanel.passwordInput.getValue() + "\"" +
+ "}");
+
+ SocketClient.socket.on("loginFailed", data -> {
+ loginPanel.statusLabel.setText("Failed to Log In. Check Username or Password.");
+ });
+
+ SocketClient.socket.on("loggedIn", data -> {
+ MainWindow.userId = (int) data[0];
+
+ SocketClient.socket.on("allBooksList", d -> {
+ try {
+ BooksList.refreshBooks(Book.fromObjects(d));
+ } catch (JSONException ex) {
+ throw new RuntimeException(ex);
+ }
+ });
+
+ SocketClient.socket.on("booksUpdated", d -> {
+ try {
+ BooksList.refreshBooks(Book.fromObjects(d));
+ } catch (JSONException ex) {
+ throw new RuntimeException(ex);
+ }
+ });
+
+ SocketClient.socket.emit("getAllBooks");
+ cardLayout.next(cardPanel);
+ });
+ }
}