Skip to content

Commit

Permalink
refactor: state of transcript claim
Browse files Browse the repository at this point in the history
`claimState` is currently passed through 5 nested
components in order to calculate the loading state.
That code wasn't working and after I fixed it with my
previous commits, the logic is now redundant.
This commit removes the redundant logic and simplifies
the related state parameter.
  • Loading branch information
kouloumos authored and Extheoisah committed May 28, 2024
1 parent f2ca54c commit 21e92ae
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 26 deletions.
8 changes: 0 additions & 8 deletions src/components/tables/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ type Props<T> = {
refetch?: <TPageData>(
options?: (RefetchOptions & RefetchQueryFilters<TPageData>) | undefined
) => Promise<QueryObserverResult<any, unknown>>;
actionState?: {
rowId: number;
};
tableStructure: TableStructure<T>[];
tableHeader?: string;
tableHeaderComponent?: React.ReactNode;
Expand All @@ -40,7 +37,6 @@ const BaseTable = <T extends object>({
emptyView,
isLoading,
refetch,
actionState,
tableStructure,
tableHeader,
tableHeaderComponent,
Expand Down Expand Up @@ -90,7 +86,6 @@ const BaseTable = <T extends object>({
}-data-row-${idx}`}
row={dataRow}
ts={tableStructure}
actionState={actionState}
/>
))
) : (
Expand All @@ -105,12 +100,10 @@ const BaseTable = <T extends object>({
const TableRow = <T extends object>({
row,
ts,
actionState,
showControls,
}: {
row: T;
ts: TableStructure<T>[];
actionState: Props<T>["actionState"];
showControls: boolean;
}) => {
return (
Expand All @@ -121,7 +114,6 @@ const TableRow = <T extends object>({
key={tableItem.name}
tableItem={tableItem}
row={row}
actionState={actionState}
/>
))}
</Tr>
Expand Down
17 changes: 7 additions & 10 deletions src/components/tables/QueueTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ const QueueTable = () => {

const retriedClaim = useRef(0);

const [claimState, setClaimState] = useState({
rowId: -1,
});
const [selectedTranscriptId, setSelectedTranscriptId] = useState(-1);
const { data: multipleStatusData } = useUserMultipleReviews({
userId: session?.user?.id,
multipleStatus: ["pending", "active", "inactive"],
Expand Down Expand Up @@ -156,7 +154,7 @@ const QueueTable = () => {
return;
}
if (session?.user?.id) {
setClaimState((prev) => ({ ...prev, rowId: transcriptId }));
setSelectedTranscriptId(transcriptId);

// Fork repo
const forkResult = await axios.post("/api/github/fork");
Expand Down Expand Up @@ -200,7 +198,7 @@ const QueueTable = () => {
throw new Error("failed to claim transcript");
}

setClaimState((prev) => ({ ...prev, rowId: -1 }));
setSelectedTranscriptId(-1);
if (data instanceof Error) {
await retryLoginAndClaim(transcriptId);
return;
Expand All @@ -217,7 +215,7 @@ const QueueTable = () => {

onError: (err) => {
const error = err as Error;
setClaimState((prev) => ({ ...prev, rowId: -1 }));
setSelectedTranscriptId(-1);
toast({
status: "error",
title: error?.message,
Expand Down Expand Up @@ -347,8 +345,8 @@ const QueueTable = () => {
modifier: (data) => data.id,
component: (data) => (
<Button
isDisabled={claimState.rowId !== -1}
isLoading={data.id == claimState.rowId}
isDisabled={selectedTranscriptId !== -1}
isLoading={data.id == selectedTranscriptId}
bgColor={"#EB9B00"}
color="white"
_hover={{ bgColor: "#EB9B00AE" }}
Expand All @@ -361,15 +359,14 @@ const QueueTable = () => {
),
},
] as TableStructure<Transcript>[],
[handleClaim, claimState.rowId]
[handleClaim, selectedTranscriptId]
);

return (
<AdminArchiveSelect>
{({ handleArchive, hasAdminSelected, isArchiving }) => (
<>
<BaseTable
actionState={claimState}
data={data?.data}
emptyView="There are no transcripts awaiting review"
handleArchive={handleArchive}
Expand Down
5 changes: 0 additions & 5 deletions src/components/tables/TableItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export const Tags = <T extends object>({
export const TableAction = <T extends object>({
tableItem,
row,
actionState,
showControls,
}: TableDataElement<T> & { showControls: boolean }) => {
const { data: userSession } = useSession();
Expand All @@ -120,7 +119,6 @@ export const TableAction = <T extends object>({
tableItem.action(row);
};

const isLoading = "id" in row && row.id === actionState?.rowId;
const isAdmin = userSession?.user?.permissions === "admin";
const showCheckBox = isAdmin && showControls;

Expand All @@ -134,7 +132,6 @@ export const TableAction = <T extends object>({
<Button
title={tableItem.isDisabledText}
isDisabled={tableItem.isDisabled}
isLoading={isLoading}
colorScheme="orange"
size="sm"
onClick={handleClick}
Expand Down Expand Up @@ -211,7 +208,6 @@ export const DataEmpty = ({
export const RowData = <T extends object>({
row,
tableItem,
actionState,
showControls,
}: TableDataElement<T> & { showControls: boolean }) => {
switch (tableItem.type) {
Expand All @@ -234,7 +230,6 @@ export const RowData = <T extends object>({
key={tableItem.name}
tableItem={tableItem}
row={row}
actionState={actionState}
/>
);

Expand Down
3 changes: 0 additions & 3 deletions src/components/tables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,4 @@ export type TableStructure<T> = {
export type TableDataElement<T> = {
tableItem: TableStructure<T>;
row: T;
actionState?: {
rowId: number;
};
};

0 comments on commit 21e92ae

Please sign in to comment.