Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Remove useeffect
Browse files Browse the repository at this point in the history
  • Loading branch information
khelif96 committed Dec 19, 2023
1 parent bb99e39 commit 21e27ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/pages/taskQueue/TaskQueueTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Body, Disclaimer } from "@leafygreen-ui/typography";
import { useTaskQueueAnalytics } from "analytics";
import { StyledRouterLink, WordBreak } from "components/styles";
import { BaseTable } from "components/Table/BaseTable";
import { TablePlaceholder } from "components/Table/TablePlaceholder";
import {
getVersionRoute,
getTaskRoute,
Expand All @@ -23,7 +24,7 @@ interface TaskQueueTableProps {

const TaskQueueTable: React.FC<TaskQueueTableProps> = ({
loading,
taskQueue,
taskQueue = [],
}) => {
const taskQueueAnalytics = useTaskQueueAnalytics();
const tableContainerRef = useRef<HTMLDivElement>(null);
Expand All @@ -46,6 +47,7 @@ const TaskQueueTable: React.FC<TaskQueueTableProps> = ({
table={table}
loading={loading && taskQueue?.length === 0}
shouldAlternateRowColor
emptyComponent={<TablePlaceholder message="No tasks found in queue." />}
/>
);
};
Expand Down
41 changes: 19 additions & 22 deletions src/pages/taskQueue/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useMemo } from "react";
import { useState } from "react";
import { useQuery } from "@apollo/client";
import styled from "@emotion/styled";
import Badge from "@leafygreen-ui/badge";
Expand All @@ -24,7 +24,6 @@ import {
import { DISTRO_TASK_QUEUE, TASK_QUEUE_DISTROS } from "gql/queries";
import { usePageTitle } from "hooks";
import { DistroOption } from "./DistroOption";
import TaskQueueTable from "./TaskQueueTable";

const TaskQueue = () => {
const taskQueueAnalytics = useTaskQueueAnalytics();
Expand All @@ -37,34 +36,29 @@ const TaskQueue = () => {
const { data: distrosData } = useQuery<
TaskQueueDistrosQuery,
TaskQueueDistrosQueryVariables
>(TASK_QUEUE_DISTROS, { fetchPolicy: "cache-and-network" });
>(TASK_QUEUE_DISTROS, {
fetchPolicy: "cache-and-network",
onCompleted: (data) => {
const { taskQueueDistros } = data;
const firstDistroInList = taskQueueDistros[0]?.id;
const defaultDistro = distro ?? firstDistroInList;
setSelectedDistro(taskQueueDistros.find((d) => d.id === defaultDistro));
console.log("Redirecting");
navigate(getTaskQueueRoute(defaultDistro, taskId), { replace: true });
},
});

const { data: taskQueueItemsData, loading } = useQuery<
DistroTaskQueueQuery,
DistroTaskQueueQueryVariables
>(DISTRO_TASK_QUEUE, {
variables: { distroId: distro },
fetchPolicy: "cache-and-network",
variables: { distroId: distro },
});

const { distroTaskQueue } = taskQueueItemsData ?? {};
const distros = useMemo(
() => distrosData?.taskQueueDistros ?? [],
[distrosData]
);
const firstDistroInList = distros[0]?.id;

// SET DEFAULT DISTRO AFTER DISTROS HAVE BEEN OBTAINED
useEffect(() => {
if (distros.length) {
const defaultDistro = distro ?? firstDistroInList;
setSelectedDistro(distros.find((d) => d.id === defaultDistro));
navigate(getTaskQueueRoute(defaultDistro, taskId), { replace: true });
}
}, [firstDistroInList, distro, navigate, taskId, distros]);

const onChangeDistroSelection = (val: { id: string }) => {
taskQueueAnalytics.sendEvent({ name: "Select Distro", distro: val.id });
setSelectedDistro(val);
};

const handleSearch = (options: { id: string }[], match: string) =>
Expand All @@ -82,7 +76,7 @@ const TaskQueue = () => {
<SearchableDropdown
data-cy="distro-dropdown"
label="Distro"
options={distros}
options={distrosData?.taskQueueDistros ?? []}
searchFunc={handleSearch}
optionRenderer={(option, onClick) => (
<DistroOption
Expand Down Expand Up @@ -122,7 +116,10 @@ const TaskQueue = () => {
)
}

<TaskQueueTable taskQueue={distroTaskQueue} loading={loading} />
{/* <TaskQueue Table
taskQueue={taskQueueItemsData?.distroTaskQueue}
loading={loading}
/> */}
</>
)}
</PageWrapper>
Expand Down

0 comments on commit 21e27ec

Please sign in to comment.