jc4balos / opa-file-bank

frontend for OPA File Bank
0 stars 0 forks source link

getSession repeatedly called #22

Open jc4balos opened 3 months ago

jc4balos commented 3 months ago

image

jc4balos commented 3 months ago

This is due to multiple components calling the getSession function. Calling it once should be enough and passing it on a useContext() but since the process is asynchronous, im having a hard time in fetching it which led to this.


  /**
   * Fetches session data from the server and handles the response accordingly.
   *
   * @param {Location} location - The location object representing the current browser URL.
   * @returns {Promise<void>} - A promise that resolves when the session data is fetched and processed.
   */
  const fetchSessionData = async (location) => {
    fullScreenLoading.show();
    const sessionService = new SessionService();
    const response = await sessionService.getSessionData();

    if (response.status !== 200 && location.pathname !== "/login") {
      const data = await response.json();
      errorModal.showErrorModal(data.message, true);
      fullScreenLoading.close();

      setTimeout(() => {
        window.location.href = "/login";
      }, 3000);
    } else {
      const data = await response.json();
      await userData.setUserData(
        data.userFullName,
        data.userName,
        data.userFullName,
        data.userId,
        data.accessLevelId
      );

      fullScreenLoading.close();
      return data;
    }
  };

  const session = { fetchSessionData };

will try to implement passing its response into userData Object

  const userData = {
    setUserData,
    userId,
    userAccessLevelId,
    userFullName,
    userTitle,
    userName,
  };