Skip to content

Commit

Permalink
Fix error on React Flow with Auto Login = False (#2164)
Browse files Browse the repository at this point in the history
This pull request addresses several improvements and fixes to enhance
the overall functionality and user experience. The key changes include:

- Improved Folders Behavior Context

- Enhanced Duplicate Request Blocker

- Bug Fix: Folders Compatibility with AutoLogin = False
  • Loading branch information
Cristhianzl authored Jun 14, 2024
2 parents 9000973 + 57a7b2b commit f520e0b
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 83 deletions.
27 changes: 13 additions & 14 deletions src/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import { autoLogin, getGlobalVariables, getHealth } from "./controllers/API";
import { setupAxiosDefaults } from "./controllers/API/utils";
import useTrackLastVisitedPath from "./hooks/use-track-last-visited-path";
import Router from "./routes";
import { Case } from "./shared/components/caseComponent";
import useAlertStore from "./stores/alertStore";
import { useDarkStore } from "./stores/darkStore";
import useFlowsManagerStore from "./stores/flowsManagerStore";
import { useFolderStore } from "./stores/foldersStore";
import { useGlobalVariablesStore } from "./stores/globalVariablesStore/globalVariables";
import { useStoreStore } from "./stores/storeStore";
import { useTypesStore } from "./stores/typesStore";
export default function App() {
useTrackLastVisitedPath();

Expand All @@ -43,10 +43,8 @@ export default function App() {

const { isAuthenticated, login, setUserData, setAutoLogin, getUser } =
useContext(AuthContext);
const refreshFlows = useFlowsManagerStore((state) => state.refreshFlows);
const setLoading = useAlertStore((state) => state.setLoading);
const fetchApiData = useStoreStore((state) => state.fetchApiData);
const getTypes = useTypesStore((state) => state.getTypes);
const refreshVersion = useDarkStore((state) => state.refreshVersion);
const refreshStars = useDarkStore((state) => state.refreshStars);
const setGlobalVariables = useGlobalVariablesStore(
Expand All @@ -56,8 +54,7 @@ export default function App() {
const navigate = useNavigate();
const dark = useDarkStore((state) => state.dark);

const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const loadingFolders = useFolderStore((state) => state.loading);
const isLoadingFolders = useFolderStore((state) => state.isLoadingFolders);

const [isLoadingHealth, setIsLoadingHealth] = useState(false);

Expand Down Expand Up @@ -115,13 +112,13 @@ export default function App() {
if (isAuthenticated) {
try {
await setupAxiosDefaults();
await getFoldersApi();
await getTypes();
await refreshFlows();

const res = await getGlobalVariables();
setGlobalVariables(res);

checkHasStore();
fetchApiData();

resolve();
} catch (error) {
console.error("Failed to fetch data:", error);
Expand Down Expand Up @@ -174,6 +171,8 @@ export default function App() {
}
};

const isLoadingApplication = isLoading || isLoadingFolders;

return (
//need parent component with width and height
<div className="flex h-full flex-col">
Expand All @@ -196,15 +195,15 @@ export default function App() {
></FetchErrorComponent>
}

{isLoading || loadingFolders ? (
<Case condition={isLoadingApplication}>
<div className="loading-page-panel">
<LoadingComponent remSize={50} />
</div>
) : (
<>
<Router />
</>
)}
</Case>

<Case condition={!isLoadingApplication}>
<Router />
</Case>
</>
</ErrorBoundary>
<div></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ const SideBarFoldersButtonsComponent = ({
const currentFolder = pathname.split("/");
const urlWithoutPath = pathname.split("/").length < 4;
const myCollectionId = useFolderStore((state) => state.myCollectionId);
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const folderIdDragging = useFolderStore((state) => state.folderIdDragging);
const refreshFolders = useFolderStore((state) => state.refreshFolders);

const checkPathName = (itemId: string) => {
if (urlWithoutPath && itemId === myCollectionId) {
Expand Down Expand Up @@ -85,7 +85,7 @@ const SideBarFoldersButtonsComponent = ({
function addNewFolder() {
addFolder({ name: "New Folder", parent_id: null, description: "" }).then(
(res) => {
getFoldersApi(true);
refreshFolders();
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const useFileDrop = (folderId, folderChangeCallback) => {
);

const setErrorData = useAlertStore((state) => state.setErrorData);
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const refreshFolders = useFolderStore((state) => state.refreshFolders);
const flows = useFlowsManagerStore((state) => state.flows);

const triggerFolderChange = (folderId) => {
Expand Down Expand Up @@ -118,7 +118,7 @@ const useFileDrop = (folderId, folderChangeCallback) => {
setFolderIdDragging("");

updateFlowInDatabase(updatedFlow).then(() => {
getFoldersApi(true);
refreshFolders();
triggerFolderChange(folderId);
});
};
Expand All @@ -129,7 +129,7 @@ const useFileDrop = (folderId, folderChangeCallback) => {
setFolderDragging(false);
setFolderIdDragging("");
uploadFlowsFromFolders(formData).then(() => {
getFoldersApi(true);
refreshFolders();
triggerFolderChange(folderId);
});
};
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/sidebarComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function SidebarNav({
}: SidebarNavProps) {
const location = useLocation();
const pathname = location.pathname;
const loadingFolders = useFolderStore((state) => state.loading);
const loadingFolders = useFolderStore((state) => state.isLoadingFolders);
const folders = useFolderStore((state) => state.folders);

const pathValues = ["folder", "components", "flows", "all"];
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/tableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const TableComponent = forwardRef<
}
}, 50);
setTimeout(() => {
realRef.current.api.hideOverlay();
realRef?.current?.api?.hideOverlay();
}, 1000);
if (props.onGridReady) props.onGridReady(params);
};
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/contexts/authContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useNavigate } from "react-router-dom";
import Cookies from "universal-cookie";
import { getLoggedUser, requestLogout } from "../controllers/API";
import useAlertStore from "../stores/alertStore";
import { useFolderStore } from "../stores/foldersStore";
import { Users } from "../types/api";
import { AuthContextType } from "../types/contexts/auth";

Expand Down Expand Up @@ -42,7 +43,8 @@ export function AuthProvider({ children }): React.ReactElement {
const [apiKey, setApiKey] = useState<string | null>(
cookies.get("apikey_tkn_lflw")
);
// const getFoldersApi = useFolderStore((state) => state.getFoldersApi);

const getFoldersApi = useFolderStore((state) => state.getFoldersApi);

useEffect(() => {
const storedAccessToken = cookies.get("access_token_lf");
Expand All @@ -64,7 +66,8 @@ export function AuthProvider({ children }): React.ReactElement {
setUserData(user);
const isSuperUser = user!.is_superuser;
setIsAdmin(isSuperUser);
// await getFoldersApi(true);

getFoldersApi(true, true);
})
.catch((error) => {
setLoading(false);
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function ApiInterceptor() {

if (!checkRequest) {
controller.abort("Duplicate Request");
console.error("Duplicate Request");
}

const accessToken = cookies.get("access_token_lf");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export function checkDuplicateRequestAndStoreRequest(config) {
const lastUrl = localStorage.getItem("lastUrlCalled");
const lastMethodCalled = localStorage.getItem("lastMethodCalled");
const lastRequestTime = localStorage.getItem("lastRequestTime");
const lastCurrentUrl = localStorage.getItem("lastCurrentUrl");

const currentUrl = window.location.pathname;
const currentTime = Date.now();

const isContained = AUTHORIZED_DUPLICATE_REQUESTS.some((request) =>
config?.url!.includes(request)
);
Expand All @@ -17,14 +18,16 @@ export function checkDuplicateRequestAndStoreRequest(config) {
lastMethodCalled === config.method &&
lastMethodCalled === "get" && // Assuming you want to check only for GET requests
lastRequestTime &&
currentTime - parseInt(lastRequestTime, 10) < 800
currentTime - parseInt(lastRequestTime, 10) < 300 &&
lastCurrentUrl === currentUrl
) {
return false;
}

localStorage.setItem("lastUrlCalled", config.url ?? "");
localStorage.setItem("lastMethodCalled", config.method ?? "");
localStorage.setItem("lastRequestTime", currentTime.toString());
localStorage.setItem("lastCurrentUrl", currentUrl);

return true;
}
2 changes: 0 additions & 2 deletions src/frontend/src/controllers/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,6 @@ export async function getMessagesTable(

const rowsOrganized = rows.data;

console.log(rowsOrganized);

const columns = extractColumnsFromRows(rowsOrganized, mode, excludedFields);
const sessions = new Set<string>();
rowsOrganized.forEach((row) => {
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/pages/AdminPage/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default function LoginAdminPage() {
const [inputState, setInputState] =
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
const { login, isAuthenticated, setUserData } = useContext(AuthContext);
const setLoading = useAlertStore((state) => state.setLoading);

const { password, username } = inputState;
const setErrorData = useAlertStore((state) => state.setErrorData);
Expand All @@ -35,6 +36,10 @@ export default function LoginAdminPage() {
};
onLogin(user)
.then((user) => {
console.log("admin page");

setLoading(true);

login(user.access_token);
navigate("/admin/");
})
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/pages/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CONTROL_LOGIN_STATE } from "../../constants/constants";
import { AuthContext } from "../../contexts/authContext";
import { onLogin } from "../../controllers/API";
import useAlertStore from "../../stores/alertStore";
import useFlowsManagerStore from "../../stores/flowsManagerStore";
import { LoginType } from "../../types/api";
import {
inputHandlerEventType,
Expand All @@ -23,6 +24,7 @@ export default function LoginPage(): JSX.Element {
const { login } = useContext(AuthContext);
const navigate = useNavigate();
const setErrorData = useAlertStore((state) => state.setErrorData);
const setLoading = useFlowsManagerStore((state) => state.setIsLoading);

function handleInput({
target: { name, value },
Expand All @@ -37,6 +39,9 @@ export default function LoginPage(): JSX.Element {
};
onLogin(user)
.then((user) => {
console.log("login page");

setLoading(true);
login(user.access_token);
navigate("/");
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
import { useState } from "react";
import { useLocation } from "react-router-dom";
import useAlertStore from "../../../../../../stores/alertStore";
import useFlowsManagerStore from "../../../../../../stores/flowsManagerStore";
import { useFolderStore } from "../../../../../../stores/foldersStore";
import { handleDownloadFolderFn } from "../../../../utils/handle-download-folder";
import InputSearchComponent from "../inputSearchComponent";
import TabsSearchComponent from "../tabsComponent";

type HeaderTabsSearchComponentProps = {};

const HeaderTabsSearchComponent = ({}: HeaderTabsSearchComponentProps) => {
const location = useLocation();
const myCollectionId = useFolderStore((state) => state.myCollectionId);
const folderId = location?.state?.folderId || myCollectionId;
const isLoading = useFlowsManagerStore((state) => state.isLoading);
const [tabActive, setTabActive] = useState("Flows");
const setErrorData = useAlertStore((state) => state.setErrorData);
const allFlows = useFlowsManagerStore((state) => state.allFlows);
const [inputValue, setInputValue] = useState("");

const setSearchFlowsComponents = useFlowsManagerStore(
(state) => state.setSearchFlowsComponents
);

const handleDownloadFolder = () => {
if (allFlows.length === 0) {
setErrorData({
title: "Folder is empty",
list: [],
});
return;
}
handleDownloadFolderFn(folderId);
};

return (
<>
<div className="relative flex items-end gap-4">
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/pages/MainPage/hooks/use-delete-folder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import useAlertStore from "../../../stores/alertStore";
import { useFolderStore } from "../../../stores/foldersStore";
import { deleteFolder, getFolderById } from "../services";

const useDeleteFolder = ({ navigate, getFoldersApi }) => {
const useDeleteFolder = ({ navigate }) => {
const setSuccessData = useAlertStore((state) => state.setSuccessData);
const setErrorData = useAlertStore((state) => state.setErrorData);
const folderToEdit = useFolderStore((state) => state.folderToEdit);
const myCollectionId = useFolderStore((state) => state.myCollectionId);
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);

const handleDeleteFolder = () => {
deleteFolder(folderToEdit?.id!)
Expand Down
9 changes: 1 addition & 8 deletions src/frontend/src/pages/MainPage/pages/mainPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@ export default function HomePage(): JSX.Element {
const [openFolderModal, setOpenFolderModal] = useState(false);
const [openDeleteFolderModal, setOpenDeleteFolderModal] = useState(false);
const is_component = pathname === "/components";
const getFoldersApi = useFolderStore((state) => state.getFoldersApi);
const setFolderToEdit = useFolderStore((state) => state.setFolderToEdit);
const navigate = useNavigate();

useEffect(() => {
setTimeout(() => {
getFoldersApi();
}, 300);
}, []);

useEffect(() => {
setCurrentFlowId("");
}, [pathname]);
Expand All @@ -45,7 +38,7 @@ export default function HomePage(): JSX.Element {
is_component,
});

const { handleDeleteFolder } = useDeleteFolder({ navigate, getFoldersApi });
const { handleDeleteFolder } = useDeleteFolder({ navigate });

return (
<>
Expand Down
Loading

0 comments on commit f520e0b

Please sign in to comment.